Data posting to mysql database

Lavanya rajan

Well-known member
Hello All,

I'm trying to send data from a Teensy 4.0 controller to a MySQL database. The board I'm using for testing is the Teensy 4.0 Expansion Board, which has an inbuilt ESP chip for data transmission over Wi-Fi.

While I can successfully post data, the interval between postings is currently 10-12 seconds, which is too long. We need the data to be updated every 2 seconds at least. When I attempt to reduce the interval, the connection to the database sometimes fails entirely. Can anyone help me resolve this issue?

attaching the code

Code:
String SSID = "xxx";
String PASSWORD = "xxx";
bool prntln = false;
String HOST_NAME = "xxxx";

unsigned long previousMillis = 0;
const long interval = 1000; // Send data every 1 seconds

float c = 25;
float h = 50;

void sendCommand(String command, unsigned long timeout);

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);  // Use Serial1 for communication with the ESP8266
  delay(2000);
  flushSerial();
  delay(200);
  Serial.println(F("Started"));

  // Check communications with the ESP8266
  Serial.println("Checking ESP8266 Version...");
  sendCommand("AT+GMR", 5000);

  Serial.println("Checking AT...");
  sendCommand("AT", 5000);

  Serial.println("Checking CWMODE...");
  sendCommand("AT+CWMODE?", 5000);

  Serial.println("Set to Station Mode...");
  sendCommand("AT+CWMODE=1", 5000);

  Serial.println("List Access points...");
  sendCommand("AT+CWLAP", 5000);

  Serial.println("Join WiFi....");
  String joinstring = "AT+CWJAP=\"";
  joinstring += SSID + "\",\"";
  joinstring += PASSWORD + "\"";
  sendCommand(joinstring, 7000);

  Serial.println("Get IP Address...");
  sendCommand("AT+CIFSR", 7000);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // Time to send data
    previousMillis = currentMillis;

    // Now, you can establish a connection to the server "cmti-edge.online" on port 80.
    Serial.println("Connecting to database...");
    sendCommand("AT+CIPSTART=\"TCP\",\"xxxx\",80", 10000);

    // Send data to the server using the provided JSON payload and PHP details
    String jsonPayload = "{\"hum\":" + String(c) + ",\"temp\":" + String(h) + "}";
    String data = "POST /xxx/xxx.php HTTP/1.1\r\n";
    data += "Host: " + HOST_NAME + "\r\n";
    data += "Content-Type: application/json\r\n";
    data += "Content-Length: " + String(jsonPayload.length()) + "\r\n\r\n";
    data += jsonPayload;

    // Send the HTTP request
    String sendRequest = "AT+CIPSEND=" + String(data.length());
    sendCommand(sendRequest, 5000);
    sendCommand(data, 5000);
    if (recFind("CLOSED", 5000, true)) {
      Serial.println("CLOSED");
    }
    else {
      Serial.println("CLOSED");
    }
  }

  // Your other code or delay can go here to perform other tasks
}

void sendCommand(String command, unsigned long timeout) {
  Serial1.println(command);  // Use Serial1 for communication with the ESP8266
  if (recFind("OK", timeout, true)) {
    Serial.println("Success");
  } else {
    Serial.println("Failed");
  }
}

void flushSerial() {
  while (Serial1.available() >= 1) {
    Serial1.read();
  }
}

bool recFind(String target, uint32_t timeout, bool prntln) {
  char rdChar = "";
  String rdBuff = "";
  unsigned long startMillis = millis();
  while (millis() - startMillis < timeout) {
    while (Serial1.available() > 0) {
      rdChar = Serial1.read();
      rdBuff += rdChar;
      if (prntln) {
        Serial.write(rdChar);
      }
      if (rdBuff.indexOf(target) != -1) {
        break;
      }
    }
    if (rdBuff.indexOf(target) != -1) {
      break;
    }
  }
  if (rdBuff.indexOf(target) != -1) {
    return true;
  } else {
    return false;
  }
}
 
Back
Top