Advertisement
phillips321

NanodeNetMon

Oct 2nd, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <EtherCard.h>
  2. #include <NanodeUNIO.h>
  3.  
  4. byte mymac[] = { 0x88,0x88,0x88,0x88,0x88,0x88 };
  5. byte Ethernet::buffer[700];
  6. static uint32_t timer;
  7. char buf[20];
  8. NanodeUNIO unio(NANODE_MAC_DEVICE);
  9. boolean r;
  10.  
  11.  
  12. void setup () {
  13.   Serial.begin(57600);
  14.   Serial.println("Nanode Network Monitor");
  15.   getmac();
  16.  
  17.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  18.     Serial.println( "Failed to access Ethernet controller");
  19.   if (!ether.dhcpSetup())
  20.     Serial.println("DHCP failed");
  21.  
  22.   ether.printIp("IP:  ", ether.myip);
  23.   ether.printIp("GW:  ", ether.gwip);
  24.  
  25. #if 1
  26.   // use DNS to locate the IP address we want to ping
  27.   if (!ether.dnsLookup(PSTR("www.google.com")));
  28.     Serial.println("DNS failed");
  29. #else
  30.   ether.parseIp(ether.hisip, "209.85.147.105");
  31. #endif
  32.   ether.printIp("SRV: ", ether.hisip);
  33.    
  34.   // call this to report others pinging us
  35.   ether.registerPingCallback(gotPinged);
  36.  
  37.   timer = -9999999; // start timing out right away
  38.   Serial.println();
  39. }
  40.  
  41. void loop () {
  42.   word len = ether.packetReceive(); // go receive new packets
  43.   word pos = ether.packetLoop(len); // respond to incoming pings
  44.  
  45.   // report whenever a reply to our outgoing ping comes back
  46.   if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
  47.     Serial.print("  ");
  48.     Serial.print((micros() - timer) * 0.001, 3);
  49.     Serial.println(" ms");
  50.   }
  51.  
  52.   // ping a remote server once every few seconds
  53.   if (micros() - timer >= 5000000) {
  54.     ether.printIp("Pinging: ", ether.hisip);
  55.     timer = micros();
  56.     ether.clientIcmpRequest(ether.hisip);
  57.   }
  58. }
  59.  
  60. // called when a ping comes in (replies to it are automatic)
  61. static void gotPinged (byte* ptr) {
  62.   ether.printIp(">>> ping from: ", ptr);
  63. }
  64.  
  65. void getmac(){
  66.   boolean mactest; char buf[20];
  67.   NanodeUNIO unio(NANODE_MAC_DEVICE);
  68.   Serial.print("Reading MAC address... ");
  69.   mactest=unio.read(mymac,NANODE_MAC_ADDRESS,6);
  70.   if (mactest){
  71.     Serial.println("success");
  72.   }
  73.   else{
  74.     Serial.println("failure");
  75.   }
  76.   sprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X",mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]);
  77.   Serial.print("MAC address is ");Serial.println(buf);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement