How to upload a parameter file to Pico after pressing BootSel

I have a Pico device controlling items and it needs to read a parameter file for specific variables, which change depending on the application or area of use.

The variables in the parameter file need to be altered by the user and uploaded to the Pico, whenever they want. It is easy to do this using such as Thonny by simply uploading the edited parameter file directly into the Pico. However, I need the remote user to be able to do this without using Thonny as they could inadvertantly mess up the program.

I would like them to be able to press the standard BootSel button whilst the Pico is connected to a PC and then drop the parameter file onto the Pico, now appearing as a USB drive. As you know when the Pico is restarted, whatever you have dropped on using this method will not appear there.

Is there a method to do upload parameter files onto a programmed Pico such that program can read that file?

I hope I have explained it OK,

The thing you can do is to run a webserver. I do this for one of my projects. Users can upload a new configuration by

  • starting the Pico-W in what I call “admin-mode” (they press a button while starting the system)
  • then use their browser to upload a new configuration. The admin-mode operates as an access-point and serves a number of web-pages for the user

Of course there are other solutions, e.g. via Bluetooth or by plugging in a SD-card. Depends on your environment.

Have you considered CircuitPython for this application?
When plugged into a PC it appears as a new drive called CIRCUITPY to which you can explore with any file manager (and Thonny),
The running program is called CODE.PY and extra files can be added in the “root” directory or in folders (like LIB).
Use a file like config(.py) that can easily be edited/replaced without touching the CODE.PY file.
(like the secrets.py file used to hold wifi credentials outside of the running program file).
Just a thought.

Thanks @bablokb, the web server is very good, but as I am not using a W version in this application I cannot link via WiFi or Bluetooth. If all else fails then that is what I will do. I didn’t want to wire in the Adafruit SD add-on, but yes, it allows loading in anything.

Thanks @neilman. I may swap and try CircuitPython to allow an upload as you describe. I have always used Pimoroni UF2 and it may be the time to look at using Adafruit’s CircuitPython.

Regards, Steve

This all really depends on your specific setup and the qualification of your users. If they can connect the system directly to a PC, they will always be able to mess up the program. It really boils down to how robust your setup has to be and how much you trust your users.

JSON files are supported. You could use a push-button input to cycle through the loading of applicable parameter files as dictionaries. I use a small I2C LCD display to confirm the mode.

For dynamic parameter changing (and saving) I use an ADC input and push-button to confirm.

Otherwise it as @bablokb says.

Hi SteveA, I use mpremote (MicroPython remote control: mpremote — MicroPython latest documentation) to transfer files between a Pico and a Linux PC, when connected with a USB cable. This only works when Thonny is not running, and doesn’t need the Pico in boot mode.

 mpremote fs cp file.cfg :
 mpremote fs cp :file.log .

You can also list the files on the Pico filesystem:

mpremote fs ls

Hope this helps.

1 Like

Thanks to everyone for all of the good information. I have looked at CircuitPython as suggested by @neilman (thanks) and have done the following which uses a ‘hidden’ parameter button such that on normal boot up the Pico as CIRCUITPY is not seen. When a parameter file is needed to be added or edited, this button is held down on bootup and CIRCUITPY is seen. I have listed the code in case it helps anyone else.

# boot.py - this file is only applicable to a Pico running CircuitPython uf2 and it is a system
# file stored in the Pico.  It is auto run before code.py or main.py to set up certain parameters,
# such as the USB device, MIDI, etc.
# By default, a Pico connected to a PC on bootup will appear as a flash drive called CIRCUITPY, which
# allows access to all of the files stored in the Pico, however this full access may not be a good idea
# for an end user to see. This modified boot.py file will stop the Pico being viewed as a USB drive on a
# PC, however the Pico will still programmically operate OK as a USB HID device.
# If a 'programming' btn is held down on bootup (I have used GPIO 14) it will allow the Pico to boot up
# and be seen as CIRCUITPY to allow you to store/edit parameter files on it.
# Reboot without pressing the btn and the CIRCUITPY drive will not appear, leaving the parameters safe.

import storage
import digitalio
import board

parambtn = digitalio.DigitalInOut(board.GP14)
parambtn.direction = digitalio.Direction.INPUT
parambtn.pull = digitalio.Pull.UP

if parambtn.value == 1:
    # print ('Btn not pressed - disable the USB device')
    storage.disable_usb_drive() # disable Pico as a USB drive.  The other cmd is: storage.enable_usb_drive()

I am using a similar solution: when the button is pressed, I fire up the system as AP with an embedded webserver so users can upload a new configuration file. My users have no physical access, so your solution which is much simpler does not work.

Just be aware that with a CIRCUITPY drive users can still inadvertently mess up the program as you call it. It is just a drive and they can do everything with any files on it. They don’t need Thonny for that, a normal Windows explorer or a similar program will do just fine.

Another thing to be aware of: as a drive, it needs to be cleanly unmounted (in Windows terms “ejected”) after use. So even if they don’t touch any of your program files they can still mess up everything.

Thank @bablokb for you expressing your concerns in case I was not aware of them. I am using a Tiny Pico so, as mentioned before no Wireless or Bluetooth. I accept that the admin person authorised to alter the system parameters, when they have pressed the program button and uploaded a new/edited parameter file would have unfetted access to the Pico. They can be instructed to safely eject the drive (as they must surely do for all ejectable drives) or shut down the PC - it is standard practice. However, once done and it is restarted anyone else connecting this Pico Device to a PC, etc, (which is acting as an USB HID device) would not normally have access to see what is on it. For me it is simpler than changing to a Pico W and creating an AP web server. It serves my purpose, however I thank you for your concerns.

Totally valid. Keep it simple as long as it works :-)

BTW: you could also disable the serial and HID-devices from boot.py, so the Pico would de facto be invisible to the PC.