WIFIClient not completing quickly

if (!client.connect(host, httpPort))
{
return;
}

while(client.connected() || client.available()){
if (client.available()) {
response += client.readStringUntil('\r\n');
// this reads each payload line, but pauses for 50+seconds after Jason payload .
}
}

Output from .ino attached

millis after connect: 14153 : 14153 : 52
trying....GET /get/latest/dweet/for/myThing HTTP/1.1
Host: dweet.io
Connection: close

Receive Response:
millis in receive loop: 14194 : 41 : 93 : HTTP/1.1 200 OK

millis in receive loop: 14195 : 1 : 94 : Access-Control-Allow-Origin: *

millis in receive loop: 14195 : 0 : 94 : Content-Type: application/json

millis in receive loop: 14202 : 7 : 101 : Content-Length: 62

millis in receive loop: 14207 : 5 : 106 : Date: Mon, 28 Jan 2019 21:14:40 GMT

millis in receive loop: 14214 : 7 : 113 : Connection: keep-alive

millis in receive loop: 14220 : 6 : 119 :

millis in receive loop: 19224 : 5004 : 5123 : {"this":"failed","with":404,"because":"we couldn't
millis after loop: 73912 : 59692 : 59811

59 seconds to complete after receiving json content "{"this"...... . Ignore the "failed" text.

Any advice would be much appreciated.

help.ino (3.08 KB)

which library? some could take some time in connected(). replace if (client.available()) { with while (client.available()) {

readStringUntil takes a char as parameter, not two chars. it waits a second for the next character, if terminator is not in input. you loose seconds there.

Thanks for your help!

Libraries:

#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <Servo.h>
#include <ESP8266WiFi.h> // GitHub - esp8266/Arduino: ESP8266 core for Arduino
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // GitHub - tzapu/WiFiManager: ESP8266 WiFi Connection manager with web captive portal version 0.14.0

I found if I use client.available() to control things, I need to wait for data to start flowing before going into the while loop. This has improved performance 10x but still takes 5 seconds. I guess that's all i'm going to get out of this tiny WEMO d2.

//delay up to 2 seconds
for (x=0; x < 20; x++) {
if (client.available())
break;
delay(100);
}

while(client.available()){
response += client.readStringUntil('\n');
}

is there a \n in the data?

try to set client.setTimeout (100);

which version of esp8266 you use?

esp82266 library is esp82266 community 2.4.2

Including client.setTimeout(100); before the read loop has done the trick. It only take 1/10 of a seconds. Quite an improvement over 50+ seconds.

There are \n in the data, but I suspect there is not one at the very end.

Is there a way to read a block of data?

In any case, thanks for sharing your expertise.

Ken

KenLajoie:
esp82266 library is esp82266 community 2.4.2

Including client.setTimeout(100); before the read loop has done the trick. It only take 1/10 of a seconds. Quite an improvement over 50+ seconds.

There are \n in the data, but I suspect there is not one at the very end.

Is there a way to read a block of data?

while (client.connected()) {
  while (client.available()) {
      int l = client.read(buff, sizeof(buff) );
      Serial.write(buff, l);
  }
}

but it is better to know the size and end after the bytes are read, then wait until connected() is false

Is there another way to know the end of the data? eof? It seems my pause is caused by not finding the until character on the final chunk of data.

I'm curious how a browser does this.

KenLajoie:
Is there another way to know the end of the data? eof? It seems my pause is caused by not finding the until character on the final chunk of data.

I'm curious how a browser does this.

browser reads the content-length header or chunked encoding or timeout or tcp connection closed