Skip to content

Commit 419b53f

Browse files
reiyaweaG6EJD
authored andcommitted
patch calculation of sleep duration
ESP32 will wake up too early if its RTC runs too fast, which causes extra wake-ups. For example, if it wakes up at 12:29:20, the sleep duration will be 40 seconds and ESP32 will wake up again soon, which results in extra battery consumption. With this patch, waking up at 12:29:20 will result in a sleep duration of 1800+40 seconds. This ensures only one wake-up every 30 minutes.
1 parent 627cf3e commit 419b53f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

examples/Waveshare_4_2/Waveshare_4_2.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ void loop() { // this will never run!
136136
//#########################################################################################
137137
void BeginSleep() {
138138
display.powerOff();
139-
long SleepTimer = (SleepDuration * 60 - ((CurrentMin % SleepDuration) * 60 + CurrentSec)); //Some ESP32 are too fast to maintain accurate time
140-
esp_sleep_enable_timer_wakeup((SleepTimer+20) * 1000000LL); // Added +20 seconnds to cover ESP32 RTC timer source inaccuracies
139+
long SleepTimer = SleepDuration * 60; // theoretical sleep duration
140+
long offset = (CurrentMin % SleepDuration) * 60 + CurrentSec; // number of seconds elapsed after last theoretical wake-up time point
141+
if (offset > SleepDuration/2 * 60){ // waking up too early will cause <offset> too large
142+
offset -= SleepDuration * 60; // then we should make it negative, so as to extend this coming sleep duration
143+
}
144+
esp_sleep_enable_timer_wakeup((SleepTimer - offset) * 1000000LL); // do compensation to cover ESP32 RTC timer source inaccuracies
141145
#ifdef BUILTIN_LED
142146
pinMode(BUILTIN_LED, INPUT); // If it's On, turn it off and some boards use GPIO-5 for SPI-SS, which remains low after screen use
143147
digitalWrite(BUILTIN_LED, HIGH);

0 commit comments

Comments
 (0)