ESP8266 Automatic Restart Modem or Router

Using the ESP8266 module to Restart Modem or Router Automatic. When internet connection loss. the attached program select for “Builtin LED” or “Serial Monitor” to show status.

Step 1: Parts List
  • ESP8266 ESP-01
  • 1 Channel 5V Relay Module
  • 2N2222 NPN transistor
  • 1k resistor
Step 2: Wiring

ESP8266 Auto Restart Router_Elec-Cafe

Step 3: Code

** Change “ssid” and “password” to your wifi network **

===============================

This Code use Builtin LED Status
#include <ESP8266WiFi.h>

const char* ssid     = "ssid"; // Your ssid
const char* password = "password"; // Your Password
const char* Hostname = "www.google.co.th";
IPAddress HostIP;
unsigned int localPort = 80;
int RELAY_Pin = 2; //Relay Pin

void setup() {
pinMode(RELAY_Pin, OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);

// Connecting to a WiFi network

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
digitalWrite(BUILTIN_LED, LOW);
delay(500);
digitalWrite(BUILTIN_LED, HIGH);
}}

void loop() {
if (!WiFi.hostByName(Hostname, HostIP)) {
digitalWrite(RELAY_Pin, HIGH);
delay(5000);
digitalWrite(RELAY_Pin, LOW);
}else{
digitalWrite(BUILTIN_LED, LOW);
delay(1000);
digitalWrite(BUILTIN_LED, HIGH);
}
delay(60000);
}

===============================

This Code use Serial Monitor Status
#include <ESP8266WiFi.h>

const char* ssid     = "ssid"; // Your ssid
const char* password = "password"; // Your Password
const char* Hostname = "www.google.co.th";
IPAddress HostIP;
unsigned int localPort = 80;
int RELAY_Pin = 2; //Relay Pin

void setup() {
pinMode(RELAY_Pin, OUTPUT);

Serial.begin(115200);
delay(10);

// Connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
}

void loop() {
if (!WiFi.hostByName(Hostname, HostIP)) {
Serial.println("");
Serial.println("Disconnected");
digitalWrite(RELAY_Pin, HIGH);
delay(5000);
digitalWrite(RELAY_Pin, LOW);
}else{
Serial.println("");
Serial.println("Connected");
}
delay(60000);
}