]> git.0d.be Git - PanikSwitch.git/blobdiff - FINAL2013V2.ino
put udp code behind ENABLE_UDP #define
[PanikSwitch.git] / FINAL2013V2.ino
index 830e97dd718779f15d77b7a34169d483c1ae592a..5415ece0f4107be3f9a70344bb845a50cc71626c 100644 (file)
@@ -1,22 +1,26 @@
 // Panik Web Form Demo
 
+#define SERIAL_DEBUG
+#define ENABLE_UDP
 
 
 // define network constants
 #define MAC_DAVID { 0x90, 0xA2, 0xDA, 0x0D, 0xF4, 0x63 }
 #define MAC_PANIK { 0x90, 0xA2, 0xDA, 0x00, 0x9A, 0x94 }
 #define MAC_PANIK2 { 0x90, 0xA2, 0xDA, 0x0E, 0xF3, 0x77 }
-#define MAC_ADDRESS MAC_PANIK
-
+#define MAC_ADDRESS MAC_PANIK2
 
 // include Arduino libraries
 #include <Wire.h>
 #include <SPI.h>
 #include <SD.h>
 #include <Ethernet.h>
+#ifdef ENABLE_UDP
+#include <EthernetUdp.h>
+#endif
 
 // include third party libraries
-#include <WebServer.h>
+#include "WebServer.h"
 
 // include in-house libs
 #include "PanikSwitch.h"    // contains variable types for panik switch
@@ -90,6 +94,13 @@ static uint8_t mac[] = MAC_ADDRESS;
 // instanciate web server
 WebServer webserver(PREFIX, 80);
 
+#ifdef ENABLE_UDP
+// and EthernetUDP instance to send notifications
+EthernetUDP Udp;
+IPAddress udp_remote_ip(192, 168, 17, 224);
+#endif
+
+
 #define NAMELEN 4
 #define VALUELEN 4
 
@@ -109,7 +120,6 @@ void webCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
 
   if (type == WebServer::POST)
   {
-    return;
     char name[NAMELEN];
     int  name_len;
     char value[VALUELEN];
@@ -171,9 +181,18 @@ void setup()
     Serial.println(Ethernet.localIP());
   else
   {
+    #ifdef SERIAL_DEBUG
     Serial.println(F("Network Error"));
+    #endif
     while (1) ;
   }
+  #ifdef ENABLE_UDP
+  if (! Udp.begin(1312)) {
+    #ifdef SERIAL_DEBUG
+    Serial.println(F("Failed to initiate UDP"));
+    #endif
+  }
+  #endif
 
   // set mode for used pins
   pinMode(RELAY_RED_LEDS, OUTPUT);
@@ -227,7 +246,9 @@ void loop()
     case nochange:
       break;
     case pressed:
-      //Serial.println(F("Button 1 pressed"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 1 pressed"));
+      #endif
       digitalWrite(ledsArray[blinkingSelection], RELAY_STATE_CLOSED);
       blinkingSelection++;
       blinkingStartTime = millis(),
@@ -235,7 +256,9 @@ void loop()
       blinkingLedState = RELAY_STATE_OPEN;
       break;
     case released:
-      //Serial.println(F("Button 1 released"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 1 released"));
+      #endif
       break;
   }
 
@@ -245,17 +268,31 @@ void loop()
     case nochange:
       break;
     case pressed:
-      //Serial.println(F("Button 2 pressed"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 2 pressed"));
+      #endif
       blinkingAbortTime = 0;        // disable blinking auto abort
       break;
     case released:
-      //Serial.println(F("Button 2 released"));
+      #ifdef SERIAL_DEBUG
+      Serial.println(F("Button 2 released"));
+      #endif
       if (activeSelection != blinkingSelection)
       {
+        char str_selection[20];
         digitalWrite(ledsArray[activeSelection], RELAY_STATE_CLOSED);
         activeSelection = blinkingSelection;  // relay states must be changed now
+        #ifdef SERIAL_DEBUG
         Serial.print(F("Active Selection: "));
         Serial.println(activeSelection);
+        #endif
+        #ifdef ENABLE_UDP
+        // notify over UDP
+        snprintf(str_selection, 19, "{\"active\": %d}", activeSelection);
+        Udp.beginPacket(udp_remote_ip, 1312);
+        Udp.write(str_selection);
+        Udp.endPacket();
+        #endif
         break;
       }
   }
@@ -350,4 +387,3 @@ buttonEvent_t debounce( const uint8_t buttonPin,
   }
   return nochange;
 }
-