main
ESP32 Power Outage Monitor
An ESP-IDF based IoT application for ESP32 that monitors power outages by sending periodic heartbeat messages to an MQTT broker.
Hardware Requirements
- ESP32-C3 (or compatible ESP32 variant)
- USB cable for power and programming
- Internet connectivity via Wi-Fi
Architecture
Data Model
Messages published to MQTT follow this JSON schema:
{
"device_id": "ESP32-AABBCCDDEEFF",
"power_status": true,
"latitude": 40.7128,
"longitude": -74.006,
"timestamp": "2025-12-17T10:30:45Z",
"metadata": {
"firmware_version": "1.0.0"
}
}
Building and Flashing
Prerequisites
-
Install ESP-IDF v5.5.1 or later:
Follow the official guide: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/
-
Set up ESP-IDF environment (required before every build):
. $HOME/esp/esp-idf/export.sh
Quick Build (Recommended)
idf.py build
Manual Build
If you prefer manual steps:
-
Set target:
idf.py set-target esp32c3 -
Build:
idf.py buildThe
sdkconfig.defaultsfile automatically configures:- Custom partition table (partitions.csv)
- 2MB flash size
- OTA rollback support
- Optimized settings
Flash
idf.py -p /dev/ttyUSB0 flash monitor
Replace /dev/ttyUSB0 with your serial port:
- Linux:
/dev/ttyUSB0or/dev/ttyACM0 - macOS:
/dev/cu.usbserial-* - Windows:
COM3,COM4, etc.
Initial Setup
First Boot
When the device boots for the first time (no Wi-Fi credentials stored):
- The device starts in Access Point mode
- Connect to Wi-Fi network:
PowerMonitor-Setup - Password:
12345678 - Open browser and navigate to:
http://192.168.4.1 - Fill in the configuration form:
- Wi-Fi SSID: Your Wi-Fi network name
- Wi-Fi Password: Your Wi-Fi password
- MQTT Broker URL: e.g.,
mqtt://broker.hivemq.com - MQTT Port: Default
1883 - MQTT Topic: e.g.,
power/monitor - MQTT Username/Password: Optional
- Device ID: Unique identifier for this device
- Latitude/Longitude: GPS coordinates
- Click Save and Restart
The device will restart and connect to your Wi-Fi network.
Subsequent Boots
On every boot after initial configuration:
- Device loads credentials from NVS
- Connects to Wi-Fi network
- Connects to MQTT broker
- Sends "power restored" event
- Starts sending periodic heartbeats (every 30 seconds)
Description
Languages
C
98.4%
CMake
1.6%