Sure, I found code on the Internet that I use and modify to my needs:
/*
Repeatedly ping a number of servers and output the response to serial and pins 2,3,4
pin 2: ping request sent
pin 3: ping response received
pin 4: ping response not received
This software requires the ICMPPing library, available at
http://www.blake-foster.com/projects/ICMPPing.zip
Portions of this code were derived from code available at
http://www.blake-foster.com/project.php?p=44
My thanks go to Blake for his hard work.
If you do not wish to output to the serial port, comment out the line
#define serialOut 1
If you do not wish to output to the LEDs, comment out the line
#define ledOut 1
The lines
byte ip[] = {192,168,0,177}; // ip address for ethernet shield
byte pingAddr[8][4] = { {91,121,5,142}, {91,121,5,143}, {91,121,5,144}, {91,121,5,145}, {91,121,5,146}, {91,121,5,147}, {91,121,5,148}, {91,121,5,149} }; // ip address to ping
need to be changed to suit your own needs. If your network has DHCP the arduino
will automatically grab an IP address for itself.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // mac address for ethernet shield
byte ip[] = {172,16,0,60}; // ip address for ethernet shield
byte pingAddr[1][4] = { {172,16,0,112}};
int numAddresses = 1; //sizeof(pingAddr[0]);
bool deviceFound = false;
int lampCountdown = 10;
SOCKET pingSocket = 0;
char buffer [256];
int delayMS = 1 * 1000; // check every 1 seconds (delay between successive pings (60 * 1000 = 60 seconds))
#define serialOut 1
#define ledOut 1
#ifdef ledOut
#define ledPing 2
#define ledOk 3
#define ledFail 4
#define lamp 5
void startPing()
{
digitalWrite(ledPing, HIGH);
}
void endPing()
{
digitalWrite(ledPing, LOW);
}
void pingSuccess()
{
deviceFound = true;
}
void pingFail()
{
deviceFound = false;
}
#endif
void setup()
{
#ifdef ledOut
pinMode(ledPing, OUTPUT);
pinMode(ledOk, OUTPUT);
pinMode(ledFail, OUTPUT);
pinMode(lamp, OUTPUT);
// initialising, turn all LEDs on
digitalWrite(ledFail, HIGH);
digitalWrite(ledOk, HIGH);
digitalWrite(ledPing, HIGH);
#endif
#ifdef serialOut
// start serial port:
Serial.begin(9600);
Serial.println("Starting ethernet connection");
#endif
// start Ethernet
if (Ethernet.begin(mac) == 0) {
#ifdef serialOut
Serial.println("Failed to configure Ethernet using DHCP");
#endif
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}
int i = 0;
void loop()
{
pingDevice();
if(deviceFound==true){
lampCountdown=10;
digitalWrite(ledFail, LOW);
digitalWrite(ledOk, HIGH);
digitalWrite(lamp, HIGH);
}else{
if(lampCountdown<=0){
digitalWrite(lamp, LOW);
}else{
digitalWrite(ledFail, HIGH);
digitalWrite(ledOk, LOW);
Serial.print("Device not found: ");
Serial.println(lampCountdown);
lampCountdown--;
}
}
}
void pingDevice(){
bool pingRet; // pingRet stores the ping() success (true/false)
#ifdef ledOut
startPing();
#endif
ICMPPing ping(pingSocket);
byte pingAddr2[] = { pingAddr[i][0], pingAddr[i][1], pingAddr[i][2], pingAddr[i][3] };
pingRet = ping(4, pingAddr2, buffer);
#ifdef ledOut
delay(250);
endPing();
#endif
#ifdef serialOut
Serial.print("i:");
Serial.print(i);
Serial.print(" ");
Serial.println(buffer);
#endif
#ifdef ledOut
if(pingRet) // Failure
pingSuccess();
else
pingFail();
#endif
i++;
if(i >= numAddresses)
i = 0;
delay(delayMS);
}