preserve time-of-day during deep sleep

This commit is contained in:
cyberman54 2021-04-06 22:45:34 +02:00
parent 4d7d92900e
commit 8a8128ed85

View File

@ -15,6 +15,7 @@ RTC_NOINIT_ATTR uint32_t RTC_restarts;
// RTC_DATA_ATTR -> keep values after a wakeup from sleep // RTC_DATA_ATTR -> keep values after a wakeup from sleep
RTC_DATA_ATTR struct timeval RTC_sleep_start_time; RTC_DATA_ATTR struct timeval RTC_sleep_start_time;
RTC_DATA_ATTR unsigned long long RTC_millis = 0; RTC_DATA_ATTR unsigned long long RTC_millis = 0;
RTC_DATA_ATTR time_t RTC_time = 0;
timeval sleep_stop_time; timeval sleep_stop_time;
@ -65,8 +66,12 @@ void do_after_reset(void) {
sleep_time_ms = sleep_time_ms =
(sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 + (sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 +
(sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000; (sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000;
ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms);
RTC_millis += sleep_time_ms; // increment system monotonic time RTC_millis += sleep_time_ms; // increment system monotonic time
ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms);
// set time
setMyTime(RTC_time + sleep_time_ms / 1000, sleep_time_ms % 1000, _set);
// set wakeup state, not if we have pending OTA update // set wakeup state, not if we have pending OTA update
if (RTC_runmode == RUNMODE_SLEEP) if (RTC_runmode == RUNMODE_SLEEP)
RTC_runmode = RUNMODE_WAKEUP; RTC_runmode = RUNMODE_WAKEUP;
@ -184,6 +189,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) {
// time stamp sleep start time and save system monotonic time. Deep sleep. // time stamp sleep start time and save system monotonic time. Deep sleep.
gettimeofday(&RTC_sleep_start_time, NULL); gettimeofday(&RTC_sleep_start_time, NULL);
RTC_millis += millis(); RTC_millis += millis();
RTC_time = now();
ESP_LOGI(TAG, "Going to sleep, good bye."); ESP_LOGI(TAG, "Going to sleep, good bye.");
esp_deep_sleep_start(); esp_deep_sleep_start();
} }