Initialize watchdog timer from function

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Initialize watchdog timer from function

Post by PM-TPI » Tue Jun 28, 2022 9:12 pm

As title states...
Need to start wdt after all other settings are complete.

I tried this, no go

Code: Select all

def start_up():
	global wdt
	other stuff....
	
	wdt = WDT(timeout=120_000) # 2min

wdt.feed() got... NameError: name 'wdt' isn't defined

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

Re: Initialize watchdog timer from function

Post by davef » Tue Jun 28, 2022 9:33 pm

Which hardware platform?
At the top of your script try:

Code: Select all

from machine import WDT
is 120_000 legal?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Initialize watchdog timer from function

Post by jimmo » Tue Jun 28, 2022 11:52 pm

PM-TPI wrote:
Tue Jun 28, 2022 9:12 pm
wdt.feed() got... NameError: name 'wdt' isn't defined
You'll need to post more of your code. Something else is accessing the wdt variable.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: Initialize watchdog timer from function

Post by PM-TPI » Wed Jun 29, 2022 12:12 am

I do have... from machine import WDT

Code: Select all

from machine import WDT

def start_up():
	global wdt
	other stuff....
	
	wdt = WDT(timeout=120_000) # 2min

def run_ing():	# this is hit every min
	global wdt
	wdt.feed()
	other stuff....

rather not use global either

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

Re: Initialize watchdog timer from function

Post by davef » Wed Jun 29, 2022 12:28 am

Now try:

Code: Select all

from machine import WDT

wdt = 0 
then your defs.

Maybe:

Code: Select all

from machine import WDT
wdt = WDT(timeout=120_000) # 2min
then your defs.

Can't help if you don't want globals

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: Initialize watchdog timer from function

Post by PM-TPI » Wed Jun 29, 2022 12:49 am

are you saying

Code: Select all

from machine import WDT
wdt = 0

def start_up():
	global wdt
	other stuff....
	
	wdt = WDT(timeout=120_000) # 2min

def run_ing():	# this is hit every min
	global wdt
	wdt.feed()
	
	other stuff....
AttributeError: 'int' object has no attribute 'feed'
only on 1st go around, then seems to work
what type of var is wdt ?

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

Re: Initialize watchdog timer from function

Post by davef » Wed Jun 29, 2022 2:17 am

Try my 2nd example:

Code: Select all

from machine import WDT
wdt = WDT(timeout=120000) # 2min
I guess in the first case wdt = 0 is not the same as:

Code: Select all

from machine import WDT
wdt = WDT(timeout=120_000) # 2min
so must be a incorrect example

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

Re: Initialize watchdog timer from function

Post by davef » Wed Jun 29, 2022 2:26 am

There might be another issue with your code.

Here is my original interrupt routine:

Code: Select all

from machine import WDT
shaft_interrupt = False

def handle_interrupt(xyz): #  needs something
    global shaft_interrupt
    shaft_interrupt = True

pir = Pin(33, Pin.IN, Pin.PULL_UP)
pir.irq(trigger=Pin.IRQ_FALLING, handler=handle_interrupt)
I had to put something in the def handle_interrupt() or I would get an error, don't remember if it was the same error as yours.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: Initialize watchdog timer from function

Post by bulletmark » Wed Jun 29, 2022 4:46 am

PM-TPI wrote:
Wed Jun 29, 2022 12:49 am

Code: Select all

from machine import WDT
wdt = 0

def start_up():
	global wdt
	other stuff....
	
	wdt = WDT(timeout=120_000) # 2min

def run_ing():	# this is hit every min
	global wdt
	wdt.feed()
	
	other stuff....
The only thing wrong with your above code is that you can't `feed()` the WDT until it has been initialized. So just change the feed line to:

Code: Select all

if wdt:
    wdt.feed()
Also it is bad/unclear to intialize `wdt` to an int (i.e. 0 in this case). Better is `wdt = None`.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: Initialize watchdog timer from function

Post by PM-TPI » Wed Jun 29, 2022 8:36 am

The only thing wrong with your above code is that you can't `feed()` the WDT until it has been initialized. So just change the feed line to:
I thought... wdt = WDT(timeout=120_000) in start_up() did the initializing Note: start_up() is run before run_ing()

1st... I just changet wdt = 0 to wdt = None and got... AttributeError: 'NoneType' object has no attribute 'feed'

then I added...
if wdt: wdt.feed()
and now works correctly... Thank you

but why?
wdt = WDT(timeout=120_000) is run before wdt.feed()

Post Reply