Ping my router with an arduino uno/ethernet shield

Dear all, I am using the example program from arduino ide to rewrite the code to ping my router at home. I have it ping and connect from the beginning, but then it will show fail connection if I restart the board by reset the reset pin to ground, or if I unplug the power and plug it it back into the Arduino Uno board.

Would there be a code to fix this problem? Please help. Thank you in advance!

#include <Ethernet.h>

#include <SPI.h>

#include <EEPROM.h>

#include <SoftwareSerial.h>

SoftwareSerial LCD(2,3); //communicate on pin 3

// Enter a MAC address and IP address for your controller below.

// The IP address will be dependent on your local network:

byte mac = { 0x91, 0xE2, 0xMA, 0x0D, 0x25, 0x4K };

byte ip = { 192, 168, 1, 105 };

byte gateway = { 192, 168, 1, 1 }; // internet access via router

byte subnet = { 255, 255, 255, 0 };

IPAddress ping(192, 168, 1, 1);

EthernetClient client;

void setup() {

// start the Ethernet connection:

Ethernet.begin(mac, ip);

// Open serial communications and wait for port to open:

LCD.begin(9600);

// give the Ethernet shield a second to initialize:

delay(500);

LCD.println(“CONNECTING…”);

delay(4000);

LCD.write(0xFE);

LCD.write(0x01);

// if you get a connection, report back via serial:

if (client.connect(ping,80)) {

LCD.println(“CONNECT”);

delay(4000);

LCD.write(0xFE);

LCD.write(0x01);

}

else {

// if you didn’t get a connection to the server:

LCD.println(“CONNECTION FAIL”);

delay(4000);

LCD.write(0xFE);

LCD.write(0x01);

}

}

void loop()

{

// if there are incoming bytes available

// from the server, read them and print them:

if (client.available()) {

char c = client.read();

LCD.print(c);

}

// as long as there are bytes in the serial queue,

// read them and send them out the socket if it’s open:

while (LCD.available() > 0) {

char inChar = LCD.read();

if (client.connected()) {

client.print(inChar);

}

}

// if the server’s disconnected, stop the client:

if (!client.connected()) {

LCD.println();

LCD.println(“DONE”);

delay(5000);

LCD.write(0xFE);

LCD.write(0x01);

client.stop();

// do nothing:

//while (true);

for(;;);

}

}

For the record, client.connect does not “ping”. It opens a connection. But it does so to the IP adres on port 80 of the router. So that implies to the HTML server on the router. “Pinging” is done with the ICMP protocol, and needs other functions/libraries. Even then, the router may or is likely configured to ignore ping requests.

// if you get a connection, report back via serial:
 if (client.connect(ping,80)) {

client.connect returns a number which indicates if it succeeded or what the reason was of failing. So storing that value into a variable (of type int should work) and printing that to the LCD or serial port to the PC would explain more about the cause of failing.

https://www.arduino.cc/en/Reference/ClientConnect

John_928:
byte mac = { 0x91, 0xE2, 0xMA, 0x0D, 0x25, 0x4K };

Those are not all valid hex digits. Only 0-9 and A-F are valid. I don't know if that is your only problem, but I'd definitely start there.

I am sorry that I don’t really know why I got that mac id. But, thank you very much for the help. However, I uploaded with the link from arduino.cc you gave me, but it is still not working. Therefore, I tested with my router, it works fine but for the google ip.

For the google link, it shows, “Connecting…” then…,“Connection failed”…,“Disconnecting”

Here is the upload:

#include <Ethernet.h>

#include <SPI.h>

byte mac = { 0xD4, 0xBE, 0xD9, 0x6F, 0x97, 0x2D };

//byte ip = { 192, 168, 1, 106 };

byte server = { 139, 130, 4, 5 }; // Google ip

//byte server = { 192, 168, 1, 1 }; //my router

EthernetClient client;

void setup()

{

Ethernet.begin(mac);

Serial.begin(9600);

delay(1000);

Serial.println(“connecting…”);

if (client.connect(server, 80)) {

Serial.println(“connected”);

client.println(“GET /search?q=arduino HTTP/1.0”);

client.println();

} else {

Serial.println(“connection failed”);

}

}

void loop()

{

if (client.available()) {

char c = client.read();

Serial.print(c);

}

if (!client.connected()) {

Serial.println();

Serial.println(“disconnecting.”);

client.stop();

for(;:wink:

;

}

}

139, 130, 4, 5 }; // Google ip

If I open this ip in my browser (Opera, IE 11, Chrome … doesn’t matter) then it does not load either. Where did you get that IP from? It is not the same as in the arduino.cc link that I gave.

I don’t know how you can ping in a Mac but in Windows type “ping www.google.com” in a DOS command window and take the IP adres that gets resolved in it. Try that in your code.

[EDIT] I guess not that different from Windows:

http://www.wikihow.com/Ping-on-Mac-OS

Valen, I got that ip from the google.com. How do I write the www.google.com into the code?

What I am trying is to make the board to ping and let me know on the LCD screen that the network is there when I am pinging it. Is it possible if there is a way to make the code run?

I have tried the top code but it works only at home on my router. It doesn’t work at all when I am trying it at work. Is there a way I can archive this matter?

Thank you

Valen, I got that ip from the google.com. How do I write the http://www.google.com into the code?

I think you should learn more about networking in general. Before you start writing code on an arduino to do it. If you don’t know the difference between pinging (a server) and requesting a HTML page, or how to resolve a domain name to an IP adres then you’ll only keep yourself confused as you don’t know what you are doing.

As I said before, you cannot “ping” with that code. It only does a HTML request to an IP adres that it assumes to be a webserver. (or that you imply it to be) If it is not behaving as a webserver, then the connection will fail.

If you want to ping a network device, which can only be done using the ICMP protocol, then you need a different library. I haven’t tested the code and library on the following link (as I don’t have a ethernetshield) but it should do what you want. The IP adress 74.125.26.147 in it replies when I ping it with Windows ping command. But I have no idea what server that is, or who operates it.

http://playground.arduino.cc/Code/ICMPPing

John_928:
…I have tried the top code but it works only at home on my router. It doesn’t work at all when I am trying it at work. Is there a way I can archive this matter?

Thank you

At work the hosts/servers/routers may have different IP adresses. There is probably nothing on the network that responds to that IP. The IT-manager there may be better able to explain why you don’t get a reply.

If you are serious about this, learn about basic networking first.

What do you mean with “archiving”? That does not make sense to me.

That IP (74.125.26.147) is for a nameserver at google : ns1.google.com

DanV:
That IP (74.125.26.147) is for a nameserver at google : ns1.google.com

Maybe it also acts as a DNS or nameserver, but it seems it is one of google’s web/html servers hosting the search engine frontend.

This link to the mxtoolbox site checks if it responds as a html webserver:

http://mxtoolbox.com/SuperTool.aspx?act … tworktools

If I ping to ns1.google.com I get a resolved IP adress 216.239.32.10. And that does not seem to respond to html requests in my browser.

P.S. Since Google is huge, it may depend on your location in the internet or world which ip addresses you get resolved to. Because of loadsharing.