Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

ICMP Ping Library for Arduino
Author:  Blake Foster
Contact: https://blake-foster.com/contact.php


Navigation


History

ReleaseDateChanges
1.02010-09-30Initial Release
2.02013-04-17API redesigned, minor bug fixes

Description

ICMP Ping library for the Arduino.


Requirements

This library works with the Arduino Ethernet shield. I have tested it with the Arduino Duemilanove, but it should work with any model that is compatible with the Ethernet shield.


Setup

  • Download the zip file or clone the repo
  • Copy the icmp_ping directory into the libraries directory of your Arduino folder.

Usage

The basic steps for using this library are:

  • Import the heder ICMPPing.h
  • Import Ethernet.h
  • Start Ethernet
  • Create an instance of ICMPPing
  • Use one of the two overloaded functional-call operators to send and receive an ICMPEcho.

For documentation of all classes and methods, see ICMPPing.h


Example

  1. /*
  2. Ping Example
  3. This example sends an ICMP pings every 500 milliseconds, sends the human-readable
  4. result over the serial port.
  5.  
  6. Circuit:
  7. * Ethernet shield attached to pins 10, 11, 12, 13
  8. created 30 Sep 2010
  9. by Blake Foster
  10. */
  11.  
  12. #include <SPI.h>
  13. #include <Ethernet.h>
  14. #include <ICMPPing.h>
  15.  
  16. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
  17. byte ip[] = {192,168,2,177}; // ip address for ethernet shield
  18. IPAddress pingAddr(74,125,26,147); // ip address to ping
  19.  
  20. SOCKET pingSocket = 0;
  21.  
  22. char buffer [256];
  23. ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
  24.  
  25. void setup()
  26. {
  27.   // start Ethernet
  28.   Ethernet.begin(mac, ip);
  29.   Serial.begin(9600);
  30. }
  31.  
  32. void loop()
  33. {
  34.   ICMPEchoReply echoReply = ping(pingAddr, 4);
  35.   if (echoReply.status == SUCCESS)
  36.   {
  37.     sprintf(buffer,
  38.             "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
  39.             echoReply.data.seq,
  40.             echoReply.addr[0],
  41.             echoReply.addr[1],
  42.             echoReply.addr[2],
  43.             echoReply.addr[3],
  44.             REQ_DATASIZE,
  45.             millis() - echoReply.data.time,
  46.             echoReply.ttl);
  47.   }
  48.   else
  49.   {
  50.     sprintf(buffer, "Echo request failed; %d", echoReply.status);
  51.   }
  52.   Serial.println(buffer);
  53.   delay(500);
  54. }


Download

Download here:

Contribute

Project in Github