Advertisement
gregoryfenton

UDP turn light on if IP address responds to a ping

Feb 10th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Portions of this code written by Gregory Fenton
  3.     http://labby.co.uk/2012/08/arduino-visual-icmp-ping-server-monitor-icmp-echo-request/
  4. */
  5.  
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8. #include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  9. #include <ICMPPing.h>
  10.  
  11. byte mac[] = {0xDE, 0xAD, 0xBE, 0xDD, 0xFE, 0xED}; // mac address for ethernet shield
  12. byte ip[] = {192,168,0,177}; // ip address for ethernet shield
  13. byte pingAddr[] = {192,168,0,10}; // ip address to ping
  14.  
  15. bool pingStatus = false;
  16.  
  17. SOCKET pingSocket = 0;
  18.  
  19. char buffer [256];
  20.  
  21. int delayMS = 5 * 1000; // delay between successive pings (60 * 1000 = 60 seconds)
  22.  
  23. #define serialOut 1
  24. #define ledOut 1
  25.  
  26. #ifdef ledOut
  27.     #define ledPing 2
  28.     #define ledOk 3
  29.     #define ledFail 4
  30. #endif
  31.  
  32. //udp stuff
  33. unsigned int localPort = 8888;          // local port to listen on
  34.  
  35. // buffers for receiving and sending data
  36. // greg: unused in this code?
  37. //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  38. //char ReplyBuffer[] = "acknowledged";           // a string to send back
  39.  
  40. // An EthernetUDP instance to let us send and receive packets over UDP
  41. EthernetUDP Udp;
  42. //end udp stuff
  43.  
  44. // greg: unused in this code?
  45. // int val=0;
  46.  
  47. bool connected = false;
  48.  
  49. void setup()
  50. {
  51.     #ifdef serialOut
  52.         // start serial port:
  53.         Serial.begin(9600);
  54.         Serial.println("Starting ethernet connection");
  55.     #endif
  56.     //start Ethernet
  57.     // if (Ethernet.begin(mac) == 0) {
  58.     #ifdef serialOut
  59.         // Serial.println("Failed to configure Ethernet using DHCP");
  60.     #endif
  61.     // DHCP failed, so use a fixed IP address:
  62.     Ethernet.begin(mac, ip);
  63.     // start udp connection
  64.     Udp.begin(localPort);
  65.     // }
  66. }
  67.  
  68. #ifdef serialOut
  69.     void startPing()
  70.     {
  71.         Serial.println("Ping begin");
  72.     }
  73.  
  74.     void endPing()
  75.     {
  76.         Serial.println("Ping fin");
  77.     }
  78. #endif
  79.  
  80. #ifdef ledOut
  81.     void pingSuccess()
  82.     {
  83.         #ifdef serialOut
  84.             Serial.println("Ping great success");
  85.         #endif
  86.         if(pingStatus == false) // we don't need to constantly turn the light on
  87.         {
  88.             #ifdef serialOut
  89.                 Serial.println("Living Room - Sofa light ON");
  90.             #endif
  91.             Udp.beginPacket("192.168.0.7", 9760);
  92.             Udp.write("678,!R2D1F1|");
  93.             Udp.endPacket();
  94.             // greg: unused in this code?
  95.             // val = random(300);
  96.             pingStatus = true;
  97.         }
  98.     }
  99.  
  100.     void pingFail()
  101.     {
  102.         #ifdef serialOut
  103.             Serial.println("Ping fail");
  104.         #endif
  105.         if(pingStatus == true) // we don't need to constantly turn the light off
  106.         {
  107.             #ifdef serialOut
  108.                 Serial.println("Living Room - Sofa light OFF");
  109.             #endif
  110.             Udp.beginPacket("192.168.0.7", 9760);
  111.             Udp.write("678,!R2D1F0|");
  112.             Udp.endPacket();
  113.             // greg: unused in this code?
  114.             // val = random(300);
  115.             pingStatus = false;
  116.         }
  117.     }
  118. #endif
  119.  
  120. void loop()
  121. {
  122.     bool pingRet; // pingRet stores the ping() success (true/false)
  123.  
  124.     #ifdef ledOut
  125.         startPing();
  126.     #endif
  127.     ICMPPing ping(pingSocket);
  128.     pingRet = ping(4, pingAddr, buffer);
  129.     #ifdef ledOut
  130.         delay(250);
  131.         endPing();
  132.     #endif
  133.  
  134.     #ifdef seriallOut
  135.         Serial.println(buffer);
  136.     #endif
  137.     #ifdef ledOut
  138.         if(pingRet) // Failure
  139.             pingSuccess();
  140.         else
  141.             pingFail();
  142.     #endif
  143.     delay(delayMS);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement