Jump to content

ESP32

From Archive

ESP32 & Home Assistant

ESP32 microcontroller integration with Home Assistant for DIY home automation

Overview

The ESP32 is a dual-core microcontroller with WiFi and Bluetooth built-in. Combined with Home Assistant, it enables custom IoT sensors and actuators throughout your home.

Hardware

Common Development Boards

Board Pins USB Chip Notes
ESP32 DevKit C 30-38 CP2102 Most popular, good for breadboard
NodeMCU-32S 30 Varies Breadboard-friendly
Adafruit ESP32 Feather Feather Native USB Battery connector, STEMMA QT
Adafruit ESP32-S3 Feather Native USB Newer variant, better performance
Adafruit ESP32-C6 Feather Native USB Thread support

Gotchas:

  • Pins 6-11 connect to integrated flash (do not use them)
  • Some pins have boot/reset functionality—check your board's pinout
  • DevKitC V4 has CP2102N (higher baud rates) vs V2 with CP2102

Adafruit Sensors

Sensor Type Interface Use Case
BME280 Temp/Humidity/Pressure I2C/SPI Weather station
DHT22 Temp/Humidity 1-Wire Room monitoring
BH1750 Light level I2C Illuminance sensing
SGP30 Gas (eCO2/TVOC) I2C Air quality
VL53L0X Time-of-flight I2C Distance/motion
PIR Motion GPIO Occupancy detection

Integration Approaches

ESPHome (Recommended)

YAML-based firmware that compiles to C++. Easiest path to Home Assistant.

Advantages:

  • Automatic device discovery in Home Assistant
  • Native API (no MQTT needed)
  • OTA updates built-in
  • Excellent sensor library support

Example config:

esphome:
  name: bedroom-sensor
  platform: esp32
  board: esp32dev

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

api:
  encryption:
    key: !secret api_key

sensor:
  - platform: dht
    pin: GPIO4
    temperature:
      name: "Bedroom Temperature"
    humidity:
      name: "Bedroom Humidity"
    update_interval: 60s

MQTT Integration

Direct Arduino code publishing to MQTT broker. More control, more boilerplate.

Key Libraries:

  • Adafruit_MQTT_Library (easy, feature-complete)
  • PubSubClient (lightweight)
  • ArduinoHA (provides MQTT discovery for auto-addition to HA)

Bluetooth Proxy

ESP32 acts as bridge for nearby BLE devices, extending Home Assistant's Bluetooth range.

Power Management

Deep Sleep

Most systems shut down except RTC. Current: 11.5µA to 7mA depending on board.

ESPHome deep sleep:

deep_sleep:
  run_duration: 10min
  sleep_duration: 50min

Battery life estimates:

  • WiFi active: ~160mA
  • Light sleep: ~10-15mA
  • Deep sleep (optimized): ~11.5µA
  • Deep sleep (standard): ~7mA

Battery Best Practices

  1. Use interval monitoring—sleep most of the time, wake every X minutes
  2. Use GPIO interrupt from sensor, only wake on change
  3. Disable Bluetooth if only using WiFi
  4. Choose boards with low deep sleep current
  5. Use lithium batteries (CR2032, CR2025, or Lithium AA)

Development Tools

Tool Best For Pros Cons
Arduino IDE Quick prototyping Simple, large library ecosystem Limited debugging, slower compilation
PlatformIO Professional projects Better build system, unit testing, VS Code integration Steeper learning curve
CircuitPython Education, rapid prototyping Python syntax, instant reload ~3x slower than Arduino C
ESPHome Home Assistant sensors YAML-based, auto-discovery Less flexibility for complex logic

Design Patterns

Single-Purpose Sensor Node

Small, battery-powered, reads one or two sensors, publishes every X minutes, uses deep sleep.

Multi-Sensor Hub

Powered device, multiple sensors via I2C/SPI, always-on or frequent updates.

Smart Switch/Actuator

Paired with sensors for feedback, fast response time, manual override buttons.

Gateway/Proxy

Bluetooth Proxy for BLE devices, permanently powered, no sensors.

Starter Projects

  1. Temperature/Humidity Monitor — DHT22 + ESP32 Feather, ESPHome, WiFi + HA display
  2. Motion-Activated Light — PIR sensor + LED, relay control, manual override
  3. Battery Environmental Sensor — BME280, deep sleep, report hourly, ~1-2 year battery
  4. Bluetooth Proxy — ESP32 + ESPHome, discover nearby BLE devices

Decision Tree

Starting Home Assistant automation?
├─ Want easiest path? → ESPHome with Feather board
├─ Need maximum flexibility? → Arduino + MQTT + PlatformIO
├─ Want to learn programming?
│  ├─ Prefer Python? → CircuitPython
│  └─ Prefer C++? → Arduino IDE
├─ Building battery-powered sensors? → ESPHome + Deep Sleep
└─ Extending Bluetooth range? → ESP32 Bluetooth Proxy

See Also


Technical
Core Technical · CLI · Dotfiles · Nvim · SSH · VPS
Tools Sketchybar · ArchiveBox · ThinkPad Linux
Systems Automation · Personal APIs · Quantified Self
Reference Runbooks · New Computer Runbook · Syntax guide