Skip to content

Instantly share code, notes, and snippets.

@biskandar
Created October 10, 2012 13:29
Show Gist options
  • Save biskandar/3865654 to your computer and use it in GitHub Desktop.
Save biskandar/3865654 to your computer and use it in GitHub Desktop.
Make your arduino and his best ethernet friend talk to the world 2012101001
#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
// prepare for tcp connection
byte mac[] = { 0x00, 0x08, 0xDC, 0x00, 0x00, 0x09 } ;
// prepare for the ping
SOCKET socket = 0 ;
int pingTimeout = 4 ;
byte destIp[] = { 74 , 125 , 235 , 14 } ; // google.com
char strLineResult[256] ;
void setup() {
// the program start when the serial debug terminal connected
Serial.begin( 9600 ) ;
while ( !Serial ) ;
// initialized the tcp connection, the ethernet shield
// shall got the local ip from dhcp server
Serial.print( "Initializing... " ) ;
delay( 2000 ) ;
if ( !Ethernet.begin( mac ) ) {
Serial.println( "FAILED" ) ;
while ( true ) ;
}
Serial.println( "OK" ) ;
// validate by display the local ip on board
Serial.print( "Local IP : " ) ;
delay( 2000 ) ;
Serial.println( Ethernet.localIP() ) ;
// display the remote ip ( debug purpose )
Serial.print( "Remote IP : " ) ;
delay( 2000 ) ;
Serial.print( destIp[0] ) ;
Serial.print( "." ) ;
Serial.print( destIp[1] ) ;
Serial.print( "." ) ;
Serial.print( destIp[2] ) ;
Serial.print( "." ) ;
Serial.print( destIp[3] ) ;
Serial.println() ;
}
void loop() {
// build a ping object with the defined socket handler
ICMPPing ping( socket ) ;
// try to ping the destination ip of the google.com
ping( pingTimeout , destIp , strLineResult ) ;
// the ping result will store into the string line result
// display it on the console terminal
Serial.println( strLineResult ) ;
// ping every one second until die die... :-P
delay( 1000 ) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment