Level: Beginner Board: ESP32-C3 SuperMini Smart home
⚠ Safety The relay switches only the low-voltage DC wire of the router adapter. Never put the relay in a mains wire. Check polarity and the adapter voltage first.
What you need Buy the parts kit · Estimated total: $9.50
ESP32-C3 SuperMini × 1
$2.50
Relay module 1-channel 5V (opto-isolated) × 1
$1.50
USB charger 5V 2A × 1
$4.00
Dupont jumper wires (M-F, M-M) × 1
$1.50
Some links are affiliate links: we may earn a commission at no extra cost to you.
Libraries Arduino IDE → Tools → Manage Libraries, then install:
ESP32Ping (marian-craciunescu) Code router-watchdog.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;
}
#include <ESP32Ping.h>
const int PIN_RELAY = 6;
const int RELAY_ON_LEVEL = LOW; // level that ENERGISES the relay (opens the NC contact)
const char* PING_HOST = "1.1.1.1";
const int FAILS_TO_REBOOT = 4; // 4 checks x 30 s = 2 minutes without internet
const uint32_t COOL_DOWN_MS = 180000; // wait for the router to boot
int fails = 0, reboots = 0; bool pending = false;
void powerCycle() {
digitalWrite(PIN_RELAY, RELAY_ON_LEVEL); delay(10000); // router off 10 s
digitalWrite(PIN_RELAY, !RELAY_ON_LEVEL);
reboots++; pending = true;
}
void setup() {
Serial.begin(115200);
pinMode(PIN_RELAY, OUTPUT); digitalWrite(PIN_RELAY, !RELAY_ON_LEVEL);
connectWiFi();
}
void loop() {
bool ok = WiFi.status() == WL_CONNECTED && Ping.ping(PING_HOST, 2);
if (ok) {
if (pending) { telegramSend(String("Internet is back after router reboot #") + reboots); pending = false; }
fails = 0;
} else {
if (WiFi.status() != WL_CONNECTED) connectWiFi(8000);
if (++fails >= FAILS_TO_REBOOT) { fails = 0; powerCycle(); delay(COOL_DOWN_MS); connectWiFi(); }
}
delay(30000);
}Flash a ready firmware from the browser Paste your error into the AI Error Doctor
Build tips Use the NC contact so the router stays on if the ESP32 loses power. Put a 5 minute cool-down after a reboot, otherwise slow routers will be rebooted in a loop. ← ← All projects
Related ideas ⚠ Caution Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.
Two reed switches report open and closed, a relay gives the opener a pulse, and a message tells you when the gate was left open.
An LED ring that shows green when power is on, red when it is off, and amber for a scheduled outage window.
⚠ Caution Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.
A Telegram bot on the ESP32 reports status on request and switches a relay with commands from your phone, with no server and no port forwarding.
Comments
No comments yet. Share your experience or ask a question.