// 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.
 }