2

I'm using an ESP32 WROOM board to capture some data in the field and send it over using arduino json. However, the complete process of sending and receiving confirmation takes up around 12 seconds and my ESP is frozen in that time. As a result, I lose the data in that time window. Is there a way to reduce the upload time of the ESP or maybe continue taking data while it is uploading?

Here is my data sending code for reference.

jsonBuffer.clear();
JsonObject &rootObject = jsonBuffer.createObject();
JsonObject &dataObject = rootObject.createNestedObject("data");
JsonArray &X1dataObject = dataObject.createNestedArray("X1");
JsonArray &Y1dataObject = dataObject.createNestedArray("Y1");
JsonArray &Z1dataObject = dataObject.createNestedArray("Z1");

if (wifiMulti.run() == WL_CONNECTED) {
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


if (send_flag) {
  send_flag = false;
  int connection_iterator = 0;
  int connection_iterator2 = 0;
  for (int iterator = 0; iterator < SAMPLE_SIZE; iterator++) {
    X1dataObject.add(X1dataArray[iterator]);
    Y1dataObject.add(Y1dataArray[iterator]);
    Z1dataObject.add(Z1dataArray[iterator]);
  }

  rootObject["coreid"] = String(low, HEX) + String(high, HEX);
  //rootObject["coreid_high"] = String(high, HEX);
  rootObject["sample_time"] = int(time_taken);
  rootObject["firmware_version"] = FIRMWARE_UPDATE_VERSION;
  rootObject.prettyPrintTo(Serial);
  Serial.print("connecting to ");
  Serial.println(host);
  while ((!client.connect(host, httpsPort)) && !setup_mode) {
    connection_iterator++;
    Serial.println("connection failed! Retrying..");
    digitalWrite(LED2, HIGH);
    delay(5000);
    digitalWrite(LED2, LOW);
    delay(5000);
    if (connection_iterator >= 10)
      ESP.restart();
  }
  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print("requesting URL: ");
  Serial.println(url);
  client.println(String("POST ") + url + " HTTP/1.0");
  client.println(String("Host: ") + host);
  client.println("Cache-Control: no-cache");
  client.println("Content-Type: application/json");
  client.print("Content-Length: ");
  client.println(rootObject.measureLength());
  client.println();
  //rootObject.printTo(client);
  Serial.println("NUMBER OF INTERRUPTS:");
  Serial.println(interruptCounter);
  Serial.println("request sent");
  while ((client.connected()) && !setup_mode) {
    connection_iterator2++;

    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
    Serial.println("Waiting for response");
    digitalWrite(LED2, HIGH);
    delay(1000);
    digitalWrite(LED2, LOW);
    delay(1000);
    if (connection_iterator2 >= 20)
      break;

  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
  digitalWrite(LED_BUILTIN, LOW);
  // readRegister(INT_SOURCE, 1, &dummy_read);
}
VE7JRO
  • 2,497
  • 15
  • 24
  • 29
  • In general, you would need to examine, in detail, which individual steps are taking the most time and try to find out how to reduce that. 12 seconds is enormous for sending an HTTP request and getting a reply, and the delay might be in the ESP32 code, might be in the website receiving the data, etc. Find out where most of the time is wasted and focus efforts there. (I am assuming sending data is an HTTP POST or GET, as json is not a protocol, but a structured text format.) – jose can u c Apr 16 '19 at 13:46
  • you send it as many small writes? https://github.com/jandrassy/StreamLib/blob/master/README.md – Juraj Apr 16 '19 at 14:21
  • JSON can be very verbose. It might be easier to send a regular GET request with a few data-variable in the query string. – Gerben Apr 16 '19 at 16:07
  • esp32 is dual core. you can send on one core while the other continues to capture data. it is a bit complicated to get going, but works well. Aside from that, 12 seconds seems too long, maybe you should post some code here and we might be able to speed that up. – dandavis Apr 16 '19 at 19:08
  • How big is this data? 12 seconds on the lowest connection speed - say 1mbit is, and taking into account some overheads, would be over 1megabytes of data - are you really sending that much? – Jaromanda X Apr 17 '19 at 01:43
  • I don't think I'm sending that much data. It just consists of 350x3 data points from the accelerometer and some header info for the server side. I am also trying to use the other core for sending data but it is giving task wdt issues. I will post my data sending code above. – peeyush tekriwal Apr 17 '19 at 07:00
  • my comment applies. the rootObject.printTo writes in small pieces. wrap client into BufferedPrint from StreamLib – Juraj Apr 18 '19 at 13:07
  • 1
    Hey, @Juraj will the StreamLib be able to send the rootObject as it is or I would have to store the values in a different buffer to be sent over> – peeyush tekriwal Apr 26 '19 at 11:43
  • 1
    And also if I use the BufferedPrint the Json encoding will be lost which I can't afford. – peeyush tekriwal Apr 26 '19 at 11:56

0 Answers0