Description
Let's take a look at a standalone MicroPython device that is only connected to a Wi-Fi router.
Here there are minimal boot.py and main.py.
boot.py
print('boot.py')
essid = "essid" # use your own Wi-Fi router essid
password = "password" # use your own password
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(essid, password)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
do_connect()
import webrepl
webrepl.start()
main.py
print('main.py')
import sys
from utime import sleep_ms
from machine import WDT
wdt = WDT(timeout=5000) # ms
counter = 0
def verify_that_everything_is_functioning_correctly():
return True
def need_to_exit_without_reboot():
global counter
#return True
#return False
return counter > 100
try:
while True:
# do something useful
counter += 1
print(counter, end='\r')
# 1/0 # uncoment this line to raise an exception then reboot
# while True: # uncoment three lines to test the reboot
# print('Wait for WDT reboot')
# sleep_ms(1000)
if verify_that_everything_is_functioning_correctly():
wdt.feed()
if need_to_exit_without_reboot():
wdt.deinit()
sys.exit(0)
sleep_ms(100)
except SystemExit:
print('will enter to REPL(WebREPL)')
except:
print('WDT will reboot the MCU after any exceptions')
raise
finally:
print()
print(counter)
boot.py starts after reset, connect to the Wi-Fi network, and then main.py starts.
main.py initializes the WDT and creates an infinite working(runtime) loop.
Now you can connect to the device via WebREPL, see print() messages, but you can't upload files(update program source code to new version) to the device when your program stays in an infinite working loop. You need to exit from the loop.
You can exit the loop, but WDT resets the device if you are using WDT.init() and don't have WDT.deinit().
When you connect to the REPL(via USB-UART)(if it is possible physically), you can break boot.py or main.py by Keyboard Interrupt before starting WDT, but you don't have time to press Ctrl-C in WebREPL.
So, you need WDT.deinit() function in the normal program life to update the program on a standalone device.
I develop this functionality for the ESP32 port
ESP32/machine_wdt.c: Add WDT.deinit() method. #6631