machine.WDT does not appear to work [Sorted]

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: machine.WDT does not appear to work

Post by davef » Thu Jul 28, 2022 12:03 am

Thank you. I think I need to spend some time to actually understand this as I'd like to use wdt.feed() through-out my programs.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: machine.WDT does not appear to work

Post by davef » Thu Jul 28, 2022 8:43 am

OK, have read-up about module-level Singletons and think I can manage to create one to use.

What I would like to know is why does the system watchdog call wdt.feed() work differently then using other library methods, ie import the library into various modules and call methods from it? Just a keyword is enough.

I use a software WDT written by some kind person on the forum and you just import it into multiple modules and then call wdt_feed() whenever and where ever you want.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: machine.WDT does not appear to work

Post by davef » Thu Jul 28, 2022 9:59 am

wdt_singleton.py

Code: Select all

from machine import WDT

#  system WDT set for 10 seconds
wdt = WDT(timeout = 10000)


def feed_my_wdt():
    wdt.feed()
Still like to know why wdt.feed() needs to be treated like this.

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: machine.WDT does not appear to work

Post by karfas » Thu Jul 28, 2022 1:27 pm

davef wrote:
Thu Jul 28, 2022 9:59 am

Code: Select all

from machine import WDT
#  system WDT set for 10 seconds
wdt = WDT(timeout = 10000)
def feed_my_wdt():
    wdt.feed()
Still like to know why wdt.feed() needs to be treated like this.
In my opinion this is basic python OO handling.
You have system-wide one watchdog, so you need to create the wdt object once. And you need the wdt object to call any of it's methods.

This can be done in main() - then you need to pass the object around (either using a global variable or a parameter).
Or you use your singleton module.
In case of the global variable or the singleton module, you need to import something wherever you want to retrigger the watchdog.

I'm pretty sure your software watchdog library also uses some module-global variable to save the watchdog object.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: machine.WDT does not appear to work

Post by davef » Thu Jul 28, 2022 8:06 pm

karfas,

Thank you for taking the time to explain.

The Software WDT (by mkiotdev):

Code: Select all

## Simple software WDT implementation
wdt_counter = 0

def wdt_callback():
    global wdt_counter
    wdt_counter += 1
    if (wdt_counter >= 10):
        machine.reset()

def wdt_feed():
    global wdt_counter
    wdt_counter = 0

wdt_timer = machine.Timer(-1)
wdt_timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=lambda t:wdt_callback())
## END Simple software WDT implementation
The key appears to be global wdt_counter.

Cheers,
Dave

Post Reply