Maximum timeout period for uasyncio.sleep()?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
VirtualWolf
Posts: 3
Joined: Tue Aug 02, 2022 9:57 am

Maximum timeout period for uasyncio.sleep()?

Post by VirtualWolf » Tue Aug 02, 2022 10:11 am

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!

VirtualWolf
Posts: 3
Joined: Tue Aug 02, 2022 9:57 am

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

Post by VirtualWolf » Sat Aug 13, 2022 4:33 am

::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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Sat Aug 13, 2022 8:37 am

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.
Peter Hinch
Index to my micropython libraries.

VirtualWolf
Posts: 3
Joined: Tue Aug 02, 2022 9:57 am

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

Post by VirtualWolf » Sun Dec 18, 2022 5:54 am

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.

Jackli
Posts: 80
Joined: Thu Apr 29, 2021 9:11 am

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

Post by Jackli » Thu May 25, 2023 8:10 am

uasyncio.sleep(t).Sleep for t seconds (can be floating point)
This is a collaborative program

Post Reply