Deep sleep advice please

If doesn't fit into any other category ....
Post Reply
Zim
Posts: 291
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 267 times
Been thanked: 131 times

Deep sleep advice please

Post by Zim »

I need to have a ESP32 deep sleep and wake up every 24 hours or so, run a script, then go back to sleep and repeat. What would be the easiest way to do this?
Thanks
Zim
User avatar
Electroguard
Posts: 889
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 286 times
Been thanked: 329 times

Re: Deep sleep advice please

Post by Electroguard »

I will be giving the Adafruit TPL5110 Power Timer Breakout a try when I need a low power sensor project.
https://learn.adafruit.com/adafruit-tpl ... breakout/
By default it can be set to 'wake' an esp device at up to 2 hour intervals set by a trim pot, but it mentions cutting the track in series with the pot to insert a different timing resistor.
And connecting the Delay pin to VDD (eg: by sensor contacts) triggers immediate wake up.
AndyGadget
Posts: 222
Joined: Mon Feb 15, 2021 1:44 pm
Has thanked: 126 times
Been thanked: 132 times

Re: Deep sleep advice please

Post by AndyGadget »

I'm assuming you have a wifi connection here and this is a battery powered device so you want to minimise power consumption?

This routine allows for the clock running the SLEEP timer to be inaccurate - I've seen figures of plus or minus 5%.
It will sleep for the required time minus the possible error and then wake to re-check the actual time and re-calculate the sleep time remaining.

If you're using sleep I recommend adding a button to allow you to terminate the program as once you've set the autorun it's possible to get a situation where the ESP32 has woken and gone back to sleep before you've had a chance to break in. The 8 second delay at startup is for debugging too.

Code: [Local Link Removed for Guests]

' With Fast Boot disabled, ESP32 will have a valid wifi connection at this point.
wlog chr$(10) + "Awake at " + time$

NEO.SETUP 16
neo.strip 0,11,0,0,0

pin.mode 4, input, pullup
if pin(4) = 0 then end     ' Exit program if button pressed at reset

pause 8000     ' Leave until debugging complete!

T$ = Time$
ActHour = val(left$(T$,2))
ActMin = val(mid$(T$,4,2))
ActSec = val(right$(T$,2))

AlarmHour = 12     ' Desired alarm hour
AlarmMin = 42      ' Desierd alarm minute

HoursToAlarm = (AlarmHour - ActHour)
MinsToAlarm = ((AlarmMin - ActMin)) + (HoursToAlarm * 60)

SecsToAlarm = MinsToAlarm * 60
if SecsToAlarm < 0  then SecsToAlarm = SecsToAlarm + 86400 ' Alarm is next day (24 hour wraparound)

if SecsToalarm > 600 then
  SleepTime = SecsToAlarm * 0.95   ' Allowing for max of 5% clock error.
else
  Sleeptime = SecsToAlarm - ActSec ' For short sleeps, set to beginning of minute so alarm not missed
end if

wlog HoursToAlarm, MinsToAlarm, SecsToAlarm, SleepTime

if (ActHour = AlarmHour) and (ActMin = AlarmMin) then gosub DoStuffHere ' Alarm time matches actual time.

wlog "Going to sleep at " + time$

wifi.sleep            ' This made a difference with ESP3266 sleep current.  Maybe with ESP32 too
pause 20
OPTION.CPUFREQ 80     ' This made a difference with ESP3266 sleep current.  Maybe with ESP32 too

sleep SleepTime     ' Sleep for desired time, or until button pressed
end                        ' Not really needed.

DoStuffHere:
' I just happened to have a NEOLED ring connected :¬)
wlog "WOOP WOOP - ALARM TIME!"
neo.strip 0,11,0,255,0
pause 5000
neo.strip 0,11,0,0,0
pause 50                            ' Allow strip to reset
sleep 60                            ' Prevent retrigger in same minute
return
Zim
Posts: 291
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 267 times
Been thanked: 131 times

Re: Deep sleep advice please

Post by Zim »

Thanks for the input, Gents.
Post Reply