/* Portions of this code written by Gregory Fenton http://labby.co.uk/2012/08/arduino-visual-icmp-ping-server-monitor-icmp-echo-request/ */ #include #include #include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 #include byte mac[] = {0xDE, 0xAD, 0xBE, 0xDD, 0xFE, 0xED}; // mac address for ethernet shield byte ip[] = {192,168,0,177}; // ip address for ethernet shield byte pingAddr[] = {192,168,0,10}; // ip address to ping bool pingStatus = false; SOCKET pingSocket = 0; char buffer [256]; int delayMS = 5 * 1000; // delay between successive pings (60 * 1000 = 60 seconds) #define serialOut 1 #define ledOut 1 #ifdef ledOut #define ledPing 2 #define ledOk 3 #define ledFail 4 #endif //udp stuff unsigned int localPort = 8888; // local port to listen on // buffers for receiving and sending data // greg: unused in this code? //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, //char ReplyBuffer[] = "acknowledged"; // a string to send back // An EthernetUDP instance to let us send and receive packets over UDP EthernetUDP Udp; //end udp stuff // greg: unused in this code? // int val=0; bool connected = false; void setup() { #ifdef serialOut // start serial port: Serial.begin(9600); Serial.println("Starting ethernet connection"); #endif //start Ethernet // if (Ethernet.begin(mac) == 0) { #ifdef serialOut // Serial.println("Failed to configure Ethernet using DHCP"); #endif // DHCP failed, so use a fixed IP address: Ethernet.begin(mac, ip); // start udp connection Udp.begin(localPort); // } } #ifdef serialOut void startPing() { Serial.println("Ping begin"); } void endPing() { Serial.println("Ping fin"); } #endif #ifdef ledOut void pingSuccess() { #ifdef serialOut Serial.println("Ping great success"); #endif if(pingStatus == false) // we don't need to constantly turn the light on { #ifdef serialOut Serial.println("Living Room - Sofa light ON"); #endif Udp.beginPacket("192.168.0.7", 9760); Udp.write("678,!R2D1F1|"); Udp.endPacket(); // greg: unused in this code? // val = random(300); pingStatus = true; } } void pingFail() { #ifdef serialOut Serial.println("Ping fail"); #endif if(pingStatus == true) // we don't need to constantly turn the light off { #ifdef serialOut Serial.println("Living Room - Sofa light OFF"); #endif Udp.beginPacket("192.168.0.7", 9760); Udp.write("678,!R2D1F0|"); Udp.endPacket(); // greg: unused in this code? // val = random(300); pingStatus = false; } } #endif void loop() { bool pingRet; // pingRet stores the ping() success (true/false) #ifdef ledOut startPing(); #endif ICMPPing ping(pingSocket); pingRet = ping(4, pingAddr, buffer); #ifdef ledOut delay(250); endPing(); #endif #ifdef seriallOut Serial.println(buffer); #endif #ifdef ledOut if(pingRet) // Failure pingSuccess(); else pingFail(); #endif delay(delayMS); }