# 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. 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--full.bin`: bootloader, partition table and application. Write it at address 0 with `espflash write-bin 0x0 smart-light--full.bin`. It stops short of the `storage` partition, so the light keeps its Zigbee network. - `smart-light-.bin`: the application alone, for a firmware slot. - `smart-light-.elf.gz`: the unstripped build, for decoding backtraces. - `SHA256SUMS`. 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.