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
AC 220V optocoupler detection module × 1
$2.00
TP4056 USB-C Li-ion charger with protection × 1
$0.80
18650 Li-ion cell + holder × 1
$4.00
Plastic project enclosure × 1
$3.00
Some links are affiliate links: we may earn a commission at no extra cost to you.
Code power-outage-notifier.ino Copy Download .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.
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.
⚠ Caution Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
A battery sensor that wakes up rarely and sends an alert when the temperature falls toward freezing.
⚠ Caution Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
A small outdoor station on a Li-ion cell and a solar panel that reports temperature, humidity and pressure trend every few minutes.
Comments
No comments yet. Share your experience or ask a question.