ESP32 millis resetting after deep sleep

EdHayes3
Posts: 5
Joined: Sun Feb 21, 2021 1:30 am

ESP32 millis resetting after deep sleep

Postby EdHayes3 » Sun Feb 21, 2021 1:37 am

I have been working with an ESP32 development board for a day now. I had code that was working with the deep sleep and millis(), but now it seems to have stopped working. The millis() timer appears to be resetting when it comes out of sleep.

I made a simple sketch to show the problem:

Code: Select all

//Sleep
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
RTC_DATA_ATTR int bootCount = 1;
#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */

void setup() {
  Serial.begin(115200);
  Serial.println("\n");
  Serial.println("Boot number: " + String(bootCount));
  print_wakeup_reason();
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  ++bootCount;
}

void loop() {
  Serial.print("millis: ");
  Serial.println(millis());
  goToSleep();
}

void goToSleep() {
  Serial.println("Going to sleep now");
  Serial.println("Will wake again in " + String(TIME_TO_SLEEP) +  " Seconds");
  esp_deep_sleep_start();
}

void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;
  wakeup_reason = esp_sleep_get_wakeup_cause();
  switch (wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
  }
}

Serial Output:

Code: Select all

Boot number: 1
Wakeup was not caused by deep sleep: 0
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 2
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 3
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 4
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 1
Wakeup was not caused by deep sleep: 0
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 2
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 3
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Boot number: 4
Wakeup caused by timer
millis: 33
Going to sleep now
Will wake again in 5 Seconds
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8

felmue
Posts: 70
Joined: Mon Nov 16, 2020 2:55 pm

Re: ESP32 millis resetting after deep sleep

Postby felmue » Mon Feb 22, 2021 2:10 pm

Hello @EdHayes3

I am not 100% sure, but I think, since the ESP32 actually does a restart after deep sleep, millis() are not supposed to be persistent but rather start from zero every time ESP32 restarts. I found this article which might help you: https://www.robmiles.com/journal/2020/1 ... deep-sleep

Thanks
Felix

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 millis resetting after deep sleep

Postby lbernstone » Mon Feb 22, 2021 2:50 pm

millis is based on the cpu timer, which stops with the cpu. If you keep the rtc running during deep sleep, you can use rtc_time_get (or just time(NULL)).
https://github.com/lbernstone/NTP_sleep

EdHayes3
Posts: 5
Joined: Sun Feb 21, 2021 1:30 am

Re: ESP32 millis resetting after deep sleep

Postby EdHayes3 » Wed Feb 24, 2021 7:50 pm

Interesting. I swear the millis was not restarting when I first started coding. But now it is restarting. Very odd.

I ended up just making a bootCount and bootCountRefresh stored in RTC memory, where both will increment every boot, and then after bootCountRefresh reaches 5, it will reset to 0. System will wake up every minute to check a few things, and then using wifi will refresh content on the 5th boot. Actually seems cleaner than using the millis, and seems to solve my problem. So crisis averted.

ArcHeRRed
Posts: 7
Joined: Tue Feb 23, 2021 4:49 pm

Re: ESP32 millis resetting after deep sleep

Postby ArcHeRRed » Mon Mar 15, 2021 4:39 pm

You try Flash ESP32, follow the link below.

https://eleceasy.com/t/esp32-watchdog/5382

Who is online

Users browsing this forum: No registered users and 96 guests