Files
smart_lights/README.md
T
Matt SpencerandCopilot c6b1537c8c
release / release (push) Successful in 1m39s
tools: install a release into the idle firmware slot
Add tools/install_release.py, a host script that takes a release from Gitea
and puts it on a board over USB the way an update would: it downloads the
release's application image and checks it against the release checksums,
writes it to the slot that is not running, and rewrites otadata to select
that slot as a new image before resetting the board.

It reads the board's own partition table and slot selection rather than
assuming a layout, refuses a board without the A/B partition table, and
writes nothing until the image has been downloaded and verified. A dry run
shows what it would do.

This lets the A/B slots be tested with real releases while there is no
over-the-air update source. The README documents it, and the full image's
install steps now clear the slot selection, so that a board which had
switched to the second slot runs the image just written.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-29 01:16:37 +00:00

202 lines
8.2 KiB
Markdown

# smart_light
Firmware for a Zigbee-controlled LED strip light, written in Rust for the
ESP32-C6. It joins a Zigbee network as an Extended Color Light, so it shows up
in Home Assistant (ZHA) as a normal light with on/off, brightness, colour
temperature and colour. It can also be controlled locally with a rotary
encoder, and can play animated effects.
## Hardware
- Seeed Studio XIAO ESP32-C6
- WS2812B LED strip (24 LEDs by default), data on D2 (GPIO2)
- Rotary encoder with push button: CLK on D10, DT on D9, SW on D8
(GPIO18, GPIO20, GPIO19)
The on-board user LED (GPIO15) blinks as a heartbeat.
## Controls
| Input | Action |
|---|---|
| Turn the encoder | Brightness up or down |
| Click | Next colour temperature preset |
| Long press | Turn on or off |
| Hold for 10 seconds | Forget the Zigbee network and go back to pairing mode |
From Home Assistant you get on/off, brightness, colour temperature and colour.
Blink and breathe effects are available through the light's `flash` option, and
the animated scenes (rainbow, pulse, comet) are described under
[Effects](#effects).
## Building from source
You need [rustup](https://rustup.rs). The repository pins a nightly toolchain
in `rust-toolchain.toml`, so the right compiler, components and the
`riscv32imac-unknown-none-elf` target are installed on the first build.
The Zigbee stack is a git submodule (`vendor/zigbee-rs`), so clone with
submodules:
```sh
git clone --recurse-submodules https://gitea.m5p3nc3r.co.uk/m5p3nc3r/smart_lights.git
cd smart_lights
```
If you already cloned without them, run `git submodule update --init`. The
submodule URL is an SSH address on GitHub; if you have no SSH key set up, switch
it to HTTPS first with
`git config submodule.vendor/zigbee-rs.url https://github.com/m5p3nc3r/zigbee-rs.git`.
Build:
```sh
cargo build --release
```
To flash a board connected over USB and open the serial log, install
[espflash](https://github.com/esp-rs/espflash) and run:
```sh
cargo install espflash
cargo run --release
```
`cargo run` uses the runner in `.cargo/config.toml`, which flashes the app with
the partition table in `partitions.csv` and then starts the monitor. The Zigbee
network state is kept in the `storage` partition, so reflashing keeps the
light on its network.
### Firmware slots
The flash has two app slots, `ota_0` and `ota_1`, and an `otadata` partition
that tells the bootloader which to run, so a new image can be written to the
idle slot while the other keeps running. A USB flash always writes slot 0 and
clears `otadata`, which is why the runner passes `--erase-parts otadata`: without
it, a board that had switched to slot 1 would keep booting the old image.
Changing from the old single-slot partition table to this one needs one USB
flash. The network state is not touched.
The firmware logs which slot it is running from and the state of its image, and
marks a new image as valid once it has stayed up for 20 seconds. Switching
slots has no update source yet (see `TODO.md`). Two development features
exercise it without one: `ota-test` switches to the other slot 30 seconds after
every boot, and `ota-test-crash` resets without confirming the image. Set
`FW_LABEL` when building to tell images apart in the log, and write an image to
the idle slot with `espflash save-image` and `espflash write-bin`.
**There is no rollback yet.** The bootloader that `espflash` installs is not
built with rollback support, so an image that crashes after being selected will
keep being booted. Recover by flashing over USB.
To build without Zigbee, for example to test the strip and encoder on their own:
```sh
cargo build --release --no-default-features --features esp32c6
```
The `led_diag`, `pin_diag` and `gpio_out_diag` binaries in `src/bin` are small
hardware checks, run with `cargo run --release --bin led_diag`.
## Configuration
- `src/config.rs`: number of LEDs, maximum brightness, encoder timing.
- `src/zigbee_stack.rs`: the Zigbee network to join. The extended PAN ID and
channel (25) are set in the code and must match your coordinator.
## Joining the network
A light that is not on a network is always in pairing mode: it scans every few
seconds, and the on-board LED blinks quickly while it does. To join, open a
pairing window on your coordinator (in ZHA, **Add device**) and the light joins
as soon as it sees it. After that it rejoins by itself when it restarts.
To pair a light that is already on a network, for example to re-pair it or move
it to another coordinator, hold the encoder button for 10 seconds. The strip
flashes green twice, the light forgets its network and restarts, and it is then
in pairing mode as above. The coordinator is not told, so Home Assistant keeps
the device and recognises it when it joins again.
The same applies if it has lost its network keys, for example after a long
coordinator outage: open the pairing window and it will join again.
The network it looks for is fixed in the firmware (see Configuration), so a
light only pairs with a coordinator whose extended PAN ID and channel match.
The extended PAN ID is broadcast by the network and is not a secret.
## Effects
Beyond the standard controls, the light has a manufacturer-specific effects
cluster (`0xFD00`) that selects an animated scene (rainbow, pulse or comet) and
sets its speed and intensity. Home Assistant needs a quirk to talk to it:
1. Copy `ha/zha_quirks/smart_light.py` into a folder in your Home Assistant
config, for example `custom_zha_quirks/`.
2. Point ZHA at it in `configuration.yaml`:
```yaml
zha:
custom_quirks_path: /config/custom_zha_quirks/
```
3. Restart Home Assistant. The light then has an Effect select and Effect speed
and Effect intensity sliders.
## Releases
Every commit to `main` builds the firmware on the Gitea runner and publishes a
release (`.gitea/workflows/release.yml`).
The version is the `MAJOR.MINOR` of the version in `Cargo.toml` followed by the
number of commits on `main`, so with `0.1.0` in `Cargo.toml` the 57th commit
becomes `0.1.57`. It only goes up, and can be worked out again from any commit.
To start a new series, change the major or minor number in `Cargo.toml`. The
build writes the version into the firmware, which logs it at boot, and the
release is tagged `v` plus the version.
Each release has:
- `smart-light-<version>-full.bin`: bootloader, partition table and
application. Write it at address 0 with
`espflash write-bin 0x0 smart-light-<version>-full.bin`, then clear the slot
selection with `espflash erase-parts otadata` so that the board runs it.
The image stops short of the `storage` partition, so the light keeps its
Zigbee network.
- `smart-light-<version>.bin`: the application alone, for a firmware slot.
- `smart-light-<version>.elf.gz`: the unstripped build, for decoding backtraces.
- `SHA256SUMS`.
### Installing a release over USB
Once a board has the A/B partition table, `tools/install_release.py` installs a
release into the slot that is not running and switches to it, without
touching the running image until the new one has been downloaded and checked:
```sh
tools/install_release.py # the latest release
tools/install_release.py 0.1.23 # a particular one
tools/install_release.py --dry-run # show what would be done, write nothing
```
It reads the board's partition table and slot selection, downloads the release's
application image and checks it against `SHA256SUMS`, writes it to the idle
slot, rewrites `otadata` to select that slot as a new image, and resets the
board. The firmware logs the version it is running, which slot it came from,
and when it confirms the image, so the log shows the update has worked. Run it
again and the board alternates to the other slot.
This is a way of testing the A/B slots with real releases, not an update
mechanism for lights in use: it needs the board on a USB cable. There is no
rollback yet (see `TODO.md`), so an image that does not start has to be fixed
by flashing over USB.
The workflow's `runs-on` must match a label of your Gitea runner. If the job's
own token is not allowed to create releases, add a token with write access to
the repository as a secret named `RELEASE_TOKEN`.
## License
Released under the [MIT License](LICENSE). The Zigbee stack in
`vendor/zigbee-rs` is a separate project with its own licenses.