Search

arduino visual ICMP ping server monitor (ICMP echo request)

arduino visual ICMP ping server monitor (ICMP echo request)

Today I had a little issue with my server – it stopped responding for no apparent reason and with no warning.

When it finally became responsive I asked myself “how can I know when my server is down quickly?”

The idea for this post was born – use my arduino to ping the server and show the status using LEDs.

After a little google search for arduino ping I found lots of references to the ping))) sensor, but that is not the type of ping I meant. Well done whoever came up with the name – you broke the internet.

Anyway, I digress. Back to the real issue.

I found a library for ICMP ping at Blake’s site – http://www.blake-foster.com/project.php?p=44
The link to the library itself is at http://www.blake-foster.com/projects/ICMPPing.zip

Install the library in the appropriate directory for your system (~/arduino/libraries in my case, your location will vary) and start the arduino software.

The following code will ping my server and alert me to the status via serial output and LED output.

Initially all three pins go high to signify ethernet attempting to connect.
Pin 2 will pulse when the arduino is pinging the server.
Pin 3 goes high when the server responds (it is up).
Pin 4 goes high when the server fails to respond (it – or your internet – is down).

The source for this script is available here
/*

* This code is the property of and remains the copyright of
* Gregory Fenton, http://labby.co.uk/2012/08/arduino-visual-icmp-ping-server-monitor-icmp-echo-request
* The code may be freely distributed and reused as long as this header is not altered or removed
* For commercial use of this code a fee of not less than $10 per hardware device the software runs on is payable for the usage rights
* Please contact Gregory Fenton at the above site should you wish to use this software in a commercial manner

Repeatedly ping a server 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

video in daughterboard

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[] = {91,121,5,142}; // 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
#include
#include

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // mac address for ethernet shield
byte ip[] = {192,168,0,177}; // ip address for ethernet shield
byte pingAddr[] = {91,121,5,142}; // ip address to ping

SOCKET pingSocket = 0;

char buffer [256];

int delayMS = 60 * 1000; // 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

void startPing()
{
digitalWrite(ledPing, HIGH);
}

void endPing()
{
digitalWrite(ledPing, LOW);
}

void pingSuccess()
{
digitalWrite(ledFail, LOW);
digitalWrite(ledOk, HIGH);
}

void pingFail()
{
digitalWrite(ledFail, HIGH);
digitalWrite(ledOk, LOW);
}
#endif

void setup()
{
#ifdef ledOut
pinMode(ledPing, OUTPUT);
pinMode(ledOk, OUTPUT);
pinMode(ledFail, 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);
}
}

void loop()
{
bool pingRet; // pingRet stores the ping() success (true/false)

#ifdef ledOut
startPing();
#endif
ICMPPing ping(pingSocket);
pingRet = ping(4, pingAddr, buffer);
#ifdef ledOut
delay(250);
endPing();
#endif

#ifdef serialOut
Serial.println(buffer);
#endif
#ifdef ledOut
if(pingRet) // Failure
pingSuccess();
else
pingFail();
#endif
delay(delayMS);
}

Related posts

Leave a Comment