-->
Page 1 of 4

Why must I use WiFi.forceSleepWake() after deep sleep?

PostPosted: Tue Nov 29, 2016 11:10 pm
by chupo_cro
Hi all, my very first post here.

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:

Code: Select allWiFi.forceSleepWake();

before

Code: Select allWiFi.begin();

so I would like to know why is that needed. Here is the stripped code:

Code: Select all#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:

Code: Select allESP.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.

Re: Why must I use WiFi.forceSleepWake() after deep sleep?

PostPosted: Wed Dec 14, 2016 5:56 pm
by shred
I guess you've solved or worked around this problem by now, but just in case...

I wonder if you're hitting this known bug?
https://github.com/esp8266/Arduino/issues/2186

Try changing your ESP8266 "board" from v2.3.0 to v2.2.0 and see if the problems querying WL_CONNECT go away. Going back to v2.2.0 fixed a lot of weirdness for me.

Re: Why must I use WiFi.forceSleepWake() after deep sleep?

PostPosted: Fri Dec 16, 2016 5:48 am
by chupo_cro
shred wrote:I guess you've solved or worked around this problem by now, but just in case...

I wonder if you're hitting this known bug?
https://github.com/esp8266/Arduino/issues/2186

Try changing your ESP8266 "board" from v2.3.0 to v2.2.0 and see if the problems querying WL_CONNECT go away. Going back to v2.2.0 fixed a lot of weirdness for me.

Well, I 'solved' it by inserting:

Code: Select allWiFi.forceSleepWake();

but I would like to know why this resolves the problem.

Without that, one of these scenarios happens:
#1
Code: Select all(boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), (boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), (boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), boot --> no connection

#2
Code: Select all(boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), (boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), boot --> no connection

#3
Code: Select all(boot --> connect --> measure --> upload --> deep sleep --> ... --> reset), boot --> no connection

#4
Code: Select allboot --> no connection

Bug #2186 is: 'WiFi.begin doesn't connect if connection was already established', but how a connection could already be established upon reset when I am using:

Code: Select allWiFi.persistent(false);

?

There isn't v2.3.0 or v.2.2.0 on the list. I selected NodeMCU 1.0 (ESP-12E module) board.

Thank you for the reply!
Regards

Re: Why must I use WiFi.forceSleepWake() after deep sleep?

PostPosted: Tue Dec 20, 2016 8:29 am
by jumpjack
I don't catch why you upload data in Setup() rather than in Loop() , but maybe it's because I don't know how "sleep mode" works.
Is there any tutorial, or a ready-made library, to build a sleep-enabled logger?