A light firmware for the ESP32-C3 board that controls a light bulb
connected to a 5 V relay. The light can be switched on or off via HTTP server
routes, a physical switch, or automatically based on input from a AM312 PIR motion
sensor and a BH1750 ambient light sensor. The board also includes a
DS18B20 temperature sensor, whose readings are displayed on an SSD1306 OLED display.
demo-light: the main firmware for the ESP32-C3 board, which expects the real sensors and relay to be connected.mock: a testing binary that simulates sensor input, for development and testing without hardware. This firmware uses the built-in LED and the button on GPIO 9 to simulate the light and switch.
It implements an HTTP server that manages the state of the light via REST requests:
light/onroute turns the light onlight/offroute turns the light offlight/toggleroute toggles the lightlight/manualroute enables manual mode, allowing control via the physical switch or the/on,/offand/togglerouteslight/motion-detectionroute enables motion-based mode, where the light turns on/off based on the AM312 PIR motion sensorlight/ambient-lightroute enables ambient-light mode, where the light turns on/off depending on the light level measured by the BH1750 sensor
Each time the light state changes, a JSON message describing the current status
is published to the /light/<esp32c3_mac_address>/events topic on an MQTT broker.
The board can be discovered by another node on the same network via
an mDNS-SD service.
The diagram below illustrates how the components are connected in this demo.
This diagram was created using Fritzing, an open-source tool.
The corresponding project file is demo.fzz.
To build the demo-light firmware run:
cargo build --release --bin demo-lightTo build the mock firmware run:
cargo build --release --bin mockTo build both of them run:
cargo build --releaseTo flash and run the demo-light firmware on an ESP32-C3 board:
cargo run --release --bin demo-lightTo flash and run the mock firmware on an ESP32-C3 board:
cargo run --release --bin mockImportant
Always use the release profile [--release] when building esp-hal crate. The dev profile can potentially be one or more orders of magnitude slower than release profile, and may cause issues with timing-senstive peripherals and/or devices.
Support for connecting USB devices is not natively available on Windows
Subsystem for Linux (WSL).
In order to use the ESP32-C3 board with WSL, follow this
guide and manually
connect the USB port used by the board to WSL.
- Rename
cfg.toml.exampletocfg.tomland populate it with your Wi-Fi credentials:ssid,password,broker_address,broker_port - Connect the board to a laptop via a
USB-Ccable to view the logs - Pin the project to a specific
nightlyversion for more stability, if needed
