Created
July 4, 2022 11:54
-
-
Save treideme/5e7ace480869ac9a4ff65c6da17d9fd2 to your computer and use it in GitHub Desktop.
ESP8266 Automatic Modem Wifi Reset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple sketch to reset your cable modem if the internet connection dies | |
* (C) 2022 Thomas Reidemeister | |
* Tested on ESP8266 NodeMCU board | |
* | |
* [GPIO]->[transistor-driven 5V Relay]->[Cable Modem power cable] | |
*/ | |
#include <Pinger.h> | |
#include <ESP8266WiFi.h> | |
extern "C" | |
{ | |
#include <lwip/icmp.h> // needed for icmp packet definitions | |
} | |
#define RELAY 2 ///< Define blinking LED pin | |
const char* ssid = "<your SSID>"; // Your ssid | |
const char* password = "<your Wifi password>"; // Your Password | |
volatile bool b_reset = false; | |
Pinger pinger; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(RELAY, OUTPUT); // Initialize the LED pin as an output | |
digitalWrite(RELAY, LOW); | |
Serial.begin(9600); | |
Serial.println("Initializing"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
pinger.OnEnd([](const PingerResponse& response) | |
{ | |
return true; | |
}); | |
pinger.OnReceive([](const PingerResponse& response) | |
{ | |
if (!response.ReceivedResponse) { | |
Serial.println("Request timed out... resetting"); | |
b_reset = true; | |
return false; | |
} | |
// Return true to continue the ping sequence. | |
// If current event returns false, the ping sequence is interrupted. | |
return true; | |
}); | |
} | |
void loop() { | |
Serial.println("Before Ping"); | |
if(pinger.Ping(IPAddress(8,8,8,8)) == false) | |
{ | |
Serial.println("Request failed... resetting"); | |
} else { | |
Serial.println("Request succeeded"); | |
} | |
delay(1000); | |
if(b_reset) { | |
b_reset = false; | |
digitalWrite(RELAY, HIGH); | |
delay(5000); | |
digitalWrite(RELAY, LOW); | |
} | |
delay(79000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment