Schalten Sie selbst nur Kleinspannungslasten. Für Netzlasten ein zertifiziertes isoliertes Modul im Gehäuse nutzen oder eine Elektrofachkraft holen.
Wenn es erst nach einem Neustart funktioniert
Billige Router, ONTs und Kabelmodems hängen sich manchmal auf, während die LEDs grün leuchten. Ein Relais, das ihnen kurz den Strom nimmt, ist eine sehr praktische Lösung. Die Kunst liegt darin, nicht übereifrig zu sein.
Logik, die Neustartschleifen vermeidet
Pingen Sie mehrere Ziele, etwa das Gateway und zwei öffentliche Adressen, und handeln Sie nur, wenn alle mehrere Minuten ausfallen.
Schalten Sie 15 bis 30 Sekunden aus, damit sich die Kondensatoren entladen.
Warten Sie nach dem Zyklus: Ein GPON-ONT braucht zwei bis drei Minuten, ein Router eine Minute. Urteilen Sie nicht zu früh.
Nutzen Sie ein Back-off: 2 Minuten, dann 10, dann eine Stunde, mit einer Grenze für Zyklen pro Tag. Hat der Anbieter eine Störung, hilft ein Neustart Ihres Routers nicht.
Schalten Sie wenn möglich die Niedervoltseite des Netzteils, mit dem Relais im Kabel, statt der 230-V-Seite. Ein Öffner hält den Router an, auch wenn der ESP32 stirbt. Denken Sie daran, dass der Watchdog dasselbe WLAN benutzt: Auch „WLAN verloren“ gilt als Fehler.
Komponenten
ESP32-C3 SuperMini
Relay module 1-channel 5V (opto-isolated)
Dupont jumper wires (M-F, M-M)
USB charger 5V 2A
Verdrahtung
Modul
Modul-Pin
ESP32-Pin
Hinweis
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
Bibliotheken
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);
}
Bautipps
Den NC-Kontakt verwenden, damit der Router weiterläuft, wenn der ESP32 stromlos wird.
Nach einem Neustart 5 Minuten Pause einplanen, sonst wird ein langsamer Router in einer Schleife neu gestartet.
Kommentare
Noch keine Kommentare. Teilen Sie Ihre Erfahrung oder stellen Sie eine Frage.