0

For a upcoming project I have a need for a microcontroller that can "self update" - load new code and logic at runtime. So far my thoughts where that I would use two ESP devices, one as the "version controller/updater" and the other one for logic. Recently I came upon the Raspberry pico and saw that it was using micropython that basically uploads python files to pico "storage". I am unfamiliar with this system, but would I be right to presume that it could just "create" these files on it's own and execute as it seems they are interpreted by some micropython loader? Google doesn't really have any results on this.

4
  • Oof.. I do a lot on the Pico in C, but there is no ability to self-update. Now, I don't see how micropython would be any different. The micropython library for the pico is an interpreter that runs on the pico, but as with C, you need to upload (flash) the instructions to be interpreted. Now let me qualify that a bit, you can with C manipulate what is stored in flash to some degree. However I don't believe this is fully what you are looking for. With C, you can use 2 picos, one as a debug probe with openocd to tranfer new code to the pico - I don't no if similar exists for python. Commented Apr 12, 2024 at 8:32
  • For micropython the device shows up as storage where you just copy the files so there is no "flashing". Thats why I thought you might just be able to edit/add files on the runtime. Commented Apr 12, 2024 at 10:26
  • The top-level view might not reflect what actually happens. If the user application is non-volatile, it will be in the flash memory, for sure. Practically each MCU with flash memory I came across has the ability to erase and write this memory in-application. Just note that this happens in blocks. Commented Apr 12, 2024 at 10:45
  • you have to build in an update solution. the pico is interesting in that it is microcontroller-ish but without on chip flash, it relies on off chip flash. other than that it is just an mcu and you have to do the update work and have a fallback in case the update fails and all that (or a recovery solution, etc). doesnt matter what programming language you are using. Commented Apr 12, 2024 at 18:21

1 Answer 1

2

MicroPython source code that you place in your board's flash memory is interpreted at runtime, and MicroPython has access to the filesystem on the flash memory, so the answer is yes: you can create or modify Python code in files on the device's filesystem and then execute them.

Here is a quick example I have tested in the Wokwi online emulator; I haven't tested it on a real board but have no reason to believe it would behave differently:

import time
import os
time.sleep(0.1) # Wait for USB to become ready

os.mkdir('foo')
with open('foo/foo.py', 'w') as f:
    f.write('print("foo!")')
from foo import foo

When you run this it prints foo! to the MicroPython console.

You can run MicroPython on ESP8266 (depending on memory) and ESP32 boards as well as on the Pico, so you might not even need to switch hardware if you are already using the ESP. The MicroPython documentation describes the differences between boards.

Allowing your code to download and run external code creates a potential security vulnerability of course, so you should consider whether this is an issue in your project.

Sign up to request clarification or add additional context in comments.

5 Comments

Agreed. Tested your code on a physical Pico w and it worked first time.
Hmm nice. This could be a potential direction for me to take.It seems it is not possible to "self" reflash in arduino runtime. Only other option I can think of is to have two controllers.
If you are looking for something with the same format (size) that gives you all the tools you have on a full Linux OS, why not a Pi zero 2 W? With 512M RAM and built in MicroSD (now readily available in 1/2 terrabyte cards) they are incredibly capable.
@DavidC.Rankin This is a viable option. My only issue would be the boot up time. Google says it is 8s, but that is with GUI. Can’t find how fast that would be in headless. Anything above 3s would be a dealbreaker
3 sec is about right. I have 2 running headless on the coffee table in front of me I"m ssh'ed into working on ADC code. The pi-imager installs Debian Bullseye. Works great. gcc 10.2.1, Python 3.9.2 (and PostgreSQL and MariaDB, and nginx and PHP and ....) It's a full desktop in a package the size of a gum-stick. They run forever. The only thing they lack is built-in ADC (I've got an ADS1115 and MCP3008 on a breadboard). Pico does ADC natively -- which will spoil you. Another option is MilkV-Duo. (pico format, but with 64M or 256M RAM and native MicroSD - runs Busybox)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.