ESP-01 slow to receive server response

I am working with an ESP-01, uploading Arduino sketch directly to it with USB-serial adaptor. No problems uploading the code.

The code connects to WiFi, then connects to a server on my local network (also an ESP8266), sends a GET request, receives the response and goes into deep sleep until it is next reset.

The problem is that receiving the response from the server takes a long time, ~10 seconds, and I have no idea why. If I use my browser to send the same request, it takes < 0.5s, so it does not appear to be caused by the server being slow.

Here is the output. I am printing the value of millis() to illustrate the timing of each part of the code:

0: Connecting to WiFi.
506: WiFi connected, SSID: granary signal: -44 IP address: 192.168.1.220

506: connecting to server
582: connected, sending request
583: request sent, waiting for response
883: received --> HTTP/1.1 200 OK
3250: received --> Host: 192.168.1.113
3800: received --> Content-Type: text/html
4075: received --> 
4625: received --> <!DOCTYPE HTML>
5175: received --> <html>
5725: received --> Received!
6275: received --> </html>
11275: received --> 
11275: disconnected from server
11275: Disconnecting Wifi
11275: Sleeping...!

As you can see, there can be gaps of anything from 0.5s to 5s between the lines received from the server.

Here's the code:

extern "C" {
#include "user_interface.h"
}

#include <ESP8266WiFi.h>

#define LED 2

// Use WiFiClient class to create TCP connections
WiFiClient wsClient;

IPAddress ip(192,168,1,220);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
}

void loop() {

  unsigned long startTime = millis();
  
  Serial.println();
  Serial.print(millis() - startTime);
  Serial.print(": Connecting to WiFi");

  WiFi.mode(WIFI_STA); // Station Mode
  WiFi.hostname("Door Bell");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin("granary", "password");

  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000UL) {
    delay(500);
    digitalWrite(LED, !digitalRead(LED));
    Serial.print(".");
  }

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": WiFi connection failed");
    digitalWrite(LED, LOW);
  }
  else {
    digitalWrite(LED, HIGH);

    Serial.println();
    Serial.print(millis() - startTime);
    Serial.print(": WiFi connected, SSID: ");
    Serial.print(WiFi.SSID());
    Serial.print(" signal: ");
    Serial.print(WiFi.RSSI());
    Serial.print(" IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": connecting to server");

    if (!wsClient.connect("192.168.1.113", 80)) {
      Serial.print(millis() - startTime);
      Serial.println(": Host connection failed");
    }
    else {

      Serial.print(millis() - startTime);
      Serial.println(": connected, sending request");

      // This will send the request to the server
      wsClient.print("GET /ALARM=103 HTTP/1.1\rHost:192.168.1.113\rConnection: close\r\r");

      Serial.print(millis() - startTime);
      Serial.println(": request sent, waiting for response");
      
      // Read all the lines of the reply from server and print them to Serial
      while (wsClient.connected()) {

        String line = wsClient.readStringUntil('\r');
        Serial.print(millis() - startTime);
        Serial.print(": received --> ");
        Serial.println(line);
        wsClient.read(); // discard end-of-line character
      }

      Serial.print(millis() - startTime);
      Serial.println(": disconnected from server");
    }
    Serial.print(millis() - startTime);
    Serial.println(": Disconnecting Wifi");
    WiFi.disconnect();
  }

  Serial.print(millis() - startTime);
  Serial.println(": Sleeping...!");
  ESP.deepSleep(0);

}

Can anyone advise what I'm doing wrong that makes receiving the server response so slow?

Thanks

The library examples show '\r\n' in the URL request. You might be running into timeout issues if you aren't receiving \r\n. I'd try just doing a single char read while data is available and see if that is faster/different than reading Strings

When you go into deep sleep on the ESP, the only thing running is the clock. When the RST pin gets a pulse to wake, the ESP has to reboot, reconnect to the WiFi, then reconnect to your local server, then get your readings. In other words, the timing should be very close to the same if you unplug the power to the ESP then plug it back in. Because that is how deep sleep works.

blh64:
try just doing a single char read while data is available and see if that is faster/different than reading Strings

Thanks for the suggestion, I will experiment with that idea.

SteveMann:
... that is how deep sleep works.

Steve, I'm aware of all that. Take a look at the timings in my first post. The problem is not time time taken to wake up, connect to wifi or connect to the server. The timings show that all those things happen quite quickly. The problem is the time taken to read the response from the server.

OK, I see the gap you are talking about.

6275: received -->
11275: received -->

In 5-seconds you should be able to download a music file, but I am guessing that your data is much smaller.

Have you checked the signal strength at the ESP?

Serial.print("Signal strength is : ");
Serial.println(wsClient.RSSI());

any reason why not use

#include <ESP8266HTTPClient.h> ?

SteveMann:
OK, I see the gap you are talking about.
Have you checked the signal strength at the ESP?

There are several large gaps, that's just the biggest one.
Check the OP for signal strength.

KASSIMSAMJI:
any reason why not use

#include <ESP8266HTTPClient.h> ?

No reason, I will try it. What advantages does that library bring (other than it might fix my problem)?

I tried ESP8266HTTPClient with the following code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define LED 2

// Use WiFiClient class to create TCP connections
WiFiClient wsClient;

IPAddress ip(192, 168, 1, 220);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  wsClient.setTimeout(500);
}

void loop() {

  unsigned long startTime = millis();

  Serial.println();
  Serial.print(millis() - startTime);
  Serial.print(": Connecting to WiFi");

  WiFi.mode(WIFI_STA); // Station Mode
  WiFi.hostname("Door Bell");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin("granary", "password");

  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000UL) {
    delay(500);
    digitalWrite(LED, !digitalRead(LED));
    Serial.print(".");
  }

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": WiFi connection failed");
    digitalWrite(LED, LOW);
  }
  else {
    digitalWrite(LED, HIGH);

    Serial.println();
    Serial.print(millis() - startTime);
    Serial.print(": WiFi connected, SSID: ");
    Serial.print(WiFi.SSID());
    Serial.print(" signal: ");
    Serial.print(WiFi.RSSI());
    Serial.print(" IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": connecting to server");

    HTTPClient http;

    //if (!wsClient.connect("192.168.1.113", 80)) {
    if (!http.begin(wsClient, "http://192.168.1.113/ALARM=103")) {
      Serial.print(millis() - startTime);
      Serial.println(": Host connection failed");
    }
    else {

      Serial.print(millis() - startTime);
      Serial.println(": connected, sending request");

      // This will send the request to the server
      //wsClient.print("GET /ALARM=103 HTTP/1.1\r\nHost: 192.168.1.113\rConnection: close\r\n\r\n");
      if (http.GET() <= 0) {
        Serial.print(millis() - startTime);
        Serial.println(": server error");
      }
      else {

        Serial.print(millis() - startTime);
        Serial.println(": request sent, waiting for response");

        // Read all the lines of the reply from server and print them to Serial
        //      String line;
        //      while (wsClient.connected()) {
        //        line = wsClient.readStringUntil('\r');
        //        Serial.print(millis() - startTime);
        //        Serial.print(": received --> ");
        //        Serial.println(line);
        //        wsClient.read(); //discard end-of-line
        //        }

        String payload = http.getString();
        Serial.println(payload);

      }

      http.end();

      Serial.print(millis() - startTime);
      Serial.println(": disconnected from server");
      wsClient.stop();
    }
    Serial.print(millis() - startTime);
    Serial.println(": Disconnecting Wifi");
    WiFi.disconnect();
  }

  Serial.print(millis() - startTime);
  Serial.println(": Sleeping...!");
  ESP.deepSleep(0);

}

Unfortunately, results are simlar:

0: Connecting to WiFi.......
4575: WiFi connected, SSID: granary signal: -47 IP address: 192.168.1.220

4576: connecting to server
4576: connected, sending request
8155: request sent, waiting for response
<!DOCTYPE HTML>
<html>
Received!
</html>

10734: disconnected from server
10734: Disconnecting Wifi
10897: Sleeping...!

No, things are significantly quicker. Connecting to WiFi in the above example is consistently taking 4.5s, where before it was 0.5s. This is because of the WiFi.disconnect(). Without that, even after resetting from deep sleep, it takes < 0.25s to re-connect to the router.

So now the whole transaction is down to around 6.3s. Of that, 3.5s to connect to the server and 2.5s to receive the response.

It's much better, but I would still like to get it down to below 1s. And as mentioned earlier, it does not appear to be the server that is the source of the slow performance, because issuing the same request from browser on PC takes < 0.5s.

Any other ideas please?

Latest code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define LED 2

// Use WiFiClient class to create TCP connections
WiFiClient wsClient;

IPAddress ip(192, 168, 1, 220);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  //wsClient.setTimeout(500);
  WiFi.persistent(true);
}

void loop() {

  unsigned long startTime = millis();

  digitalWrite(LED, LOW); //Doorbell LED on
  
  Serial.println();
  Serial.print(millis() - startTime);
  Serial.print(": Connecting to WiFi");

  WiFi.mode(WIFI_STA); // Station Mode
  WiFi.hostname("Door Bell");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin("granary", "password");

  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000UL) {
    delay(25);
  }

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": WiFi connection failed");
  }
  else {


    Serial.println();
    Serial.print(millis() - startTime);
    Serial.print(": WiFi connected, SSID: ");
    Serial.print(WiFi.SSID());
    Serial.print(" signal: ");
    Serial.print(WiFi.RSSI());
    Serial.print(" IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println();
    Serial.print(millis() - startTime);
    Serial.println(": connecting to server");

    HTTPClient http;
    if (!http.begin(wsClient, "http://192.168.1.113/ALARM=103")) {
      Serial.print(millis() - startTime);
      Serial.println(": Host connection failed");
    }
    else {

      Serial.print(millis() - startTime);
      Serial.println(": connected, sending request");

      // This will send the request to the server
      if (http.GET() <= 0) {
        Serial.print(millis() - startTime);
        Serial.println(": server error");
      }
      else {
        Serial.print(millis() - startTime);
        Serial.println(": request sent, waiting for response");
        String payload = http.getString();
        Serial.print(millis() - startTime);
        Serial.println(": response received");
        Serial.println(payload);
      }
    }
    http.end();
  }
  digitalWrite(LED, HIGH); //Doorbell LED off
  Serial.print(millis() - startTime);
  Serial.println(": Sleeping...!");
  ESP.deepSleep(0);
}

Latest output:

0: Connecting to WiFi
181: WiFi connected, SSID: granary signal: -48 IP address: 192.168.1.220

181: connecting to server
182: connected, sending request
3750: request sent, waiting for response
6330: response received
<!DOCTYPE HTML>
<html>
Received!
</html>

6330: Sleeping...!