Power outage notifier

A battery-backed ESP32-C3 that detects when mains disappears and sends a message with the time of the blackout.

Power & batteries Board: ESP32-C3 SuperMini Level: ●●○ ⏱ 2–3 h

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

The catch nobody mentions: the router goes dark too

A device that says "power is off" must itself have power and a way out. Most routers die with the mains, so the message can never be sent at that moment. There are two honest designs. Either the notifier stores the time of the outage and sends "off for 2 h 13 min" when the power and the Wi-Fi return, or the router sits on a small UPS and you get the message right away.

Building it

A mains-detect module with an optocoupler gives a clean logic level. The ESP32-C3 is powered from an 18650 cell through a charger, so it keeps running when the socket is dead. On the first low level it saves a timestamp to flash, then goes to light sleep and checks the pin.

  • Take the time from NTP at start; the C3 does not keep time through a power cut, so keep a counter in RTC memory while it lives on battery.
  • A TP4056 board without the protection chip does not protect the cell. Choose the version with DW01 or add a protected cell.
  • Test the full cycle by pulling the plug on purpose: check the duration the message reports against your watch.

Components

  • ESP32-C3 SuperMini
  • AC 220V optocoupler detection module
  • TP4056 USB-C Li-ion charger with protection
  • 18650 Li-ion cell + holder
  • Plastic project enclosure

Wiring

Module Module pin ESP32 pin Note
AC-detect module OUT optocoupler GPIO32 isolated module, mains side enclosed
ESP32 power 5 V power bank / Li-ion + boost must stay alive when mains is lost

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN";   // from @BotFather
const char* TG_CHAT  = "YOUR_CHAT_ID";

// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
  if (WiFi.status() != WL_CONNECTED) return false;
  WiFiClientSecure client;
  client.setInsecure();   // simple; pin the certificate for production
  HTTPClient http;
  String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
  if (!http.begin(client, url)) return false;
  http.addHeader("Content-Type", "application/json");
  String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
  int code = http.POST(body);
  http.end();
  return code == 200;
}
const int PIN_AC = 32;               // HIGH while mains is present (check your module)
bool mainsOk = true;
void setup() {
  pinMode(PIN_AC, INPUT);
  Serial.begin(115200);
  connectWiFi();
  telegramSend("Power monitor started");
}
void loop() {
  static uint32_t lastChange = 0; static bool stable = true;
  bool now = digitalRead(PIN_AC) == HIGH;
  if (now != stable) { stable = now; lastChange = millis(); }
  if (millis() - lastChange > 5000 && stable != mainsOk) {          // debounce 5 s
    mainsOk = stable;
    if (WiFi.status() != WL_CONNECTED) connectWiFi(10000);
    telegramSend(mainsOk ? "Mains power restored" : "Mains power lost! Running on battery");
  }
  delay(100);
}

Build tips

  • Add a free healthchecks.io check: it alerts you when the pings stop.
  • The router must also be on a UPS or power bank to deliver the outage alert.

What you need

Buy the parts kit · Estimated total: $12.30

ESP32-C3 SuperMini × 1
$2.50
Buy at AliExpress Buy at Amazon.com
AC 220V optocoupler detection module × 1
$2.00
Buy at AliExpress Buy at Amazon.com
TP4056 USB-C Li-ion charger with protection × 1
$0.80
Buy at AliExpress Buy at Amazon.com
18650 Li-ion cell + holder × 1
$4.00
Buy at AliExpress Buy at Amazon.com
Plastic project enclosure × 1
$3.00
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Ukraine: we buy and sell these parts ourselves, order them on this site.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Power outage journal

Records every outage with start, end and duration, builds statistics per day and week, and shows how closely the real power matches the published schedule.

  • Board ESP32-C3 SuperMini
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit
⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Orchard and greenhouse frost alert

A battery sensor that wakes up rarely and sends an alert when the temperature falls toward freezing.

  • Board ESP32-C3 SuperMini
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit
⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Solar-powered weather node

A small outdoor station on a Li-ion cell and a solar panel that reports temperature, humidity and pressure trend every few minutes.

  • Board ESP32-C3 SuperMini
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.