WDT with asyncio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
vigorem
Posts: 2
Joined: Wed Aug 28, 2019 2:45 pm

WDT with asyncio

Post by vigorem » Wed Aug 28, 2019 2:59 pm

I'm trying to implement a wdt using @peterhinch' excellent micropython-mqtt

He kindly indicated me a way to do so (on the git issue board) with the following:

Code: Select all


import uasyncio as asyncio
from machine import WDT

async def do_wdt():
    tim = 300000
    wdt = WDT(timeout=tim)
    while True:
        wdt.feed()
        await asyncio.sleep(tim // 10)  # Feed 10 times in timeout period (very conservative)

...
loop = asyncio.get_event_loop()
loop.create_task(do_wdt())

Unfortunatly I get an error "TypeError: function doesn't take keyword arguments" which I suspect comes from the

Code: Select all

WDT(timeout=tim)
line.

How could I properly handle this WDT coroutine?

Many thanks,

Sébastien

vigorem
Posts: 2
Joined: Wed Aug 28, 2019 2:45 pm

Re: WDT with asyncio

Post by vigorem » Wed Aug 28, 2019 3:38 pm

My shame, looks the issue is related to ESP8266 boards, used a custom WDT class found on the forum and looks good so far :

viewtopic.php?t=6376#p36275

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

Re: WDT with asyncio

Post by pythoncoder » Thu Aug 29, 2019 7:45 am

A quick experiment suggests that the ESP8266 WDT does not accept an argument. It seems to have a timeout of about 2 seconds. If it's a hardware WDT, so long as you can feed it frequently enough, it may be better than the software WDT.

If you want to try it I suggest creating a coroutine which instantiates it then loops forever feeding it (say) every 500ms. Start the coroutine when your code has connected to the server.

Code: Select all

import uasyncio as asyncio

Code: Select all

async def do_wdt():
    wdt = machine.WDT()
    while True:
        await asyncio.sleep_ms(500)
        wdt.feed()
After connection:

Code: Select all

    loop = asyncio.get_event_loop()
    loop.create_task(do_wdt())
The reason for waiting until after connection is that DNS in particular can block for longer than the WDT period.
Peter Hinch

Post Reply