Help with pinging a website

Hello,

I am working on this project:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250344151

and need to ping a website to verify I have an active internet connection.

I have been using the web client example sketch as a start and it accomplishes what I need to do but with it performing a google search and returning the output it takes many seconds to return the message that it can connect.

Is there such a thing as a simple ping command for the ethernet shield? Thanks!

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

Serial.println("connecting...");

if (client.connect()) {
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:
;
}
}

Hi Chorob,

I think I heard people asking for an ICMP lib somewhere, but in the meantime you could reduce the HTTP operations, and just exit after a connect rather than reading the page, like...

if (client.connect()) {
// success
client.stop();
}
else
{
// no connection.
}

I have a similar home monitoring sketch that uses the internet to get time via HTTP. The call to check connectivity and even set the time via the DateTime library takes less than a second to complete.

Spinlock, Thanks for the help! This works great.

Now a new question. I am trying to input the milliseconds since the program has been running in my Tweet (or just changing the static Tweet to a counter for that matter). Looks like the twitter library only allows static tweets. Any ideas around that? I am kind of new to this stuff. Here is my code so far. I am using green and red LEDs right now to visually indicate status as well as the serial port.

Thanks again!

#include <Ethernet.h>
#include <Twitter.h>

// these are the LED definitions
int greenPin = 8; // Green LED connected to digital pin 11
int redPin = 9; // Red LED connected to digital pin 8

unsigned long time; // this defines the time variable

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 35 };
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

// this is the twitter definition
Twitter twitter("TwitterUserID:TwitterPassword");
char msg[] = "This is a Test. I am the Arduino DSL internet Watchdog!!";

void setup()
{
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(redPin, OUTPUT); // sets the digital pin as output

Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

}

void loop()
{
Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
Serial.println();
Serial.println("connecting to Twitter ...");
if (twitter.post(msg)) {
int status = twitter.wait();
if (status == 200) {
Serial.println("Tweet Successful!!.");
digitalWrite(redPin, LOW); // sets the red LED off
digitalWrite(greenPin, HIGH); // sets the Green LED on
client.stop();
delay(300000); // delays restart of program after success for 5 minutes
}
}
} else {
Serial.println("connection failed");
digitalWrite(greenPin, LOW); // sets the Green LED off
digitalWrite(redPin, HIGH); // sets the red LED on
// waits for half a second
client.stop();
delay(300000); // delays restart of the program after failure for 5 minutes
}
}

sprintf may be a good thing for this specific task:

char buffer[512];
sprintf(buffer, "It has been %ld milliseconds since I started my arduino.", millis());
 if (twitter.post(buffer)) {
...etc.

You can look up sprintf (MS has a good developer article http://msdn.microsoft.com/en-us/library/ybk95axf(VS.71).aspx. Check our format specification to see how to change the millis() to what you want.

Check out the DateTime library too - it can help keep date/time, although you are going to want to set it to something proper first. (Arduino Playground - DateTime)

Thanks for the help!

I have been making some good progress but just ran into a snag. Here is my setup:

-An arduino
-an official ethernet shield

  • three LEDs (Red, Green, White)

The Arduino checks if it can connect to google to verify the internet connection to my house. While checking the connection the white LED is on.

If it confirms that it has an internet connection it twitters "the house is online" then turns the green LED on.

If it cannot connect to the internet it turns the red LED on.

It checks the internet connection every 2.5 minutes.

My ultimate goal is to automatically reset the internet modem if it verifies there is no connection to the internet. I have had many issues with needing to reboot the router every 2-3 days.

The code below was working wonderfully for the last 3 days but as of today it cannot connect to the internet. I even took the unit over to my neighbors and tried it on his internet connection with zero luck.

I have also tried commenting out the twitter functionality to see if it was causing the issue. No luck.

Any ideas? Thanks!

#include <Ethernet.h>
#include <Twitter.h>

// these are the LED definitions
int greenPin = 8; // Green LED connected to digital pin 8. indicates intenet is working
int redPin = 9; // Red LED connected to digital pin 9. indicates internet is not working
int whitePin = 7; // white LED connected to digital pin 7. indicates arduino is testing the internet connection

unsigned long time; // this defines the time variable

byte mac[] = { 0xAE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 32 };
// byte gateway[] = { 192, 168, 0, 1 };
// byte subnet[] = { 255, 255, 0, 0 };

byte server[] = { 64, 233, 187, 99 }; // Google
// byte server[] = { 69, 147, 76, 15 }; // Yahoo

Client client(server, 80);

// this is the twitter definition
Twitter twitter("twitterusername:twitterpassword");
char msg[] = "This is the 13th Test. I am the Arduino DSL internet Watchdog!!"; // not currently using this, see below

void setup()
{
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(redPin, OUTPUT); // sets the digital pin as output
pinMode(whitePin, OUTPUT); // sets the digital pin as output
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
}

void loop()
{
digitalWrite(whitePin, HIGH); // Sets the white LED on
Serial.println();
Serial.println("Testing your Internet connection...");

if (client.connect()) {
Serial.println("You are connected to the Internet");
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
Serial.println("Connecting to Twitter...");
if (twitter.post("The house is online!")) {
int status = twitter.wait();
if (status == 200) {
Serial.println("Tweet Successful!!");
digitalWrite(redPin, LOW); // sets the red LED off
digitalWrite(greenPin, HIGH); // sets the Green LED on
client.stop();
digitalWrite(whitePin, LOW); // sets the white LED off
delay(150000); // delays restart of program after success for 2.5 minutes
}
}
} else {
Serial.println("You are not connected to the Internet!");
digitalWrite(greenPin, LOW); // sets the Green LED off
digitalWrite(redPin, HIGH); // sets the red LED on
delay(500); // waits for half a second
client.stop();
digitalWrite(whitePin, LOW); // sets the white led off
delay(150000); // delays restart of the program after failure for 2.5 minutes
}
}

Update:

I just uploaded the stock web client sketch and it too doesn't work. Is it possible I may have a hardware failure in either the Arduino or Ethernet shield?

Thanks again.

Cool Project!

I have been trying to find a way to detect when networked devices are awake instead of just the presence of an active internet link, in-order to control the power to other devices.

There does not seem to be an easy way to ping (ICMP) which would seem the obvious solution. The post above connects to devices through port 80 how can I detect a device if the TCP/UDP ports are not available? (As appears to happen with computers behind a firewall or not hosting any services...)