Eduardo Zola
Published © GPL3+

netBOOT

Automatic Cable Modem Rebooter.

IntermediateFull instructions provided4 hours3,382
netBOOT

Things used in this project

Story

Read more

Schematics

netBoot alternative assembly

wire connections

Part: 1

Part: 2

Part: 3

Part: 4

Code

netBoot code version 1

Arduino
http://www.zolalab.com.br/eletronica_projetos/netboot.php
// by: Eduardo Zola - 2015 (www.zolalab.com.br) egzola@gmail.com

#include <SPI.h>

#include <Ethernet.h>

#include <ICMPPing.h>

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};

EthernetClient client;

#define MAX_IPs 5

IPAddress pingAddrs[MAX_IPs];

int CNT = 0;

int ERRORS_COUNT = 0;

SOCKET pingSocket = 0;

char buffer [256];

ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup()

{

  pinMode(9, OUTPUT); // relay Pin
  digitalWrite(9, HIGH);

  Serial.begin(9600);

  Serial.println("Iniciando...");

  delay(2000);

  pingAddrs[0] = IPAddress(74, 125, 26, 147); // google.com

  pingAddrs[1] = IPAddress(8, 8, 8, 8); // dns google

  pingAddrs[2] = IPAddress(200, 221, 2, 45); // uol

  pingAddrs[3] = IPAddress(23, 216, 170, 96); // microsoft

  pingAddrs[4] = IPAddress(31, 13, 73, 1); // facebook

  delay(3000);

  while (Ethernet.begin(mac) == 0) Serial.println("Failed to configure Ethernet using DHCP");

  Serial.print("My IP address: ");

  for (byte thisByte = 0; thisByte < 4; thisByte++)

  {

    Serial.print(Ethernet.localIP()[thisByte], DEC);

    Serial.print(".");

  }

  Serial.println();

}

void loop()

{

  if (ERRORS_COUNT >= 3)

  {

    ERRORS_COUNT = 0;

    Serial.println("BOOT");

    boot();

  }

  if (CNT >= MAX_IPs) CNT = 0;

  ICMPEchoReply echoReply = ping(pingAddrs[CNT], 4);

  if (echoReply.status == SUCCESS)

  {

    ERRORS_COUNT = 0;

    sprintf(buffer,

            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",

            echoReply.data.seq,

            echoReply.addr[0],

            echoReply.addr[1],

            echoReply.addr[2],

            echoReply.addr[3],

            REQ_DATASIZE,

            millis() - echoReply.data.time,

            echoReply.ttl);

  }

  else

  {

    sprintf(buffer, "Echo request failed; %d", echoReply.status);

    ERRORS_COUNT ++;

  }

  Serial.println(buffer);

  delay(1000);

  CNT ++;

}

void boot()

{

  digitalWrite(9, LOW); // desliga o modem, por tanto, o relay deve ser usado em NO, ao inves de NC... ou seja, o relay desligado permite que o router fique ligado. Ao acionar o relay, a energia do router eh cortada.

  delay(5000); // aguarda 5 segundos com o modem desligado

  digitalWrite(9, HIGH);

  delay(60 * 1000 * 5); // aguarda 5 minutos para o modem inicializar e reconectar, evitando que a rotina o desligue novamente.

}

netBoot code version 2 (with display)

Arduino
#include <SPI.h>
#include <UTFT.h>
#include <Ethernet.h>
#include <ICMPPing.h>

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};

EthernetClient client;


#define MAX_IPs 5
IPAddress pingAddrs[MAX_IPs];
int CNT = 0;
int ERRORS_COUNT = 0;

SOCKET pingSocket = 0;
 
char buffer [256];

ICMPPing ping(pingSocket, (uint16_t)random(0, 255));


extern uint8_t SmallFont[];
UTFT myGLCD(UNO_26,A2,A1,A3,A4);   //USE  UNO_26 FOR ST7787/7781 / UNO_24 FOR SPFD4508A

int Y = 0;

void setup() 
{
   // Setup the LCD
   pinMode(A0,OUTPUT);    // for the UNO_SHIELD_1IN1
   digitalWrite(A0,HIGH);  // the RD pin must be set high
   myGLCD.InitLCD();
   myGLCD.clrScr();
  
   pinMode(9,OUTPUT);
   digitalWrite(9,HIGH);

   pinMode(8,OUTPUT);
   digitalWrite(8,LOW);

     
   Serial.begin(9600);

   Serial.println("Initing...");

   Y = 20;
   
   myGLCD.setColor(0, 0, 180);
   myGLCD.fillRect(0, 0, 319, 14);
   myGLCD.setBackColor(0, 0, 180);
   myGLCD.setColor(255, 255, 255);
   myGLCD.setFont(SmallFont);
   myGLCD.print("netBOOT - Zola Lab", CENTER, 2);
   myGLCD.setBackColor(0, 0, 0);
   myGLCD.setColor(255, 255, 255);
   myGLCD.print("Initing...", LEFT, Y);
   
   Y += 20;
  
   delay(2000); // mantem 2 segundos com a ethernet desligada (reseta a ethernet)
   digitalWrite(8,HIGH);
   delay(2000); // mantem 2 segundos com a ethernet ligada, antes de executar ethernet.begin


  

   pingAddrs[0] = IPAddress(74,125,26,147); // google.com
   pingAddrs[1] = IPAddress(8,8,8,8); // dns google
   pingAddrs[2] = IPAddress(200,221,2,45); // uol
   pingAddrs[3] = IPAddress(23,216,170,96); // microsoft
   pingAddrs[4] = IPAddress(31,13,73,1); // facebook

   Y += 20;
   
   delay(3000);
   while(Ethernet.begin(mac)==0) {Serial.println("Failed to configure Ethernet using DHCP");myGLCD.print("Failed to configure Ethernet using DHCP", LEFT, Y);};

   Clear();
   printIP();
}

void loop() 
{
  if(ERRORS_COUNT >= 3)
  {
      ERRORS_COUNT = 0;

      boot(); 
  }
  
  if(CNT >= MAX_IPs) CNT = 0;

  ICMPEchoReply echoReply = ping(pingAddrs[CNT], 4);
  
  if(echoReply.status == SUCCESS)
  {
     ERRORS_COUNT = 0;
     
     sprintf(buffer,
            "IP: %d.%d.%d.%d    TIME: %ldms",
//            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
//            REQ_DATASIZE,
            millis() - echoReply.data.time
//            echoReply.ttl
            );
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
    ERRORS_COUNT ++;
  }
  Serial.println(buffer);
  myGLCD.print(buffer, LEFT, Y);  

  Y += 20;
  
  if(Y >= 240)  Clear();
    
  delay(1000);  
  CNT ++;  
}


void boot()
{
   Clear();
   Serial.println("BOOT");      
   myGLCD.print("BOOT", LEFT, Y);   
   Y+=20;
      
   digitalWrite(9,LOW);  // desliga o modem, por tanto, o relay deve ser usado em NO, ao inves de NC...  ou seja, o relay desligado permite que o router fique ligado. Ao acionar o relay, a energia do router eh cortada.
   delay(5000);  // aguarda 5 segundos com o modem desligado
   digitalWrite(9,HIGH);
   
   for(int j=0;j<150;j++) {myGLCD.print(".", j*2, Y);delay(2500);};
//   delay(60*1000*5); // aguarda 5 minutos para o modem inicializar e reconectar, evitando que a rotina o desligue novamente.

   Y+=20;
   while(Ethernet.begin(mac)==0) {Serial.println("Failed to configure Ethernet using DHCP");myGLCD.print("Failed to configure Ethernet using DHCP", LEFT, Y);};

   Y+=20;
   printIP();
}

void Clear()
{
     Y = 20;
     myGLCD.setColor(0, 0, 0);
     myGLCD.setBackColor(0, 0, 0);
     myGLCD.fillRect(0, 20, 319, 240);
     myGLCD.setColor(255, 255, 255);
}


void printIP()
{
   Serial.print("My IP address: ");
   myGLCD.print("My IP address: ", LEFT, Y);
   
   String IP = "";
   for (byte thisByte = 0; thisByte < 4; thisByte++) 
   {
      int ip;
      ip = Ethernet.localIP()[thisByte];
      IP = IP + String(ip) + ".";
   }
   IP = IP.substring(0,IP.length()-1);
   IP = IP + "   ";
   Serial.println(IP);
   myGLCD.print(IP, RIGHT, Y);
   Y+=20;   
}

Credits

Eduardo Zola

Eduardo Zola

2 projects • 3 followers
BSc computer science, Tinkerer in electronic, Reverse Engineering, C and Assembly programmer. Or just a Maker ;-)

Comments