A comprehensive starting template for ESP32 IoT projects with Home Assistant integration. This template provides a solid foundation for building IoT devices with professional features like automatic WiFi setup, factory reset, OTA updates, and sensor integration.
- WiFi Setup: Automatic AP (Access Point) mode for initial configuration with captive portal
- ESP32 Improv: Mobile app support for easy WiFi configuration
- Factory Reset: Hardware button for device reset (GPIO4 by default)
- OTA Updates: Over-the-air firmware updates
- mDNS Discovery: Better network discovery for Home Assistant
- API Integration: Native Home Assistant API support
- ESP32 Variants: Configurable for different ESP32 boards
- I2C Bus: Pre-configured for sensors and displays
- GPIO Pins: Flexible pin configuration
- Optional Display: SSD1306 OLED support (commented out by default)
- Sensor Framework: Ready for various sensors (BME280 example included)
- Auto-Discovery: Enhanced discovery with unique device names
- Device Status: Uptime and WiFi signal monitoring
- Remote Control: API services for device control
- Diagnostic Sensors: Built-in health monitoring
- ESP32 development board
- ESPHome CLI (
pip install esphome) - Home Assistant (recommended)
# Copy the template
cp base-template.yaml my-project.yaml
# Edit configuration
nano my-project.yamlsubstitutions:
device_name: "my-sensor" # Unique device identifier
device_description: "My IoT Sensor" # Friendly description
button_pin: GPIO4 # Reset button pin
i2c_sda_pin: GPIO21 # I2C SDA pin
i2c_scl_pin: GPIO22 # I2C SCL pin# Validate configuration
esphome config my-project.yaml
# Flash to ESP32
esphome run my-project.yaml- Device starts in AP mode:
my-sensor-xxxx - Connect to WiFi:
my-sensor-xxxx(password:esphome123) - Open
192.168.4.1in browser - Configure your WiFi network
- Device connects and appears in Home Assistant
Option A: Auto-Discovery (may not work in all network configurations)
- Home Assistant should detect the device automatically
Option B: Manual Addition
- Go to Settings β Devices & Services β ESPHome
- Click "Add Device"
- Enter device IP address (check your router's DHCP table)
- Port:
6053(default)
The template includes three WiFi connection methods:
- AP Mode: Initial setup access point
- ESP32 Improv: Mobile apps (ESP Touch, etc.)
- Captive Portal: Automatic browser redirect
Uncomment and modify the BME280 example:
sensor:
- platform: bme280_i2c
temperature:
name: Temperature
humidity:
name: Humidity
pressure:
name: Pressure
address: 0x76For OLED displays, uncomment the display section:
display:
- platform: ssd1306_i2c
model: "SSD1306 128x64"
address: 0x3C
lambda: |-
it.clear();
it.printf(0, 0, id(font_small), "Device Status");
// Add your display logic hereModify button behavior in the on_press section:
on_press:
then:
- lambda: |-
// Your custom button logic
ESP_LOGI("button", "Custom action triggered");sensor:
- platform: dht
pin: GPIO5
model: DHT22
temperature:
name: Temperature
humidity:
name: Humidityswitch:
- platform: gpio
pin: GPIO5
name: "Relay"
id: relaylight:
- platform: rgb
name: "RGB LED"
red: output_red
green: output_green
blue: output_blue
output:
- platform: esp8266_pwm
id: output_red
pin: GPIO12
- platform: esp8266_pwm
id: output_green
pin: GPIO13
- platform: esp8266_pwm
id: output_blue
pin: GPIO14- Check Network: Ensure device and HA are on same subnet
- Manual Addition: Add by IP address instead of auto-discovery
- Firewall: Check if mDNS (port 5353) is blocked
- AP Mode: Device shows as
device-name-xxxxWiFi network - Password: Default AP password is
esphome123 - Captive Portal: Access
192.168.4.1when connected to AP
- I2C Address: Use
i2c.scan: trueto find correct addresses - Pin Configuration: Verify SDA/SCL pins match your wiring
- Power: Ensure sensors have proper power supply
Add API services for remote control:
api:
services:
- service: custom_action
then:
- lambda: |-
// Your custom service logic
ESP_LOGI("service", "Custom action executed");sensor:
- platform: dht
pin: GPIO5
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
- platform: adc
pin: GPIO34
name: "Light Level"
update_interval: 30sFor battery-powered devices:
deep_sleep:
run_duration: 60s
sleep_duration: 5min- Fork the repository
- Create a feature branch
- Test your changes
- Submit a pull request
This project is open source. Feel free to use, modify, and distribute.
This ESPHome base template was developed with the assistance of an AI language model. The template incorporates best practices from the ESPHome community and provides a solid foundation for IoT projects.
Happy Hacking! π