micropython-ota-quickstart-readme.mp4
An application-file updater for MicroPython devices. It follows a configured
GitHub branch or the latest published GitHub Release, downloads a changed src
tree into a staging directory, and swaps it into place after the download
succeeds.
This project updates Python application files. Firmware upgrades are performed
from a host with make flash; it does not rewrite the MicroPython firmware over
the air.
Read the companion article, MicroPython OTA updates with GitHub and ESP32, for the design background and a complete walkthrough.
This path installs the updater on one ESP32_GENERIC board and starts a minimal
application from GitHub.
For this example, use the preconfigured
micropython-ota-quickstart
repository.
Create your own public repository from this template;
it already includes the required src/main.py and start(settings, updater)
entrypoint.
git clone https://github.com/smysnk/micropython-ota-updater.git
cd micropython-ota-updater
python3 -m venv .venv
. .venv/bin/activate
make installcp device/env.example.py device/env.local.pyBefore deploying, replace both Wi-Fi placeholders in device/env.local.py.
The example already points to the public quickstart application; replace
githubRemote when you are ready to deploy your own repository:
'wifiAP': 'YOUR_WIFI_NAME',
'wifiPassword': 'YOUR_WIFI_PASSWORD',
'githubRemote': 'https://github.com/smysnk/micropython-ota-quickstart',Then choose one update mode.
Track a branch: install the current commit from the selected branch. This is the default and the simplest option while developing.
'githubUpdateMode': 'branch',
'githubRemoteBranch': 'main',Track GitHub Releases: install the commit tagged by the latest published non-draft, non-prerelease release.
'githubUpdateMode': 'release',Release mode ignores githubRemoteBranch. It resolves the release tag to an
immutable commit SHA and installs src from that commit; attached release
assets are not downloaded.
Connect one ESP32 over USB and confirm that mpremote lists a USB serial
device:
python -m mpremote devsOn macOS, the device normally looks like /dev/cu.usbserial-* or
/dev/cu.SLAB_USBtoUART. Do not continue if the list only contains Bluetooth
or debug-console ports.
Warning:
eraseremoves the existing firmware and every file on the selected device.
Erase the board and install MicroPython:
make erase flashThen copy the updater and your local configuration to the board:
make deploy
make replAfter the REPL connects, press Ctrl-D to restart the device and see the
complete boot sequence. On its first boot, the updater connects to Wi-Fi and
downloads the quickstart application's src/ directory from GitHub, so the
first startup can take a little longer. It should finish by printing:
ota-controller: OTA application started
ota-controller: heartbeat 1
ota-controller: heartbeat 2
Tip: The commands automatically select the connected board. If more than one compatible serial device is connected, specify one with, for example,
SERIAL_PORT=/dev/cu.usbserial-0001 make deploy. Runmake helpfor the other commands that accept this setting.
In branch mode, edit src/main.py, commit it, and push it to the configured
branch. In release mode, also create a GitHub Release whose tag points to the
commit you want to deploy.
Press the board's reset button or enter Ctrl-D in the REPL. The updater will
install the selected commit and retain the previous application until the new
version calls updater.confirm().
If you later change modes in device/env.local.py, run make deploy before
resetting the device. Selecting the same commit causes no reinstall; selecting
a different commit installs it even if it is older.
The sections below cover the application contract and recovery in more detail.
The GitHub application repository must contain a src directory with a
main.py module exposing start:
import machine
import time
def start(settings, updater):
# Optional: a reset before confirmation restores src.previous on next boot.
watchdog = machine.WDT(timeout=60000) if settings.get('watchdog') else None
# Perform enough initialization to know this version can run, then confirm.
updater.confirm()
# Enter the application's normal loop.
while True:
if watchdog:
watchdog.feed()
time.sleep(1)Confirmation is important. Until updater.confirm() runs, the previous
application remains in src.previous and .ota-pending marks the new version
as unconfirmed. If the device resets first, the next boot restores the previous
application. An exception raised while importing or starting the new
application also triggers rollback and reset.
HTTP, logging, and time are application concerns in version 3. Import the standard MicroPython modules the application needs instead of expecting the bootstrap to inject wrappers.
- Recover any interrupted or unconfirmed previous update.
- Compare
src/.versionwith the selected branch or release commit SHA. - Check free filesystem space and download into
src.next. - Write and read back
src.next/.version. - Create
.ota-pending, renamesrctosrc.previous, and renamesrc.nexttosrc. - Start the application. The application calls
updater.confirm()when its critical initialization has succeeded.
The updater streams file bodies in 512-byte chunks, closes responses on all handled failures, verifies TLS hostnames and certificate chains, and leaves the current application untouched if staging fails.
Automatic recovery is conservative: an update with both .ota-pending and
src.previous is rolled back at the beginning of the next updater run. For
manual inspection:
mpremote connect /dev/cu.usbserial-0001 fs tree -sh :
mpremote connect /dev/cu.usbserial-0001 fs cat :.ota-pendingDo not remove src.previous while .ota-pending exists. Full recovery steps
and the migration notes from the earlier release are in docs/UPGRADING.md.
The updater is intended for recent network-capable MicroPython builds with verified HTTPS support, a writable filesystem with directory and rename operations, and enough storage for the active application, a staged download, and a rollback copy. MicroPython 1.23 and newer releases are likely compatible.
The currently validated baseline is ESP32_GENERIC with MicroPython 1.28.0,
as pinned in manifest.json. CI also compiles against MicroPython's development
branch. Other boards and older releases may work, but should be treated as
unverified until their TLS, filesystem, memory, and reset behaviour are tested
on hardware.
Project guides: Development · CI and hardware validation