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.
Other countries: order these parts on AliExpress with the links below.
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.
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.
Comments
No comments yet. Share your experience or ask a question.