With my ESP8266, I need to make a simple GET request to a server, and then go to deep sleep (the goal is 1 year battery powered)... until a signal comes on RST
and then it starts again.
The following code works, but it takes 6 to 10 seconds on each RST
to get connected to my home WiFi using "WPA2 Personal" (when it stops blinking in my code).
Is this 6 to 10 seconds delay normal, do you have the same order of magnitude? Or can we go down to 1 second, after a wake-up from deep sleep?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
HTTPClient http;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
if (WiFi.SSID() != WIFI_SSID) { // don't do begin if not necessary, see tutorial link after
WiFi.begin("MySSID", "MyPassword");
WiFi.persistent(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
}
while(WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW); delay(10); digitalWrite(LED_BUILTIN, HIGH); delay(200); // blinking
}
http.begin(client, "http://example.com/request.php");
http.GET();
http.end();
digitalWrite(LED_BUILTIN, LOW); delay(500); digitalWrite(LED_BUILTIN, HIGH); delay(500);
ESP.deepSleep(0);
}
void loop() {
}
Note: I have read and carefully respected this useful information about low power / battery-powered ESP8266:
Power Saving tips for the ESP8266 (especially this paragraph):
Do not call WiFi.begin() in setup().
The ESP8266 chip saves the last known wifi settings. Calling WiFi.begin() wipes those out. By calling WiFi.begin() only when it was needed, I shaved more than 2 seconds from the average amount of time it takes to associate with the AP. WiFi.begin() should be called only if the saved SSID does not match the configured SSID. You can check the configured SSID with the WiFi.SSID() call. Also, it is good practice to clear the saved WiFi settings if the timer expires. (See next section "Use watchdog timers")