Files
smart_lights/README.md
T
Matt SpencerandCopilot 9ea25c3aab feat: add A/B firmware slots
Split the single app partition into two slots, ota_0 and ota_1, with an
otadata partition that tells the bootloader which to run, so an update can
be written to the idle slot while the other keeps running. The Zigbee
storage partition is unchanged, so the light keeps its network. Moving from
the old table needs one USB flash.

* src/ota.rs logs the running slot and the state of its image at boot,
  marks an image valid after it has stayed up for 20 seconds, and can
  switch to the other slot and restart. The slot in use is read from the
  flash mapping, not from otadata.
* src/flash.rs shares the one flash handle: esp-storage allows only one, and
  both the Zigbee stack and the update code need it.
* The runner clears otadata on every USB flash. A USB flash always writes
  slot 0, so without this a board that had switched to slot 1 would keep
  booting the old image.
* Two development features, ota-test and ota-test-crash, exercise slot
  switching and an unconfirmed image without an update source. FW_LABEL tells
  builds apart in the log.

Tested on the hardware: switching between two images across six reboots, each
booting the slot it had just selected, resuming its Zigbee network, and
confirming itself. Rollback is not available: the bootloader espflash installs
is not built with it, so a newly selected image reports "New" rather than
"PendingVerify", and an image that resets without confirming itself is booted
again indefinitely. Needs a rollback-enabled bootloader. Nothing delivers
updates yet. Both are recorded in TODO.md.

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

145 lines
5.6 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.