ok so im trying to write some code which basically pings a server and waits for a responce.
ive got something from a code libary which works fine.
however, i need my code to wait for x amount of failed pings or x amount of time then run another function.
im using the IMCPPing.h libary
here is what i have so far
#include <SPI.h>
#include <Ethernet.h> // call ethernet module
#include <ICMPPing.h> // call ping module
#include <LiquidCrystal.h> // call lcd module
LiquidCrystal lcd(12,11,5,4,3,2); // tell lcd module where pins being
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set mac address for ethernet shield
byte ip[] = {192,168,2,177}; // set desired ip address for shield
IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
int const plugRelay =8;
SOCKET pingSocket = 0;
char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
void setup()
{
Ethernet.begin(mac, ip); // start Ethernet
lcd.begin(16,2); // start lcd
Serial.begin(9600); //
lcd.setCursor(0,0); // set lcd to print to line 1
lcd.print(" Power-Ping "); // message to print
// delay(5000); // delay for message
lcd.clear(); // clear lcd
lcd.print("Power | ON"); // print
lcd.setCursor(0,0); // let lcd to print to line 2
lcd.print("Ping "); // print
}
void loop()
{
ICMPEchoReply echoReply = ping(pingAddr, 4); // start ping
if (echoReply.status == SUCCESS) // advise of success
{
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 // if no success the report fail message
{
sprintf(buffer, "Echo request failed; %d", echoReply.status); // failed message
}
Serial.println(buffer);
delay(500); // 0.5 sec delay
}
now this is what i think i need to is change the loop to the following but im not 100% if this is going to work
what should happen is the code should check for 3 errors, then
change the state of a input_pullup
ICMPEchoReply echoReply = ping(pingAddr, 4); // start ping
if (echoReply.status == SUCCESS) // advise of success
{
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 // if no success the report fail message
{
for(int j = 1; j < 3; j++) // if ping fails 3times the change state of relay
{
sprintf(buffer, "Echo request failed; %d", echoReply.status); // failed message
}
Serial.println(buffer);
delay(500); // 0.5 sec delay
pinMode(plugRelay, INPUT_PULLUP);
}
thanks and hopefull this makes sence