Pico W - Running BOOT.PY while Developing MAIN.PY

1,955 views
Skip to first unread message

Paul Naish

unread,
Feb 11, 2023, 8:50:26 PM2/11/23
to thonny
I am interested in following suggestions about separating out WiFi connectivity from MAIN.PY to BOOT.PY. What I don't know is if I can develop and deploy BOOT.PY on the Pico W and still develop and run MAIN.PY in Thonny and running it on the Pico W from Thonny. Will the BOOT.PY run before Thonny downloads and runs the MAIN.PY? Thanks

Aivar Annamaa

unread,
Feb 12, 2023, 5:23:13 AM2/12/23
to thonny
> Will the BOOT.PY run before Thonny downloads and runs the MAIN.PY?

I assume that by "downloading main.py" you mean saving some code as main.py on the device. Please note just saving main.py does not run anything -- for Thonny these are different actions. Thonny allows you to run the editor content directly with the green arrow button or F5 key -- in this case nothing gets saved on the device. Alternatively, you can save the editor content to the main.py on the device but in this case nothing gets executed automatically. If you want to run the main.py as it is on the device (regardless of what do you currently have in the editor), you can press Ctrl-D to soft reboot your device.

Every time you power on your device or press Ctrl-D on idle prompt, your device first executes boot.py and then main.py. Please note that when you connect to your device in Thonny before boot.py has completed, then with the default settings (with "Interrupt working program on connect" checked) it will be interrupted.

What happens when you run the editor content in Thonny, depends on another setting -- "Restart interpreter before running a script". If this is checked, then Thonny first puts you device's REPL into raw mode and then performs a soft-reboot. In this mode soft-reboot only runs boot.py, not main.py. After boot.py has completed, Thonny sends whatever you have in the active editor to be executed on your device. If this setting is OFF, then Thonny executes the editor content right away, i.e. with the global state your device happens to have at this moment.

I now realized the users may get confused by this, because currently Thonny surpresses the output from boot.py in case of these soft-reboots. I'll change this in the next Thonny version (https://github.com/thonny/thonny/issues/2655).

"Restart interpreter before running a script" is ON by default, because I assume in general people want to start their program in a consistent state, without being affected by the leftovers of previous runs or REPL commands. It is optional, because sometimes soft-reboot can be too slow. For example, when you put the WiFi connection code in boot.py of ESP32 devices, it resets the connection and reconnects each time (at least with some MicroPython versions). With Pico W this doesn't seem to be the case -- according to my experiments, Pico W keeps the Wifi connection over soft-reboots, so if you have managed to create the connection once, doing it again after the soft-reboot will be very quick (until the next hard-boot, of course).

Best regards,
Aivar

Aivar Annamaa

unread,
Feb 12, 2023, 5:33:40 AM2/12/23
to thonny
On Sun, Feb 12, 2023 at 12:23 PM Aivar Annamaa <aivar....@gmail.com> wrote:
Thonny allows you to run the editor content directly with the green arrow button or F5 key -- in this case nothing gets saved on the device.

I was imprecise here. If your current editor represents a file on the device's filesystem, then it does save it before running it, but it still runs the code by sending the editor content directly to the MicroPython interpreter, not via the saved file. If your editor represents a file in the local filesystem, then nothing gets saved to your MicroPython device (which means testing your code will not wear your device's flash).

A.

Paul Naish

unread,
Feb 12, 2023, 2:01:10 PM2/12/23
to thonny
Thanks Aivar for the detailed response. Also for the setting which I found checked.

I created a simple test:

[boot.py] saved to the Pico
# boot.py
with open("test.txt","w") as f:
    f.write("Hello from BOOT.PY\n")

main.py saved on my RPI 400
# main.py
with open("test.txt","a") as f:
    f.write("and from MAIN.PY\n")

When i clicked (restart backend) the file 'test.txt' appeared on the Pico.
When I ran 'main.py' and test.txt was updated as

Hello from BOOT.PY
and from MAIN.PY


Reply all
Reply to author
Forward
0 new messages