I have a question about ESP waking up from the deep sleep mode. I made a temperature logger (using DS1621 hires mode) where ESP8266 connects to the wifi and uploads the data (20 seconds between uploads) to ThingSpeak service and that worked well but then I added deep sleep mode because I am powering the module (LoLin v3) with 18650 battery + 5V step-up (the current when wifi is on is about 120 mA).
What I noticed after adding the code to enable deep sleep was the ESP8266 never uploaded more than 3 results. That is - the module sometimes uploaded just one result, sometimes 2, sometimes 3 and sometimes it didn't upload anything and debugging showed WL_CONNECTED always returned false. After some experimenting I found everything works OK if I add:
WiFi.forceSleepWake();
before
WiFi.begin();
so I would like to know why is that needed. Here is the stripped code:
#include <ESP8266WiFi.h>
const char* ssid = "";
const char* password = "";
IPAddress ip(192,168,1,xx); // desired IP address
IPAddress gateway(192,168,1,yy); // IP address of my PC acting as a router (ADSL adapter is in bridge mode, no DHCP, PC establishes the connection through PPPoE (dial-up))
IPAddress subnet(255,255,255,0);
WiFiClient client;
void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); // turn-off the built-in LED
Serial.begin(74880);
delay(100);
Serial.println();
Serial.println("Start...");
// read some sensors
// ...
WiFi.persistent(false); // <-- prevents flash wearing?
WiFi.forceSleepWake(); // <-- WITHOUT THIS ESP CONNECTS ONLY AFTER FIRST FEW RESTARTS OR DOESN'T CONNECT AT ALL
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
digitalWrite(2, LOW); // blink built-in LED
delay(50);
digitalWrite(2, HIGH);
// upload the data
// ...
digitalWrite(2, LOW); // blink built-in LED
delay(50);
digitalWrite(2, HIGH);
// GPIO16 (D0) connected to RST
ESP.deepSleep(16000000);
//delay(100); // no need
}
void loop() {
}
I am aware I did not provide the mode parameter to:
ESP.deepSleep();
but since wifi connection sometimes does work even after wake-up - that means wifi is not disabled by that command.
As I said, after adding WiFi.forceSleepWake() everything works fine but I would really like to know what is going on since I cannot see the reason why would I have to use that method after deep sleep.