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