ESP32 Smart Home
Servo light switches, a servo door lock and a keypad, five ESP32 boards talking over ESP-NOW and controlled from my phone. No cloud, no smart bulbs.
What it does
I wanted to control my apartment's lights and front door from my phone without replacing anything. Instead of smart bulbs and a smart lock, small servos press the existing light switches and turn the existing deadbolt.
- Phone control: a web page with on/off buttons for the bed light and the kitchen light, and unlock/lock for the door.
- Remote unlock: one button unlocks the door for someone, turns both lights on and starts a 3-minute countdown on the keypad. If the right code isn't entered in time, the buzzer sounds.
- Keypad by the door: D unlocks when leaving and re-locks 10 seconds later, B and C toggle the lights, and during a countdown it takes the code.
How it works
I planned it on paper first: one hub that speaks both Wi-Fi and ESP-NOW, and four nodes that are "dumb actuators". The hub is a translator: HTTP from the phone comes in, ESP-NOW packets go out. ESP-NOW sends raw packets between ESP32 boards by MAC address, with no router and no IP addresses involved, so a command reaches a servo almost instantly.
Every command is the same two-byte packet. A node ignores anything not addressed to its device number:
typedef struct {
uint8_t deviceId; // 1, 2, 3 = servos, 99 = keypad event
uint8_t action; // 0 = OFF / LOCK, 1 = ON / UNLOCK
} Command;
if (cmd.deviceId != DEVICE_ID) return; // not for me
servo.write(cmd.action == 1 ? ON_POS : OFF_POS); // e.g. 45 deg
Remote unlock is a short sequence on the hub:
void s3on() {
sendCommand(NODE3_MAC, 3, 1); // unlock the door
delay(1000);
sendCommand(NODE1_MAC, 1, 1); // bed light on
sendCommand(NODE2_MAC, 2, 1); // kitchen light on
sendCommand(KEYPAD_MAC, 99, KP_REMOTE_UNLOCK); // start countdown
}
Hardware
| Node | Board | Parts |
|---|---|---|
| Hub | ESP32-S3 DevKit | Wi-Fi web server and ESP-NOW |
| 1 · bed light | ESP32-C3 | servo on a 3D-printed switch-plate mount |
| 2 · kitchen light | ESP32-C3 | servo on a 3D-printed switch-plate mount |
| 3 · door lock | ESP32-C3 | MG996R servo in a printed mount that grips the deadbolt's thumb-turn |
| 4 · keypad | ESP32 DevKit | 4×4 keypad, 16×2 I²C LCD, buzzer, printed enclosure |
Design and build
I designed the mounts in CAD and 3D-printed them. The door-lock mount is a round base with four prongs that grip the deadbolt's thumb-turn, so the servo turns the existing lock. The light switches get a plate that holds a small servo right next to the switch lever.
Making it reliable
When I later connected the keypad to my robot, its events sometimes never reached the hub. Two causes, two fixes:
- Lost broadcasts. ESP-NOW broadcasts aren't acknowledged, so one dropped packet meant a missed event. Keypad events now go to the hub as unicast, which the radio acknowledges and retries, and each event is sent three times with a sequence number so the hub can drop the duplicates.
- A sleepy hub. With Wi-Fi power saving on (the default), the hub's radio dozed between
beacons and missed packets that arrived meanwhile. Turning it off fixed it:
WiFi.setSleep(false).
void sendHubEvent(uint8_t event) {
HubEvent ev = { 99, event, ++hubEventSeq }; // one seq for all copies
for (int i = 0; i < HUB_EVENT_REPEATS; i++) { // 3 acknowledged unicasts
esp_now_send(HUB_MAC, (uint8_t*)&ev, sizeof(ev));
delay(20);
}
}
One more gotcha: ESP-NOW only works when every board is on the same Wi-Fi channel as the hub's router, since the hub has to stay on that channel to serve the web page.
Working with Project Qwen
When I built my home robot, Project Qwen, I connected it to this system instead of starting over. The hub gained a few HTTP routes for the robot (door and lights on their own, cancelling the countdown) and a small event log the robot polls. Now pressing D on the way out arms Qwen's security mode, Qwen turns the lights on when it sees an intruder, and after a remote unlock it locks the door again once it recognizes my face.
What I'd improve
- Security: the control page has no login and the ESP-NOW packets aren't encrypted. It only works on my home network, but both are the next things to add.
- Feedback: the nodes don't report back, so the page can't show whether a light is actually on.