Page 1 of 1

Maximum timeout period for uasyncio.sleep()?

Posted: Tue Aug 02, 2022 10:11 am
by VirtualWolf
Hi everyone!

I have several ESP32s that run Peter Hinch's excellent mqtt_as.py library and are connected to temperature sensors. I've set up WDT to reboot them if they get into an odd state, but just occasionally, maybe once every month or two, they manage to get into a state where they no longer send updates but the WDT doesn't reboot them either so I end up having to go out and physically press the reset button on them. Rather annoying.

Instead of attempting to figure out something that happens so rarely, I figured I'd just schedule a machine.reset() every week. I'm testing this on my spare FeatherS2 (the other devices that are in active use are Adafruit's HUZZAH32s) and the code in question at the bottom of main.py looks like this:

Code: Select all

[...snipped...]

client = MQTTClient(config)

async def weekly_reboot():
    while True:
        await uasyncio.sleep(604800) # 1 week in seconds
        logger.log('Weekly reboot!')
        reset()

try:
    uasyncio.create_task(weekly_reboot())

    uasyncio.run(main(client))
    uasyncio.run(weekly_reboot())
finally:  # Prevent LmacRxBlk:1 errors.
    client.close()
    uasyncio.new_event_loop()
If I run that with the sleep period set to 604,800, the device actually almost constantly reboots. If I drop it down to a period of 86400 seconds (one day) — or even lower, like 30 seconds just for testing purposes — it works fine and reboots at the period I've specified.

Am I doing something silly, or missing something, or is there a maximum limit that uasyncio.sleep() will take?

Many thanks!

Re: Maximum timeout period for uasyncio.sleep()?

Posted: Sat Aug 13, 2022 4:33 am
by VirtualWolf
::facepalm::

I realised that rather than screwing around with this, I can just do:

Code: Select all

wdt = WDT(timeout=604800000)
And never WDT.feed() it, and that'll kick the watchdog timer to restart it after a week.

Re: Maximum timeout period for uasyncio.sleep()?

Posted: Sat Aug 13, 2022 8:37 am
by pythoncoder
The code for uasyncio.sleep multiplies by 1000 and calls uasyncio.sleep_ms, with uasyncio using ticks_add and ticks_diff for timing. These will wrap round, with the exact modulo dependent on platform but typically 149 hours (less than a week). The answer is simply to await (say) a day in a loop.

In general timed events can be scheduled with this library.

Re: Maximum timeout period for uasyncio.sleep()?

Posted: Sun Dec 18, 2022 5:54 am
by VirtualWolf
Ahh, thanks Peter!

It turns out I had another issue anyway, I'd subsequently discovered that you can only have one watchdog timer on the ESP32 and because I was already using one to restart the board in case my temperature/humidity sensor readings crapped out, the longer period of this restart never triggered regardless.

Re: Maximum timeout period for uasyncio.sleep()?

Posted: Thu May 25, 2023 8:10 am
by Jackli
uasyncio.sleep(t).Sleep for t seconds (can be floating point)
This is a collaborative program