ESP8266 HTTP Response Time, timeout

Hi,

I'm trying to read out some data from heater, which share data on it's own webserver. I'm using example program HTTP basic client, from ESP8266 library. It's reading fine, but there is delay 5 second between reading. Delay is caused by String payload = http.getString(); function. I suspect there is something timing out. Any ideas? Thanks

ESP8266 NodeMcu V3 (china)

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

 

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("Brozapetr", "");

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    WiFiClient client;

    HTTPClient http;

    if (http.begin(client, "http://192.168.1.3/ext/daqdata.cgi?key=asdfas595ds")) {  // HTTP

      int httpCode = http.GET();

      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          [b]String payload = http.getString();[/b]
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }

//  delay(10000);
}

Content of web page (plain text):

["PLNE ZATIZENI",69.86,5.12,56,70.93,68.12,32.69,99,9.80,1,true,120.00,0,-20.00,0,-20.00,0,true,60.00,24.68,36.33,true,7.91,31.92,true,true,-9.00,-9.00,-20.00,true,-9.00,70.00,true,true,-9.00,-9.00,-20.00,true,-9.00,70.00,true," ",120.00,"NORMAL","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO","VYPNUTO",1181724,2466,344,120.00,-20.00,-20.00,true,true,true,70.00,70.00,70.00,-20.00,-20.00,-20.00,-20.00,-20.00,-20.00,true,true,true]

Does the server send a content length header?

Is this the right command to get header length?

http.contentLength();

or should I use

http.collectHeaders();

Thanks

Do a request manually (command line):

telnet 192.168.1.3 80

then

GET /ext/daqdata.cgi?key=asdfas595ds HTTP/1.0

End that line by hitting the return key twice. Post the result you get!

Here is the result

telnet.JPG

telnet.JPG

As I suspected the server doesn't provide a content-length header, so the ESP8266 code doesn't know when the end of the transfer is reached. So it waits for the timeout.
The easiest way to solve your problem is to teach the server to send a correct content-length header and your delay will probably disappear immediately.
The other way, to change the ESP8266 core library code to check for a closed connection without waiting for the timeout is much more work and I doubt that you're able to do it.

Thanks for advice.

As this is 3rd party device, I can't change behavior of that webserver. What I read online, it's good practice to add content-length to header, right?

Page content (size) is always the same, just values are changing, could I specify content-length manually from Arduino?

Page content (size) is always the same, just values are changing, could I specify content-length manually from Arduino?

No, at least not easily. But you can just not use the http.getString() call and use low level calls instead, like this code:

uint16_t size = 64; // replaces content-length value
uint8_t * buff = (uint8_t *) malloc(size);
int bytesRead = http.getStream().eadBytes(buff, size);

Alright it's little too complex for me. I can't see, where incoming data are being stored, as bytesRead is just integer.

I think you got typo in code:

int bytesRead = http.getStream().readBytes(buff, size);

I was able to parse the stream with help of this tutorial: https://www.instructables.com/id/ESP8266-Parsing-JSON/

just instead of
JsonObject& root = jsonBuffer.parseObject(http.getString());
use:
JsonObject& root = jsonBuffer.parseObject(http.getStream());