Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Created August 7, 2012 10:09
Show Gist options
  • Save arduinoboard/3284200 to your computer and use it in GitHub Desktop.
Save arduinoboard/3284200 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of 6413138333135171C0E0
/*
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[] = {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.
*/
#define lcdOut 1
#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
#ifdef lcdOut
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);
#endif
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 = 8;
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 lcdOut
lcd.begin(16, 2);
lcd.blink();
lcd.clear();
int x;
sprintf(buffer, " ");
for(x = 0; x < numAddresses; x++)
if(x < 9)
buffer[x] = '1' + x;
else
buffer[x]= 'A' + (x - 10);
//buffer[numAddresses] = 0;
lcd.print(buffer);
#endif
#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
#ifdef lcdOut
lcd.setCursor(i,1);
//delay(1000);
#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 lcdOut
if(pingRet) // Failure
lcd.print("U");
else
lcd.print("D");
#endif
#ifdef ledOut
if(pingRet) // Failure
pingSuccess();
else
pingFail();
#endif
i++;
if(i >= numAddresses)
{
i = 0;
//delay(delayMS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment