Page 1 of 2

Initialize watchdog timer from function

Posted: Tue Jun 28, 2022 9:12 pm
by PM-TPI
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

Re: Initialize watchdog timer from function

Posted: Tue Jun 28, 2022 9:33 pm
by davef
Which hardware platform?
At the top of your script try:

Code: Select all

from machine import WDT
is 120_000 legal?

Re: Initialize watchdog timer from function

Posted: Tue Jun 28, 2022 11:52 pm
by jimmo
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.

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 12:12 am
by PM-TPI
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

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 12:28 am
by davef
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

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 12:49 am
by PM-TPI
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 ?

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 2:17 am
by davef
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

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 2:26 am
by davef
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.

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 4:46 am
by bulletmark
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`.

Re: Initialize watchdog timer from function

Posted: Wed Jun 29, 2022 8:36 am
by PM-TPI
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()