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