Automated router restarting

Some time ago I posted about my ISP replacing my old fiber modem, with a new one, with a build in router. A quite buggy router, even. I have been calling them a few times, and they are now monitoring the problem, because now it just completely stops, about once daily. Most of the times, just after I have left the house, so my server can’t be reached, and it can’t reach out to anything either. So now, I am going to fix a problem that should not even exist, until I get my ISP to replace my router-fiber box, with just a fiber box. Then I will use my own Mikrotik router, and hopefully everything will then run smoothly again.

Before making this, I actually had another system in place, to do the same, just with some cheap 433Mhz wireless modules instead. The problem with those modules was that the connection to them was not as stable as anyone would have hoped, and they then sometimes failed to turn the modem off, or back on again. With everything in the same unit, and with the code being a bit smarter, it will hopefully work a lot better.

The hardware I am going to use for this, is one of the Smart Sockets from ITEAD I received yesterday, by flashing it with another firmware.

In the tests I have done so far, it does exactly what I expect it to do, and hopefully it will continue to do so next time the router decides to take a break.

Here is an example of a cold boot, where the power is turned on, and the relay inside the Smart Socket is turning the router on and starts connecting, while waiting for the router to get a connection.

In earlier projects, I have been using NodeMCU, but this time I am going to try flashing the ESP8266 inside the Smart Socket with Arduino instead.

Jumping right into the code, and here it is…

// WiFi info
const char* ssid = "SSID";
const char* password = "WPAKEY";


// Startup variables
#define startDelay 120000 // Giving the router 2 minutes to start up


// Pin configuration
#define relayPin 12
#define ledPin 13


// Server to open a connection to
char serverAdr[] = "www.google.com";
#define serverPort 80
#define interval 300000 // Try to connect every 5 minutes


// Library
#include <ESP8266WiFi.h>


WiFiClient wifiClient;

unsigned long prevTime = 0;
unsigned long healthLed = 0;
unsigned long routerReset = 0;

void setup()
{
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  // Turn the relay on
  digitalWrite(relayPin, HIGH);
  
  Serial.begin(9600);
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(250);
    Serial.print(".");
    
    digitalWrite(ledPin, !digitalRead(ledPin)); // Rapid flashing of the led to show we are trying to connect

    if(millis() > startDelay && routerReset == 0) // Wait 120 seconds for the router to start up and hand out an ip
    {
      Serial.println("WiFi connection failed, restarting.");
      routerReset = 1;
    }

    routerResetRoutine();
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop()
{
  // WiFi connection lost while running, reset to get back to the startup routine
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("WiFi connection lost, restarting.");
    ESP.restart();
  }
  
  if (millis() - prevTime >= interval) // Try to contact the server when time has run out
  {
    prevTime = millis();
    httpGET();
  }

  if (millis() - healthLed >= 2000) // Slow flashing of the led to show we are connected and monitoring
  {
    healthLed = millis();
    digitalWrite(ledPin, !digitalRead(ledPin));
  }

  routerResetRoutine();
}

void httpGET()
{
  Serial.println("\nStarting connection to server.");

  Serial.print("Connection: ");  
  if (wifiClient.connect(serverAdr, serverPort))
  {
    Serial.println("OK");
    wifiClient.stop();
  }
  else
  {
    Serial.println("FAILED");
    routerReset = 1;
  }
}

void routerResetRoutine()
{
  if (routerReset == 1)
  {
    Serial.println("Switching router off");
    routerReset = millis();
    digitalWrite(relayPin, LOW);
  }

  if (millis() - routerReset >= 10000 && routerReset > 1)
  {
    Serial.println("Restarting...");
    ESP.restart();
  }
}

All that should be needed to configure to adapt this to another setup, should be the SSID and code for it.

28 Comments

Add a Comment

Your email address will not be published. Required fields are marked *