Sketch to ping remote computer using Arduino (Using EtherShield Library and an ENC28J60 Shield)

Print Friendly, PDF & Email

Well over the past weekend I had been scouring the net trying to get a cue as to how to ping a remote computer using an Arduino Duemilanove and an ENC28J60 based shield (I am using SureElectronics Ethernet Communication Module USB SPI – ENC28J60 Remote Control).

SureElectronics Ethernet Shield

After hours of Wiresharking my code and scouring the Ethershield library for possible cues I was finally able to ping a remote computer

Without much ado here is a dump of the sketch. I have used Andy’s EtherShield library.
Caution: -To make this sketch work following changes are required in ip_arp_udp_tcp.c file in the Ethershield library.

Replace
buf[IP_TOTLEN_L_P]=0x82;//gives error when viewed in wireshark
By
buf[IP_TOTLEN_L_P]=0x54;
Replace
buf[IP_PROTO_P]=IP_PROTO_UDP_V;;//sends packet as UDP instead of ICMP
By
buf[IP_PROTO_P]=IP_PROTO_ICMP_V;

The stuff works, but it stops sending packets after 3rd iteration. I have not resolved the problem.(Solved. Problem was in variable type definition of  endtime. Had defined it as uint16_t instead of uint32_t.)

 

#include 
#define PING_client
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x25};
static uint8_t myip[4] = { 172,16,100,25 };
static uint8_t pcip[4] = { 172,16,100,2 };
static uint8_t gwip[4] = { 172,16,100,1 };
static uint8_t fooip[4] = { 172,16,100,100 }; //unavailable pc. simulating a offline pc

#define BUFFER_SIZE 700
static uint32_t timer,count;
static uint8_t buf[BUFFER_SIZE+1];
int state=0;
EtherShield es=EtherShield();
void setup(){
  Serial.begin(19200);
    // Initialise SPI interface
  es.ES_enc28j60SpiInit();

  // initialize enc28j60
  es.ES_enc28j60Init(mymac);

  // init the ethernet/ip layer:
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  Serial.println("Starting"); delay(3000);
}

// Output a ip address from buffer from startByte
void printIP( uint8_t *buf ) {
  for( int i = 0; i < 4; i++ ) {
    Serial.print( buf[i], DEC );
    if( i<3 )
      Serial.print( "." );
  }
}

void printHex( uint8_t hexval ) {
  if( hexval < 16 ) {
    Serial.print("0");
  }
  Serial.print( hexval, HEX );
  Serial.print( " " );
  timer = -9999999; // start timing out right away
}

void loop(){
  pinMode(14, OUTPUT);      // sets the digital pin as output
  pinMode(15, OUTPUT);      // sets the digital pin as output
  digitalWrite(14,HIGH);
  digitalWrite(15,LOW);

  ping(pcip);

  ping(gwip);

  ping(fooip);

}

void ping(uint8_t *remIP){

   uint32_t endtime,dat_p;
  uint16_t timeout=5000; //timeout ping after 2seconds
  endtime=millis();

  //send an arp request to get the mac address of teh remoteIP
//  Serial.print("Senidng ARP Request to ");printIP(remIP);Serial.println();
   es.ES_client_set_gwip(remIP);
    while(1){
      if(millis()-endtime>=timeout){Serial.print("Timeout Waitin for Ping Reply ");Serial.print(endtime);Serial.println();state=0;break;}//timeout ping
       int plen=es.ES_enc28j60PacketReceive(BUFFER_SIZE,buf);
       dat_p=es.ES_packetloop_icmp_tcp(buf,plen);
       if(dat_p==0) {
          // we are idle here
          if (es.ES_client_waiting_gw() ){
          continue;
          }

        }
      //  if (!es.ES_client_waiting_gw() ){Serial.println("Got ARP Reply");state=1;break;}

       // report whenever a reply to our outgoing ping comes back
       if (plen > 0 && es.ES_packetloop_icmp_checkreply(buf,remIP)) {
       Serial.print("  ");
       Serial.print((micros() - timer) * 0.001, 3);
       Serial.println(" ms");
       state=1;break;
        }
      if (micros() - timer >= 1000000) { //ping every 1 second
       Serial.print(count);
        Serial.print(". Pinging: ");
        printIP(remIP);
        Serial.print("-->");
        timer = micros();
        es.ES_client_icmp_request(buf,remIP);
        count++;
      } 

   }

}

 

 

 

Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *