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);
}
9
  • 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? 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.