Is it the right way to deep sleep with ESP32?

I want to put my ESP32 into deep sleep mode at a certain time. I’ve seen that with struct tm you can use the command tm_hour and you can control the time that’s past since midnight.

My idea was to make a millis() function in the void loop() similar to this:

if(millis() >= tm_hour + (22*3600000)){  // i want deep sleep to be at 10PM

I’m not so sure tho if it’s the right way to do so, maybe the if statement isn’t the right way? Also I’m not sure how to set up the wake up function, do i just put esp_sleep_enable_timer_wakeup() with the time i want it to sleep in the paratheses in the loop() function or in the setup()?

tm_hour is a member of the struct tm structure, not a standalone function / macro / variable definition in itself. So that’s syntactically incorrect.

Also millis() will give the amount of elapsed milliseconds since the start of the firmware, not the absolute real-world time in milliseconds.

So you have to slightly rethink your logic. First, you have to have a valid RTC time, so that has to be synchronized once (e.g. online via NTP). You can e.g. try and use time and functions (docs) to see if you can output the correct real world time.

You can then use localtime() to convert the output of time() into a struct tm, which has the desired tm_hours field. You can then check that if you have passed the 10PM mark, aka 22 in the tm_hours field (referenced from 0 to 23!), and then go to deepsleep.

Using localtime however must be done carefully, since it expect the correct timezone to be set. The device must know which timezone it is in, since when it’s 10pm in London happens it’s some earlier time in the USA etc (GMT minus something). So that must be respected as well.

1 Like

First of all, google is your friend. There are plenty of pages covering this. An “ESP32 arduino deep sleep” search on google gave this page as the first hit:

If you want to get the real world time (like 10pm), you need to manually keep track of it, since there is no “calendar” function in the builtin Real Time Clock (RTC) of the ESP32. If you want to accurately wake up at a specific date/time, then your best bet is to use an accurate, external RTC. They keep track of the real world date/time, once you set it correctly once (it’s also what’s in a normal computer/laptop). Then you can set an “alarm” on it to pull a pin high/low at a specific time, and use that as external wakeup signal for your ESP32.
If you just want to sleep for a duration (1 hour, 1 minute, etc), then you can just use the timer as wakeup source (sere link). No need for the time struct you mentioned.
Now if you really can’t use an external RTC, and you NEED the real time, a third option is to keep track of the date/time in software and calculate the sleep time by checking the current time and then calculating how long you need to sleep for so you wake up at the right time. But that is more complicated in software. There may be some libraries out there though, that do this for you. I haven’t checked.
You might even add NTP functionality to check the actual time on the internet and update your date/time that way.
In any case, realize that after wakeup, you sketch goes back to the “setup” function, it doesn’t stay in the loop function.