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

  1. Install ESP-IDF v5.5.1 or later:

    Follow the official guide: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/

  2. Set up ESP-IDF environment (required before every build):

    . $HOME/esp/esp-idf/export.sh
    
idf.py build

Manual Build

If you prefer manual steps:

  1. Set target:

    idf.py set-target esp32c3
    
  2. Build:

    idf.py build
    

    The sdkconfig.defaults file 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/ttyUSB0 or /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):

  1. The device starts in Access Point mode
  2. Connect to Wi-Fi network: PowerMonitor-Setup
  3. Password: 12345678
  4. Open browser and navigate to: http://192.168.4.1
  5. 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
  6. Click Save and Restart

The device will restart and connect to your Wi-Fi network.

Subsequent Boots

On every boot after initial configuration:

  1. Device loads credentials from NVS
  2. Connects to Wi-Fi network
  3. Connects to MQTT broker
  4. Sends "power restored" event
  5. Starts sending periodic heartbeats (every 30 seconds)
S
Description
No description provided
Readme 67 KiB
Languages
C 98.4%
CMake 1.6%