Why does my ESP32 never wake from deep sleep?

28 Views Asked by At

My deep sleep call never restarts the ESP32 and I don't know why. This is my code:

#define uS_TO_S_FACTOR 1000000ULL
void deepSleep(long seconds) {
    loggerMain->info("entering deep sleep for: %l seconds",seconds);
#ifndef NO_DEEP_SLEEP
    esp_deep_sleep(seconds * uS_TO_mS_FACTOR);
#endif
    // we stop here 
}

I call this with 5 seconds and wait... and it hasn't come back after 10 minutes. What I expected was it would sleep for 5 seconds and then restart (ie run setup() and then start running loop()). But it just waits as if I have specified a very, very long time, not 5 seconds. I've looked at lots of examples and the code seems to be right, though clearly I am doing something wrong. Can anyone spot the problem? I have tried using esp_sleep_enable_timer_wakeup() with esp_sleep_start() and it made no difference. To be clearer I call the function above from loop() sometimes, usually after I get an SMS telling me how long to sleep for.

This is using Arduino IDE 1.8.19 and an ESP32S3, specifically a Lilygo T-CAM with an attached SIM7000E modem. I'm hoping the rest of the stuff isn't relevant and I have just misunderstood how to call deep sleep.

1

There are 1 best solutions below

0
RogerParkinson On

It seems to be something about the missing Serial.flush(). I revised the code to this:

#define uS_TO_S_FACTOR 1000000ULL
void deepSleep(long seconds) {
  loggerMain->info("entering deep sleep for: %l seconds",seconds);
#ifndef NO_DEEP_SLEEP
  //esp_deep_sleep(seconds * uS_TO_mS_FACTOR);
  esp_sleep_enable_timer_wakeup(10 * uS_TO_S_FACTOR);
  SerialMon.flush();
  esp_deep_sleep_start();
#endif
  // we stop here 
}

and it worked immediately. Problem solved.