Search

arduino visual ICMP ping multiple server monitor (ICMP echo request)

arduino visual ICMP ping multiple server monitor (ICMP echo request)

This post is a version of my previous post with the option of multiple IP addresses to be pinged.

It was requested by one of the people that commented on my blog, Leandro.

I hope it is useful.

/*
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

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[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
#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[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
int numAddresses = sizeof(pingAddr[0]);

SOCKET pingSocket = 0;

char buffer [256];

int delayMS = 1 * 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);
}
}

int i = 0;

void loop()
{
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);
}

Related posts

Leave a Comment