from machine import WDT, Pin, reset_cause, WDT_RESET
import time
led = Pin("LED", Pin.OUT)
# Create a watchdog with a 5-second timeout
wdt = WDT(timeout=5000)
print()
print("Watchdog Timer Demo")
if reset_cause() == WDT_RESET:
print("*** Previous reset was caused by the WATCHDOG ***")
else:
print("Normal startup")
start_time = time.ticks_ms()
last_blink = time.ticks_ms()
while True:
elapsed = time.ticks_diff(time.ticks_ms(), start_time)
# Simulate a lockup after 15 seconds
if elapsed > 15000:
print()
print("*** Simulating software hang ***")
print("*** Watchdog will reset the Pico in ~5 seconds ***")
while True:
pass
# Feed watchdog during normal operation
wdt.feed()
# Blink LED once per second
if time.ticks_diff(time.ticks_ms(), last_blink) >= 1000:
last_blink = time.ticks_ms()
led.toggle()
print("Running:", elapsed // 1000, "seconds")
time.sleep_ms(50)