ESPHome is a remarkably simple yet powerful framework and toolchain for programming ESP8266/ESP32 boards. ESPHome provides a whole bunch of “components” - pre-built modules to manage the underlying hardware/functionality - and lets you enable, configure, and integrate them with a few short lines of YAML.
ESP32 (and the ESP8266 predecessor) is “a series of low-cost, low-power system on a chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth” with a wide range of uses, but particularly valuable for IoT and/or home automation.
There are relatively inexpensive dev boards (£14 on The Pi Hut), and a whole bunch of commercial “smart” devices built on top of an ESP core.
I wanted a simple bed-side “smart button” to turn off the house lights. All too often I’d get into bed and turn my side lamp off, only to realise I’d left something on downstairs. Then I’d have to get my phone and unlock it and be dazzled by the screen and then open Home Assistant and find the right button etc. etc.
I acknowledge there are much bigger problems in the world, but this was an annoyance I hoped I could easily fix!
I’m reluctant to build too much from scratch here. There’s other ways I’d rather spend my spare time, and building good UX is hard and I’m not the only user. But, like all-too-much home automation kit, all the commercial-off-the-shelf “smart buttons” I found required internet connectivity and a third-party online service - nope! Building something myself seemed like the way forward.
On the hardware front, the M5Stack Atom Lite offers a USB-C-powered ESP32 board with button and RGBW LED in a nice enclosure and for just $6 - ideal!
Now, I have a background in electronics and computing. I could probably download the Arduino toolchain and eventually cobble together some code to make it connect to my WiFi, register an event listener for the button, and fire off an HTTP request when pressed. But most of the work would be me writing unoriginal scaffolding and infrastructure code. Other people have definitely done this already, and probably much better than I would. I have no desire to re-invent the wheel - I just want it to work.
Enter ESPHome.
ESPHome comes with components for managing logging, WiFi connectivity, switches, displays, and much more!
So having the board connect to WiFi on startup is just:
wifi:
ssid: SSID
password: PASSWORD
Configuring the LED:
light:
- platform: fastled_clockless
chipset: SK6812
name: "LED"
pin: GPIO27
id: led
rgb_order: GRB
num_leds: 1
And then making the button do all the magic:
binary_sensor:
- platform: gpio
name: "Goodnight"
pin: GPIO39
filters:
- invert:
on_click:
then:
- light.turn_on:
id: led
brightness: 50%
red: 100%
green: 0%
blue: 0%
- homeassistant.service:
service: switch.turn_off
data:
entity_id: group.all_lights
- delay: 1s
- light.turn_off:
id: led
No need to configure my own event loop, handle switch bounce, or spend any real time on anything apart from the behaviour unique to me.