Serial Monitor ?

so today ive recieved my W5100 ethernet sheild.

ive plugged in my project which basically just pings google, if there is a responce it keeps checking,

if the responce fails, then it checks x amount of times, if after x amount of times the ping is still not responding it turns a relay low, waits x amount of seconds then brings the relay up. another delay then trys to ping and go back to the start

here is my code

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(22,24,26,28,30,32);      // tell lcd what pins are being used to control lcd
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set mac address for ethernet port
    byte ip[] = {192,168,2,177}; // set desired ip address ethernet port
    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    const int plugRelay = 7;
    boolean pingSuccess = true;
    SOCKET pingSocket = 0;
    const int maxNumberOfTimesToTryToPing = 20; 
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
      digitalWrite(plugRelay, HIGH);
      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

    }
     
void loop()
{
   pingSuccess = true;
   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
         pingSuccess = false;
         // Do whatever you need to with the reply data
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping          OK"); // print
         break; // skip the rest of the iterations of the for loop
      }
   }

   if(!pingSuccess)
   {
   lcd.print("PING FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
   delay(2000);
   lcd.print("Restart in 10sec");
   delay(10000);
   digitalWrite(plugRelay, LOW);
   lcd.print("Successful");
   delay(2000);
   lcd.print("Power on in 10secs");
   delay(10000);
   digitalWrite(plugRelay, HIGH);
   }
}

can anyone see anything wrong with it,

also is there a way to test if it is actually pinging and getting a responce from the serial monitor ?

Regards

can anyone see anything wrong with it,

Yes. That is not likely the MAC address of your shield.

also is there a way to test if it is actually pinging and getting a responce from the serial monitor ?

The hardware on the other end (google's server) responds, or not, to the ping request. You don't "get a responce from the serial monitor".

If you meant "Can I display the response, or lack thereof, on the Serial Monitor application instead of (or in addition to) the LCD?", the answer is yes.

Hi Paul, Thanks for the swift reply.

how would i go about finding my mac address of the shield ? do i need to delare the mac address ?

as for the serial monitor output ive been trying with the following code and other variations of it, can you just let me know if im on the right track.

Serial.print(echoReply.addr[0]) 
Serial.print(echoReply.ttl)

thank you.

PS:

the arduino never come with a sticker with the mac address on.

how would i go about finding my mac address of the shield ?

My ethernet shield had a sticker on it with the MAC address printed on it.

as for the serial monitor output ive been trying with the following code and other variations of it, can you just let me know if im on the right track.

Just change lcd.print() to Serial.println() everywhere (or copy and paste every lcd.print() statement and change the copy to Serial.println()).

so this is the my code which has been changed

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(22,24,26,28,30,32);      // tell lcd what pins are being used to control lcd
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xED}; // set mac address for ethernet port
    byte ip[] = {192,168,2,177}; // set desired ip address ethernet port
    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    const int plugRelay = 34;
    boolean pingSuccess = true;
    SOCKET pingSocket = 0;
    const int maxNumberOfTimesToTryToPing = 5; 
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
      digitalWrite(plugRelay, HIGH);
      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
Serial.println("POWER UP // DISPLAY WELCOME MESSAGE");  // print to serial monitor
      lcd.print("   Power-Ping   ");  // message to print
      delay(5000);  // delay for message
      lcd.clear();  // clear lcd
      lcd.print("Power    |    ON"); // print

    }
     
void loop()
{
   pingSuccess = true;
   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
 //     Serial.println("PING SUCCESSFULL");  // print to serial monitor
      {
         pingSuccess = false;
         // Do whatever you need to with the reply data
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping          OK"); // print
         break; // skip the rest of the iterations of the for loop
      }
   }

   if(!pingSuccess)
   {
   lcd.print("PING FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
   delay(2000);
   lcd.print("Restart in 10sec");
   delay(10000);
   digitalWrite(plugRelay, LOW);
   lcd.print("Successful");
   delay(2000);
   lcd.print("Power on in 10secs");
   delay(10000);
   digitalWrite(plugRelay, HIGH);
   }
}

as far as i can tell its still not pinging. would this now be down to the mac address being wrong ?

The mac adress isn't important, as long as you're only using one shield. I normally just use the mac adress from the examples. As long as the mac dress is unique you're good ;)

as far as i can tell its still not pinging. would this now be down to the mac address being wrong ?

No. As long as the MAC address is unique on your network, it will work.

What output do you get in the Serial Monitor?

     if (echoReply.status == SUCCESS)   // advise of success
 //     Serial.println("PING SUCCESSFULL");  // print to serial monitor
      {

That line should be uncommented and should be AFTER the {.

works a treat, i did notice after i had posted that it was commented out..

also i could not work out why it was not pinging, i had stupidly set the network address incorrect.

PaulS

thank you ever so much for your help and support. :D

sorry PaulS

i just have one more question if thats ok,

ok so this bit of code here the void loop()

void loop()
{
   pingSuccess = true;
   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
//     Serial.println("PING SUCCESSFULL");  // print to serial monitor
        pingSuccess = false;
         // Do whatever you need to with the reply data
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping          OK"); // print
         break; // skip the rest of the iterations of the for loop
      }
   }

   if(!pingSuccess)
   {
   lcd.print("PING FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
    }
}

ok so if im not mistaken this code dose the following

sets boolean to true
gets the number of requests it should try to ping (5)

if the ping is successful then try to ping again
if the ping fails it should then goto

   if(!pingSuccess)
   {
   lcd.print("PING FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
    }

then this bit of code should display a message on LCD saying FAILED

im only asking because when i remove the ethernet cable it dose not do anything (NO FAILED MESSAGE)

is that correct ?

is that correct ?

The terminology is wrong, but the intent is correct.

It only keeps trying to ping if the current ping fails. If the first ping works, it only pings once. If the first ping fails, but the second succeeds, pinging stops. You only see the failed message if none of the ping attempts succeeds.

i thought because it was in the void loop() it runs continually?

therefor if the ping fails then the lcd reports failed ?

i thought because it was in the void loop() it runs continually?

Yes. It will keep repeating the whole "try 5 times" operation.

therefor if the ping fails then the lcd reports failed ?

Yes.

it dont seem to work for some reason, is there anything you can see in my code which would force it not to work ?

Regards

it dont seem to work for some reason, is there anything you can see in my code which would force it not to work ?

It does something. It prints something to the serial monitor. I can’t see what it does, or outputs.

sorry i ment from my inital code ive posted.

as for the serial monitor…

when it pings ive set it to log to the serial monitor… every 2 seconds… (will this fill up any memory logging ?)

ok so ive just added a serial.printin for the failed section and this is the log from the monitor

 POWER UP // DISPLAY WELCOME MESSAGE
PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT
PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART
PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT
PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART
PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT
PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART

this is my full code

    #include <SPI.h> 
    #include <Ethernet.h>  // call ethernet module
    #include <ICMPPing.h> // call ping module
    #include <LiquidCrystal.h> // call lcd module
    LiquidCrystal lcd(22,24,26,28,30,32);      // tell lcd what pins are being used to control lcd
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xED}; // set mac address for ethernet port
    byte ip[] = {192,168,1,177}; // set desired ip address ethernet port
//.    IPAddress pingAddr(216,58,208,78); // ip address to ping (google.com)
    IPAddress pingAddr(192,168,1,199); // ip address to ping (google.com)
    const int plugRelay = 34;
    boolean pingSuccess = true;
    SOCKET pingSocket = 0;
    const int maxNumberOfTimesToTryToPing = 1; 
    char buffer [256];
    ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
     
    void setup()
    {
      digitalWrite(plugRelay, HIGH);
      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
Serial.println("POWER UP // DISPLAY WELCOME MESSAGE");  // print to serial monitor
      lcd.print("   Power-Ping   ");  // message to print
      delay(5000);  // delay for message
      lcd.clear();  // clear lcd
      lcd.print("Power    |    ON"); // print

    }
     
void loop()
{
   pingSuccess = true;
   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
     Serial.println("PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT");  // print to serial monitor
        pingSuccess = false;
         // Do whatever you need to with the reply data
      lcd.setCursor(0,1);  // let lcd to print to line 2
      lcd.print("Ping          OK"); // print
      delay(2000);
      break; // skip the rest of the iterations of the for loop
      }
   }

   if(!pingSuccess)
   {
   Serial.println("PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART");  // print to serial monitor   
   lcd.setCursor(0,1);
   lcd.print("PING FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
    }
}

ive read threw the code several times and to me everything looks fine…

this is why i asked if you could spot anything as your a pro… i was hopinh it was something stupid i had done…

also should my boolean be set to

    boolean pingSuccess = true;

** ive tried it with true and false and it makes no difference .

Regards.

    IPAddress pingAddr(192,168,1,199); // ip address to ping (google.com)

No that is not google's IP address.

If the ping was successful, why are you setting pingSuccess to false? That makes NO sense.

ive changed the ip address to my internal pc one. so i can simulate the internet connection going offline.

i thought the boolean as default was set to false. when a successful ping happens it sets the boolean to true.....

i thought the boolean as default was set to false.

It is. But, then you set it to true:

   pingSuccess = true;

Then, you have:

   for(int p=0; p<maxNumberOfTimesToTryToPing; p++)
   {
      ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
      if (echoReply.status == SUCCESS)   // advise of success
      {
     Serial.println("PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT");  // print to serial monitor
        pingSuccess = false;

So, if the ping IS successful, you set pingSucceed to false.

You should be setting pingSuccess to false initially, and then set it to true when a ping succeeds.

Later, the if statement says if(!pingSuccess) which means “if the ping was NOT successful…”.

ive just fixed the issue.

Thanks once again PaulS

Vauled member of Arduino Community