ESP-NOW in Rust
On ESP32 Microcontrollers
FEKT VUT Brno |
Why Rust on ESP32?
- Memory Safety: No buffer overflows or dangling pointers. Essential for devices running 24/7.
- Testability:
Functional features (Iterators, Enums, Pattern Matching) allow you to separate logic from hardware.
Result: Test logic on your PC, flash to MCU with confidence.
- Espressif Ecosystem:
Official support (esp-rs) and esp-hal v1.0.0 (Stable API) was recently released.
The Foundation: Embassy
This project relies heavily on Embassy — the Async Embedded Runtime.
- Async/Await on Hardware: Write concurrent code like it's JavaScript or Go, but on bare metal.
- No RTOS Required: No FreeRTOS complexity. The executor manages tasks cooperatively.
- Efficiency: Handles Wi-Fi, Timers, and GPIO simultaneously with zero-cost abstractions.
// Embassy makes async easy
#[embassy_executor::task]
async fn connection_task(mut controller: WifiController) {
loop {
match controller.connect().await {
Ok(_) => info!("Wifi connected!"),
Err(e) => warn!("Failed to connect: {:?}", e),
}
Timer::after(Duration::from_secs(5)).await;
}
}
Why ESP-NOW for Automation?
Advantages
- Instant Boot & Send (< 100ms)
- Extremely Low Power
- Long Range (Wi-Fi PHY)
- No Router / IP stack overhead
Constraints
- Small Payload (250 Bytes)
- No Direct Internet Connection
- Connectionless (Send & Pray/Ack)
Technology Comparison
| Feature |
ESP-NOW |
Zigbee |
Wi-Fi Station |
Bluetooth LE |
| Topology |
Peer-to-Peer |
Mesh |
Star (Central) |
P2P / Mesh |
| Hardware |
Standard ESP32 |
Special Radio |
Standard ESP32 |
Standard ESP32 |
| Range |
High (Hundreds m) |
Medium |
High |
Low |
| Battery Life |
Excellent |
Excellent |
Poor |
Good |
| Hub? |
Optional (DIY) |
Required |
Router Req. |
Phone/Gateway |
The Connectivity Gap
Smart devices on ESP-NOW are "invisible" to your PC.
They don't have IP addresses. They don't show up on your Router.
Solution: The USB Gateway
- A dedicated ESP32-C3 plugged into USB.
- Listens on ESP-NOW Channel 11.
- Bridges packets to Serial Terminal (UART).
System Architecture
[ Temp Sensor ] ))) ESP-NOW (((
|
[ Light Switch] ))) ESP-NOW ((( --> [ GATEWAY C3 ]
| |
[ Motion Det. ] ))) ESP-NOW ((( | USB / UART
v
[ Raspberry Pi / PC ]
(Interactive Terminal)
Use the gateway for debugging or as a Home Assistant bridge.
repo: esp32-c3-supermini-esp-now-gateway
LIVE DEMO
Connecting to Gateway terminal...
Checking for broadcasts on Channel 11
Resources & Future
Note: The code presented works on any ESP32, but was validated on the C3 SuperMini.