So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By amadeuspzs
#87264 Hello,

I have an ESP12F with DHT22 sensor that publishes to MQTT over WiFi, then goes to deep sleep for 10 minutes, and tries again. I am using the Arduino IDE.

I've had really inconsistent behaviour where sometimes it works fine, sometimes it gets stuck in a WiFi blackhole: on boot it connects fine, but after deep sleep, it cannot find any networks at all.

I'm trying to minimise battery power so use

Code: Select all  WiFi.mode(WIFI_OFF);

at the start of the code, and when I know I need to use WiFi use:

Code: Select all    WiFi.forceSleepWake();
    // Bring up the WiFi connection

    // Static IP assignment
    WiFi.begin(wifi_ssid, wifi_password);

    int count = 0;
    while (WiFi.status() != WL_CONNECTED) {
      if (count > 50) { // 5 seconds
          if (debug) {
            Serial.println("WiFi connection timeout, dropping back into deep sleep");
            int n = WiFi.scanNetworks(false, true);
            Serial.println(" WiFi network(s) detected");
            String ssid;
            uint8_t encryptionType;
            int32_t RSSI;
            uint8_t* BSSID;
            int32_t channel;
            bool isHidden;
            for (int i = 0; i < n; i++)
              WiFi.getNetworkInfo(i, ssid, encryptionType, RSSI, BSSID, channel, isHidden);
              Serial.printf("%d: %s, Ch:%d (%ddBm) %s %s\n", i + 1, ssid.c_str(), channel, RSSI, encryptionType == ENC_TYPE_NONE ? "open" : "", isHidden ? "hidden" : "");
          ESP.deepSleep(polling_us, WAKE_RF_DISABLED ); // in uS
      if (debug) Serial.print(".");

This is the output with serial debugging:

Code: Select all13:50:36.068 -> Connecting to SSID
13:50:36.172 -> fpm close 3
13:50:36.172 -> mode : sta(dc:4f:22:1d:a3:24)
13:50:36.172 -> add if0
13:50:36.279 -> ............................scandone
13:50:39.026 -> no SSID found, reconnect after 1s
13:50:39.093 -> .reconnect
13:50:39.199 -> ......................WiFi connection timeout, dropping back into deep sleep
13:50:41.293 -> 6
13:50:41.293 -> scandone
13:50:43.490 -> scandone
13:50:43.490 -> 0 WiFi network(s) detected
13:50:43.490 -> del if0
13:50:43.490 -> usl
13:50:43.557 -> enter deep sleep

The schematic is: Image (I get the same error powering off USB so it's not a current draw issue from the MCP1700).

The full code is here.

Any help appreciated - I've been trying different things over the last couple of weeks and can't seem to shake this - hopefully something newbie-related that I've missed.

Last edited by amadeuspzs on Wed May 27, 2020 7:43 am, edited 1 time in total.
User avatar
By schufti
#87291 I am not 100% sure about the advances in new cores but when I last tried things like that it was more complicated to restart WiFi after ESP.deepSleep(x, WAKE_RF_DISABLED).
WAKE_RF_DISABLED is way more "bare metal" than modem_sleep (what is related to force_sleep_wake).

you will probably need sth like
Code: Select all#include <user_interface.h>

void WiFiOn() {
to enable WiFi after your ESP.deepSleep(x, WAKE_RF_DISABLED)
User avatar
By amadeuspzs
#87295 Thanks schufti that is good to know about WAKE_RF_DISABLED being more intense.

I've dug out folks who've had the same issue and it seems the official solution is to run:

Code: Select allESP.deepSleep(1, WAKE_RF_DEFAULT)

To reboot with WiFi enableable, then transmit your packets.

It adds the complication of having to track extra state variables in RTC, but it does appear to be working across three different ESP8266 modules.

Edit: here is the updated flowchart to implement this workaround: Image