Problems with sleep timer

GumbyMan
Posts: 2
Joined: Sun Apr 05, 2020 7:37 pm

Problems with sleep timer

Postby GumbyMan » Sun Apr 05, 2020 7:45 pm

Hello,

i hope somebody can help me with that.
Currently i'm developing a soil moisture sensor with an ESP32 (MH-ET Live MiniKit).
The prorgamming is done in c++ using Platform.io in VsCode.

The ESP gets controlled by MQTT messages and so far everything is working find.
I'm sending the deep sleep time to the ESP using MQTT.
During the day, it gets put to sleep for 300s, during the night it should sleep for 3600s.
And exactly this is my problem. As far as I send the ESP to sleep for 3600s , it immediately wakes up.
My code is plain simple:

Code: Select all

#define uS_TO_S_FACTOR 1000000L  /* Conversion factor for micro seconds to seconds */
...
ret = esp_sleep_enable_timer_wakeup(3600* uS_TO_S_FACTOR);
in all cases (even with 3600s), the return value of ESP_SLEEP_ENABLE_TIMER is ESP_OK.

Does anybody have an idea what could be the reason?
There are no peripherals connected to the ESP so far, so it definitely isn't another wakeup trigger.

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: Problems with sleep timer

Postby ESP_Sprite » Mon Apr 06, 2020 8:00 am

I think your issue is that the sleep time you specify is larger than a 32-bit value. This is not an issue for esp_sleep_enable_timer_wakeup as it takes a 64-bit value as an argument, but you're not calculating a 64-bit argument as uS_TO_S_FACTOR is specified as a long which is only 32-bit. My conversion-fu isn't entirely up-to-date, but I'd think specifying it as '1000000LL' instead should fix it. (Also note that the compiler probably is warning you about the fact that this is happening.)

GumbyMan
Posts: 2
Joined: Sun Apr 05, 2020 7:37 pm

Re: Problems with sleep timer

Postby GumbyMan » Mon Apr 06, 2020 4:13 pm

:D
Thanks. It is working now.

I've realized previously that the timer function takes a 64bit value, and I've tried the following

Code: Select all

esp_sleep_enable_timer_wakeup(uint64_t(SleepTimeSeconds * uS_TO_S_FACTOR));
But the calculation itself ran into an overflow since i've still multiplied two 32bit values.

Thanks to your solution it is working now perfect.
For further reference, this is my solution now

Code: Select all

#define uS_TO_S_FACTOR 1000000LL  /* Conversion factor for micro seconds to seconds */
...
SleepTimeSeconds = GetSleepDelaySecondsOrDefault(); //returns the configured time from EEPROM
...
ret = esp_sleep_enable_timer_wakeup(uint64_t(SleepTimeSeconds) * uS_TO_S_FACTOR);

Who is online

Users browsing this forum: No registered users and 161 guests