I'm trying to program an ESP8266 device to be able to take commands over HTTP with ESP8266WebServer but no matter what I do any request takes 2 seconds to complete.
Upon further investigation I noticed there are the following headers in the response:
Connection: keep-alive
Keep-Alive: timeout=2000
I tried adding a Connection: close header manually but it just ended up adding both and still taking 2s to complete the request.
I also tried looking at the source code and I found HTTP_MAX_CLOSE_WAIT but if I try to define it in my code I get the following warning when compiling, and the request still takes 2s
warning: "HTTP_MAX_CLOSE_WAIT" redefined
1 | #define HTTP_MAX_CLOSE_WAIT 0
I also tried manually closing the connection after returning the response, which also did not help and had a side effect of the server stopping to respond for some reason.
http.send(200, "text/plain", "test");
http.close();
Additionally, I'm having the same issue when I try to send an HTTP request from the ESP to another server, each request takes 2s.
I had started a PHP dev server php -S <addr>, and added some simple code to send an HTTP request to it every time a pin changes:
void setup() {
pinMode(D4, INPUT_PULLUP);
}
int oldv;
void loop() {
int val = digitalRead(D4);
if (val != oldv) {
oldv = val;
request("<ip>", 4444, "/test", "");
}
}
void request(String host, int port, String uri, String body, bool https = false) {
WiFiClient client;
HTTPClient http;
http.setReuse(false);
http.begin(client, host, port, uri, https);
http.POST(body);
http.end();
}
But no matter how fast I trigger the pin, requests come in only once per 2s:
[Mon Jan 1 17:19:16 2024] 192.168.1.1:53935 Accepted
[Mon Jan 1 17:19:16 2024] 192.168.1.1:53935 [404]: (null) /test - No such file or directory
[Mon Jan 1 17:19:16 2024] 192.168.1.1:53935 Closing
[Mon Jan 1 17:19:18 2024] 192.168.1.1:64024 Accepted
[Mon Jan 1 17:19:18 2024] 192.168.1.1:64024 [404]: (null) /test - No such file or directory
[Mon Jan 1 17:19:18 2024] 192.168.1.1:64024 Closing
[Mon Jan 1 17:19:20 2024] 192.168.1.1:61577 Accepted
[Mon Jan 1 17:19:20 2024] 192.168.1.1:61577 [404]: (null) /test - No such file or directory
[Mon Jan 1 17:19:20 2024] 192.168.1.1:61577 Closing
[Mon Jan 1 17:19:22 2024] 192.168.1.1:65345 Accepted
[Mon Jan 1 17:19:22 2024] 192.168.1.1:65345 [404]: (null) /test - No such file or directory
[Mon Jan 1 17:19:22 2024] 192.168.1.1:65345 Closing
How can I speed things up?