Pico W Micro Python autorun of code even after reboot

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Pico W Micro Python autorun of code even after reboot

Post by Rissy » Sun Aug 14, 2022 5:17 pm

As some will be aware, I was initially struggling with getting some good data out of a Temp/Humidity sensor I bought for my new Pico W.
If you're not aware, but interested, you can find that thread here: viewtopic.php?f=21&t=12900

The end result of that, with LOTS of very good help from "Roberthh" (Thank you very much for that Roberthh!) is that I am now getting useful data from that sensor.

Now on to the next challenge!

In addition to the above, and getting MQTT working (see here for that thread: viewtopic.php?f=21&t=12904),

I also need to know how to make my new Pico W code "live" as it were, so that it survives a power down, and automatically starts the code on boot up.

Can Anyone advise me, and guide me on how to do this?

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python autorun of code even after reboot

Post by Rissy » Mon Aug 15, 2022 9:35 pm

Okay, so without any assistance, and a bit of googling and watching youtube, I've now become aware of the necessity for boot.py and main.py. This isn't even described in the instruction manual! However, some nice chap on youtube enlightened me to these file naming requirements to get my code to autoboot. *two thumbs up*

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pico W Micro Python autorun of code even after reboot

Post by pythoncoder » Tue Aug 16, 2022 8:53 am

Normally you only need main.py.
Peter Hinch
Index to my micropython libraries.

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python autorun of code even after reboot

Post by Rissy » Tue Aug 16, 2022 2:51 pm

pythoncoder wrote:
Tue Aug 16, 2022 8:53 am
Normally you only need main.py.
Thanks Peter, I recognise this. I'm sure having a boot.py could be useful perhaps, like maybe engaging the wi-fi since this is something I'd probably always want to do anyway, and then run the main.py on the understanding that the wi-fi is already up and connected? Is this proposed reasonable practice?

efahl
Posts: 15
Joined: Sat Dec 23, 2017 2:02 pm

Re: Pico W Micro Python autorun of code even after reboot

Post by efahl » Tue Aug 16, 2022 3:54 pm

I've been using boot.py exclusively for like 6 years. I never thought there was any best practice on this, am I mistaken?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pico W Micro Python autorun of code even after reboot

Post by pythoncoder » Tue Aug 16, 2022 5:06 pm

boot.py runs before the board is fully configured. The filesystem is set up, but USB is not yet configured. The recommendation is to use main.py to launch your application.

Using boot.py to connect to WiFi should be OK but the official advice is to keep boot.py code minimal.
Peter Hinch
Index to my micropython libraries.

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python autorun of code even after reboot

Post by Rissy » Tue Aug 16, 2022 5:11 pm

pythoncoder wrote:
Tue Aug 16, 2022 5:06 pm
boot.py runs before the board is fully configured. The filesystem is set up, but USB is not yet configured. The recommendation is to use main.py to launch your application.

Using boot.py to connect to WiFi should be OK but the official advice is to keep boot.py code minimal.
Ah right. Well it's certainly minimal right now. I've just got it flashing the onboard LED as an indication that it's now definitely powered up! :D

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

Re: Pico W Micro Python autorun of code even after reboot

Post by jimmo » Wed Aug 17, 2022 3:00 am

efahl wrote:
Tue Aug 16, 2022 3:54 pm
I've been using boot.py exclusively for like 6 years. I never thought there was any best practice on this, am I mistaken?
pythoncoder wrote:
Tue Aug 16, 2022 5:06 pm
boot.py runs before the board is fully configured. The filesystem is set up, but USB is not yet configured. The recommendation is to use main.py to launch your application.
That's right. Any code than runs in boot.py will be unable to print to stdout on boards that use USB to access the console/REPL.

The purpose of boot.py is only to configure the peripherals that might be needed to run main.py, such as:
- Filesystems (e.g. mounting block devices, so that you can actually find main.py and other libraries)
- USB (so that main.py can print, etc)
- Network (so that webrepl is ready by the time main.py runs)

Post Reply