Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.
When it works only after a reboot
Cheap routers, ONT boxes and cable modems sometimes freeze while the lights stay green. A relay that power-cycles them is a very practical fix. The art is in not being overzealous.
Logic that avoids reboot loops
Ping several targets, for example the gateway and two public addresses, and act only if all of them fail for several minutes.
Power off for 15 to 30 seconds so that the capacitors discharge.
After the cycle wait: a GPON ONT can take two or three minutes to be ready, and a router needs a minute. Do not judge it too early.
Use a back-off: 2 minutes, then 10, then an hour, with a limit on the number of cycles per day. If the provider has an outage, rebooting your router will not help.
Switch the low-voltage DC side of the power adapter if you can, with a relay in the cable, instead of the 230 V side. A normally closed contact keeps the router on even if the ESP32 dies. Remember that the watchdog uses the same Wi-Fi: treat "Wi-Fi lost" as a failure too.
Components
ESP32-C3 SuperMini
Relay module 1-channel 5V (opto-isolated)
Dupont jumper wires (M-F, M-M)
USB charger 5V 2A
Wiring
Module
Module pin
ESP32 pin
Note
Relay IN
signal
GPIO6
active LOW on most modules (set RELAY_ON_LEVEL)
Relay VCC / GND
5V / GND
Router DC power
+ wire
relay COM / NC
break only the DC + wire of the router adapter (low voltage, e.g. 12 V); never the mains
Libraries
ESP32Ping (marian-craciunescu)
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;
}
#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);
}
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.
Comments
No comments yet. Share your experience or ask a question.