Skip to content

Signed OTA updates from GitHub releases + firmware modernization - #2

Merged
NinjaLikesCheez merged 5 commits into
mainfrom
ota-updates-and-modernize
Nov 12, 2026
Merged

NinjaLikesCheez merged 5 commits into
mainfrom
ota-updates-and-modernize

Conversation

@NinjaLikesCheez

Copy link
Copy Markdown
Contributor

Summary

The switch now checks GitHub for a newer release on every boot and installs it over the air. The firmware also moves to the current ESP32 toolchain and libraries.

This fixes a live problem: the certificate for spacestate.pixelbar.nl was replaced on 28 Oct 2026. It now chains to DigiCert Global Root G2, but the current firmware only trusts Let's Encrypt's ISRG Root X1. The deployed switch is therefore probably failing to submit and fetch the space state. The new firmware checks certificates against the standard Mozilla CA bundle instead of one hard-coded root, so it connects again.

OTA updates

  • On boot the device fetches releases/latest from the GitHub API. If the tag is newer than the running version, it streams firmware.bin into the inactive OTA slot and reboots into it.
  • Encrypted connections: all traffic uses TLS, checked against the ESP-IDF CA bundle (covers api.github.com, the GitHub release CDN and spacestate.pixelbar.nl).
  • Signed firmware: images must carry an ECDSA P-256 signature from the project key. This uses the signing support built into the ESP32 core's update library. Someone who gets into the GitHub repo or its releases still can't install their own firmware.
  • No downgrades: only strictly newer versions are installed. The signed image must contain a version marker matching the release tag, so an older signed image can't be re-uploaded under a newer tag.
  • Rollback: if the new firmware doesn't get as far as connecting to WiFi, the bootloader switches back to the previous version.
  • Local builds don't update: builds made by hand report version dev and never replace themselves.

Release pipeline

  • release.yml runs when a vX.Y.Z tag is pushed. It builds with that version, signs the binary, checks the signature against the previous release's public key (the key devices actually trust, so key rotation is handled safely), checks the version marker, and publishes the release.
  • It refuses to publish unsigned firmware if the key or secret is missing.
  • build.yml builds on PRs and pushes to main.

Modernization

  • Platform: pioarduino (Arduino-ESP32 3.3.12 / ESP-IDF 5.5). The official PlatformIO platform is stuck on 2.x.
  • Libraries: ArduinoJson 7 and the maintained ESP32Async AsyncTCP / ESPAsyncWebServer forks.
  • MQTT: PubSubClient replaced by ESP-IDF's built-in client, which reconnects in the background, so the main loop no longer blocks when the broker is down.
  • Double reset: the archived double-reset library is replaced by a small NVS-based detector, which also removes the SPIFFS dependency.
  • Code layout: the .ino is split into modules.

Security fix: no credentials in firmware

The space state API token used to be compiled in, and release binaries are public. Credentials are now stored on the device and set via the config portal; Secrets.h is optional and only used to seed them on a USB flash. The portal no longer pre-fills passwords or tokens, and the token is no longer printed to the serial log.

Bug fixes

  • A task could spin without ever pausing before its first check.
  • Failed submissions were retried every 200 ms; now every 10 s.
  • Status polling and submissions could race each other; both now run in one task.
  • The state was never re-published, so Home Assistant's expire_after marked the sensor unavailable after 15 minutes. It's now re-published every 5 minutes.
  • LEDs and shared state were accessed from several tasks without coordination.

Before merging / deploying

  1. scripts/generate-ota-signing-key.sh, then gh secret set OTA_SIGNING_KEY < ota_signing_key.pem, then commit keys/ota_signing_key.pub.pem. Until then OTA stays disabled.
  2. Flash the device over USB once. The partition table changes to min_spiffs.csv: the build is about 1.40 MB and doesn't fit the old 1.25 MB slots. WiFi and MQTT settings are kept. Enter the space state credentials in the portal, or provide src/Secrets.h for that flash.
  3. Tag v1.0.0 to publish the first signed release.

Testing

  • Builds cleanly; no warnings in project sources with -Wall -Wextra.
  • Signing pipeline tested locally with a throwaway key: build, sign and verify all pass, and the version marker and public key are embedded.
  • Release verification logic tested on first-release, normal and key-rotation scenarios: each was correctly accepted or rejected.
  • Chunked version-marker search unit-tested on the host, including markers spanning chunk boundaries.
  • Confirmed the roots for spacestate.pixelbar.nl and the GitHub hosts are in the embedded CA bundle.
  • Not yet tested on hardware: WiFi portal, switch/LEDs, MQTT/Home Assistant, a real OTA update, and rollback.

🤖 Generated with Claude Code

NinjaLikesCheez and others added 5 commits November 5, 2026 19:18
On boot the device checks the latest GitHub release and installs it if
newer. Updates are fetched over TLS (verified against the ESP-IDF CA
bundle), must carry an ECDSA P-256 signature from the project key, and
must contain a version marker matching the release tag so older signed
images can't be republished to downgrade devices. Failed boots roll back.

A release workflow builds, signs and publishes firmware for v* tags,
verifying the signature against the key devices currently trust.

Modernization:
- pioarduino platform (Arduino-ESP32 3.3.12 / ESP-IDF 5.5)
- ArduinoJson 7, ESP32Async web server, ESP-IDF MQTT client
- Replace archived double-reset library with a small NVS-based one
- Use the CA bundle instead of pinning ISRG Root X1; spacestate.pixelbar.nl
  now chains to DigiCert Global Root G2, which the old firmware rejects
- Split the sketch into modules; min_spiffs partitions for 1.9MB OTA slots

Credentials moved from compile-time Secrets.h into NVS (set via the config
portal), since release binaries are public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move ota::markFirmwareValid() to run immediately after WiFi connects,
before checkForUpdate(). This prevents the OTA check from causing a
rollback if it fails, hangs, or crashes. The rollback decision should
be based on whether the firmware successfully connected to WiFi, not
on whether a subsequent GitHub API call succeeded.

Co-authored-by: NinjaLikesCheez <NinjaLikesCheez@users.noreply.github.com>
Change the retain flag from 0 to 1 in publishState() so the state
topic is retained as documented in the README. This ensures Home
Assistant and other MQTT clients see the last state immediately
on connection without waiting for the next periodic publish.

Co-authored-by: NinjaLikesCheez <NinjaLikesCheez@users.noreply.github.com>
spacestate.pixelbar.nl now rejects HTTP/1.0 requests with
426 Upgrade Required, so every state fetch failed. Drop useHTTP10()
and read the body with getString(), which handles chunked responses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NinjaLikesCheez
NinjaLikesCheez marked this pull request as ready for review November 12, 2026 11:02
@NinjaLikesCheez
NinjaLikesCheez merged commit e98c28c into main Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants