Pastebin launched a little side project called HostCabi.net, check it out ;-)Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 9th, 2013  |  syntax: None  |  size: 2.55 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  5. #include <ICMPPing.h>
  6.  
  7. byte mac[] = {0xDE, 0xAD, 0xBE, 0xDD, 0xFE, 0xED}; // mac address for ethernet shield
  8. byte ip[] = {192,168,0,177}; // ip address for ethernet shield
  9. byte pingAddr[] = {192,168,0,10}; // ip address to ping
  10.  
  11. SOCKET pingSocket = 0;
  12.  
  13. char buffer [256];
  14.  
  15. int delayMS = 5 * 1000; // delay between successive pings (60 * 1000 = 60 seconds)
  16.  
  17. #define serialOut 1
  18. #define ledOut 1
  19.  
  20. #ifdef ledOut
  21.   #define ledPing 2
  22.   #define ledOk 3
  23.   #define ledFail 4
  24.  
  25. //udp stuff
  26. unsigned int localPort = 8888;      // local port to listen on
  27.  
  28. // buffers for receiving and sending data
  29. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  30. char  ReplyBuffer[] = "acknowledged";       // a string to send back
  31.  
  32. // An EthernetUDP instance to let us send and receive packets over UDP
  33. EthernetUDP Udp;
  34. //end udp stuff
  35.  
  36.  
  37. int val=0;
  38.  
  39. bool connected = false;
  40.  
  41. void setup()
  42. {
  43.  
  44.  
  45. #ifdef serialOut
  46.   // start serial port:
  47.   Serial.begin(9600);
  48.   Serial.println("Starting ethernet connection");
  49. #endif
  50.   //start Ethernet
  51.  // if (Ethernet.begin(mac) == 0) {
  52. #ifdef serialOut
  53.    // Serial.println("Failed to configure Ethernet using DHCP");
  54. #endif
  55.     // DHCP failed, so use a fixed IP address:
  56.     Ethernet.begin(mac, ip);
  57. // An EthernetUDP instance to let us send and receive packets over UDP
  58. EthernetUDP Udp;
  59. //end udp stuff
  60.  
  61. }
  62.  
  63.  
  64.  
  65. void startPing()
  66. {
  67.   Serial.println("Ping begin");
  68. }
  69.  
  70. void endPing()
  71. {
  72.   Serial.println("Ping fin");
  73. }
  74.  
  75. void pingSuccess()
  76. {
  77. Serial.println("Ping great success");
  78.  Serial.println("Living Room - Sofa light ON");
  79.     Udp.beginPacket("192.168.0.7", 9760);
  80.     Udp.write("678,!R2D1F1|");
  81.     Udp.endPacket();
  82.     delay(5000);
  83.     int val = random(300);
  84. }
  85.  
  86. void pingFail()
  87. {
  88. Serial.println("Ping fail");
  89.  Serial.println("Living Room - Sofa light OFF");
  90.     Udp.beginPacket("192.168.0.7", 9760);
  91.     Udp.write("678,!R2D1F0|");
  92.     Udp.endPacket();
  93. delay(5000);
  94. int val = random(300);
  95. }
  96. #endif
  97.  
  98.  
  99. void loop()
  100. {
  101.   bool pingRet; // pingRet stores the ping() success (true/false)
  102.  
  103. #ifdef ledOut
  104.   startPing();
  105. #endif
  106.   ICMPPing ping(pingSocket);
  107.   pingRet = ping(4, pingAddr, buffer);
  108. #ifdef ledOut
  109.   delay(250);
  110.   endPing();
  111. #endif
  112.  
  113.  Serial.println(buffer);
  114.  
  115. #ifdef seriallOut
  116.   Serial.println(buffer);
  117. #endif
  118. #ifdef ledOut
  119.   if(pingRet) // Failure
  120.     pingSuccess();
  121.   else
  122.     pingFail();
  123. #endif
  124.   delay(delayMS);
  125. }