Esp8266 web server random delay between server finish and client

Using a D1 mini.

I'm trying to figure out why there is a random wait between my request finishing, and my client.

Here is my example sketch:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

void handleTest() {
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  server.send(200, "text/html", "hello");
  digitalWrite(LED_BUILTIN, HIGH);
}

void setup() 
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  Serial.begin(115200);


  WiFi.mode(WIFI_STA);
  WiFi.begin("xxx", "xxxxxxxx");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  server.on("/test", HTTP_GET, handleTest);
  server.keepAlive(false);
  server.begin();
}
 
void loop() {
  server.handleClient();
}

And I am testing response time using curl:

$ time curl '192.168.1.220/test'
hello
real	0m3.197s
user	0m0.015s
sys	0m0.009s
$ time curl '192.168.1.220/test'
hello
real	0m3.261s
user	0m0.008s
sys	0m0.017s
$ time curl '192.168.1.220/test'
hello
real	0m1.422s
user	0m0.013s
sys	0m0.010s
$ time curl '192.168.1.220/test'
hello
real	0m1.039s
user	0m0.010s
sys	0m0.013s
$ time curl '192.168.1.220/test'
hello
real	0m1.035s
user	0m0.017s
sys	0m0.009s
$ time curl '192.168.1.220/test'
hello
real	0m2.895s
user	0m0.021s
sys	0m0.004s

I can often see the led turn off on my d1 mini well before curl gets the response.

I am assuming this has something to do with headers, but I can't quite crack it.

$ time curl -v '192.168.1.220/test'
*   Trying 192.168.1.220:80...
* TCP_NODELAY set
* Connected to 192.168.1.220 (192.168.1.220) port 80 (#0)
> GET /test HTTP/1.1
> Host: 192.168.1.220
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 5
< Connection: keep-alive
< Keep-Alive: timeout=2000
< 
* Connection #0 to host 192.168.1.220 left intact
hello
real	0m1.460s
user	0m0.013s
sys	0m0.014s

I've tried setting
server.setContentLength(),
server.keepAlive(false),

and trying to manually start/stop the server after every connection
server.stop();
server.begin();

All have no effect on the response time.

Any ideas?

Thanks

try server.send(200, "text/plain", "hello");

I can't think of a reason why @Juraj 's suggestion would make any difference, but I'm not sure what will either.

Maybe try

void handleTest() {
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  server.send(200, "text/html", "hello");
  server.flush();
  digitalWrite(LED_BUILTIN, HIGH);
}

Thanks for the suggestions.

I tried text/html, and there are still significant occasional delays
server.flush() isn't a member of the ESP8266WebServer class. I tried to see if there is anything similar at Arduino/ESP8266WebServer.h at master · esp8266/Arduino · GitHub, but there doesn't seem to be. my manually calling stop on the server was an attempt at this.

Appreciate the help.

found out how to flush:

  ESP8266WebServer::ClientType client = server.client();
  server.send(200, "application/json", "hello", 5);
  client.flush();

but it still doesn't help...

why do you keep setting content types not matching the content?

I did try text/html as suggested as well. eventually my functions will return json

the suggestion was text/plain

my mistake, i did try text/plain as well. It turns out this must be a problem with my test machine. I tried running curl on a different box and it all seems to work fine.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.