A smart thermostat built on the ESP32 platform featuring environmental sensing, e-paper/LVGL display, deep sleep power management, and multiple connectivity options (WiFi, Bluetooth, Blynk IoT).
Designed to fit inside an existing thermostat housing, this project includes both the firmware and KiCad hardware design files.
- Environmental sensing via Bosch BME680 (temperature, humidity, pressure, air quality)
- Display options: E-paper (GxEPD2) or LVGL-based display
- Deep sleep with RTC memory persistence for battery efficiency
- Task scheduler that survives deep sleep cycles
- Multiple connectivity: WiFi with static IP, Bluetooth LE GATT, Blynk IoT
- Bistable relay control via DRV8837 H-bridge (no continuous power needed)
- Serial CLI for debugging and configuration
- OTA updates support
esp32-thermostat/
├── hardware/ # KiCad PCB design files
│ ├── thermo_v2_266.kicad_sch # Schematic
│ ├── thermo_v2_266.kicad_pcb # PCB layout
│ └── pins.ods # Pin mapping spreadsheet
├── software/ # PlatformIO firmware project
│ ├── src/ # Implementation files
│ ├── include/ # Header files
│ ├── lib/ # Libraries (BSEC, LVGL submodule)
│ └── platformio.ini # Build configuration
└── README.md
- PlatformIO (VS Code extension or CLI)
- ESP32 development board (tested with LILYGO TTGO T5 V2.66)
git clone https://github.com/your-username/esp32-thermostat.git
cd esp32-thermostat
git submodule update --init --recursive- Copy the secrets template and fill in your credentials:
cp software/include/secrets.h.example software/include/secrets.h-
Edit
software/include/secrets.hwith your WiFi credentials and Blynk tokens. -
Configure features in
software/include/config.h:
// Display type (choose one)
//#define DISPLAY_EPD // E-paper display
#define DISPLAY_LVGL // LVGL-based display
// Optional features
#define HAS_SENSOR // Enable BME680 sensor
#define SENSOR_SAMPLERATE 3 // Sample rate: 3 or 300 seconds
#define COMMANDS // Extended CLI commands
//#define BLUETOOTH // Bluetooth LE support
//#define OTA // WiFi OTA updates
#define BLYNK // Blynk IoT platformcd software
pio run -t upload
pio device monitorFor OTA updates (after initial flash):
pio run -e mhetesp32devkit-ota -t uploadThe firmware is organized into namespaces, each handling a specific subsystem:
| Namespace | File | Description |
|---|---|---|
Power |
power.cpp/h | Deep sleep management, battery monitoring, wake source handling |
Tasks |
tasks.cpp/h | RTC-persistent task scheduler that survives deep sleep |
RTC |
rtc.cpp/h | Real-time clock and timestamp management |
CLI |
cli.cpp/h | Serial command interface for debugging |
Commands |
commands.cpp/h | Extended CLI command set |
| Namespace | File | Description |
|---|---|---|
Sensor |
sensor.cpp/h | BME680 environmental sensor via BSEC library |
Display |
display.cpp/h | Display abstraction (EPD or LVGL backend) |
Input |
input.cpp/h | Button handling via AceButton library |
Peripherals::LED |
peripherals.cpp/h | Status LED control |
Peripherals::Thermo |
peripherals.cpp/h | Bistable relay control via DRV8837 |
Pins |
pins.cpp/h | GPIO pin definitions and initialization |
| Namespace | File | Description |
|---|---|---|
Wifi |
wifi.cpp/h | WiFi with static IP, NTP sync, HTTP client, OTA |
Bluetooth |
bluetooth.cpp/h | BLE GATT server (Environmental Sensing, Thermostat, Battery services) |
_Blynk |
blynk.cpp/h | Blynk IoT platform integration |
First boot:
- Full initialization of all subsystems
- Register scheduled tasks
- Display boot screen
- Enter main loop
Wake from deep sleep:
- Minimal initialization (pins, power, RTC, input)
- Restore task scheduler from RTC memory
- Run due tasks based on wake source
- Return to deep sleep
Variables marked with RTC_DATA_ATTR survive deep sleep:
- Boot count
- Last sensor reading
- BSEC sensor calibration state
- Task schedules and next run times
- Temperature target setpoint
- Heating state
Managed via PlatformIO:
espressif32- ESP32 platformzinggjm/GxEPD2- E-paper display driverbxparks/AceButton- Button debouncingblynkkk/Blynk- IoT platformlvgl/lvgl- Graphics library (git submodule)- BSEC library (Bosch) - BME680 sensor processing
The hardware/ directory contains KiCad 8 project files for a custom PCB designed to fit inside an existing thermostat housing. Key components:
- ESP32 module (LILYGO TTGO T5 V2.66 compatible pinout)
- BME680 environmental sensor
- DRV8837 H-bridge for bistable relay control
- Multiple button inputs with pull-up resistors
- Two red leds to illuminate the display indirectly
MIT
