Power outage and “light is back” notifier

An ESP32 on a mains charger sends a Telegram message when power returns and keeps a heartbeat that alerts you when it stops.

Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Power & blackouts

⚠ Safety

Use a certified USB charger; nothing else touches mains.

What you need

Buy the parts kit · Estimated total: $13.80

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
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.

Wiring

ModuleModule pinESP32 pinNote
AC-detect module OUToptocouplerGPIO32isolated module, mains side enclosed
ESP32 power5 Vpower bank / Li-ion + boostmust stay alive when mains is lost

Code

power-outage-notifier.ino
#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);
}

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

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.

← ← All projects

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

Comments

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

Comments are moderated.