From a3a2c0448f4394fb3a9568ec64650289b4dd63cd Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sun, 27 Nov 2016 22:37:51 +0100 Subject: [PATCH] udp notifier finished --- TODO.txt | 4 +-- wled00/wled00.ino | 10 +++--- wled00/wled02_xml.ino | 8 +---- wled00/wled03_set.ino | 17 ---------- wled00/wled05_init.ino | 7 ++--- wled00/wled07_notify.ino | 68 ++++++++++++++++++++++++---------------- wled00/wled08_led.ino | 1 + 7 files changed, 53 insertions(+), 62 deletions(-) diff --git a/TODO.txt b/TODO.txt index 598b5266..cf67ebd9 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,13 +2,12 @@ color cycle sequence simple slide transition additional color picker field -implement all settings setters +implement all settings setters (notifyNightlight \n udpPort) implement HSB slider option implement ranges implement discrete range color setter implement discrete single color setter svg icons in html -notifier function -> send get request add preferred colors to settings -> quickly t. UI, button select, use iframe for settings, seperate tabs for wifi and application confg use iframe for all adv. features? @@ -20,4 +19,3 @@ BUGS static ip disables mdns ? authentification for security relevant areas ([/settings, /reset]) (Unverified) led_amount does nothing (is always 16) because NeoPixelBus is initiated before EEPROM read -notifier wrong ips diff --git a/wled00/wled00.ino b/wled00/wled00.ino index a296ee8c..66dfba77 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -1,13 +1,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include /* * @title WLED project sketch @@ -36,12 +36,12 @@ boolean only_ap = false; uint8_t led_amount = 16; uint8_t buttonPin = 3; //needs pull-up boolean buttonEnabled = true; -String notifier_ips[]{"10.10.1.191","10.10.1.129"}; boolean notifyDirect = true, notifyButton = true, notifyForward = true, notifyNightlight = false; boolean receiveNotifications = true; uint8_t bri_n = 100; uint8_t nightlightDelayMins = 60; boolean nightlightFade = true; +unsigned int udpPort = 21324; double transitionResolution = 0.011; @@ -59,18 +59,19 @@ byte bri_it = 0; byte bri_last = 127; boolean transitionActive = false; boolean buttonPressedBefore = false; -int notifier_ips_count = 1; -String notifier_ips_raw = ""; boolean nightlightActive = false; boolean nightlightActive_old = false; int transitionDelay_old; long nightlightPassedTime = 0; int nightlightDelayMs; +boolean udpConnected = false; +byte notifierBuffer[16]; NeoPixelBus strip(led_amount, 1); ESP8266WebServer server(80); ESP8266HTTPUpdateServer httpUpdater; +WiFiUDP notifierUdp; File fsUploadFile; @@ -102,6 +103,7 @@ void setup() { void loop() { server.handleClient(); + handleNotifications(); handleTransitions(); handleNightlight(); handleButton(); diff --git a/wled00/wled02_xml.ino b/wled00/wled02_xml.ino index 55600a3b..af5da99d 100644 --- a/wled00/wled02_xml.ino +++ b/wled00/wled02_xml.ino @@ -101,13 +101,7 @@ void XML_response_settings() resp = resp + bool2int(notifyButton); resp = resp + ""; resp = resp + bool2int(notifyForward); - resp = resp + ""; - for (int i = 0; i < notifier_ips_count; i++) - { - resp = resp + notifier_ips[i]; - resp = resp + "\n"; - } - resp = resp + ""; + resp = resp + "Legacy"; resp = resp + ""; resp = resp + bool2int(ota_lock); resp = resp +""; diff --git a/wled00/wled03_set.ino b/wled00/wled03_set.ino index 91c51b1d..fddf1bea 100644 --- a/wled00/wled03_set.ino +++ b/wled00/wled03_set.ino @@ -123,10 +123,6 @@ void handleSettingsSet() notifyDirect = server.hasArg("NSDIR"); notifyButton = server.hasArg("NSBTN"); notifyForward = server.hasArg("NSFWD"); - if (server.hasArg("NSIPS")) - { - notifier_ips_raw = server.arg("NSIPS"); - } if (server.hasArg("OPASS")) { if (!ota_lock) @@ -156,8 +152,6 @@ boolean handleSet(String req) return false; } int pos = 0; - boolean isNotification = false; - if (req.indexOf("N=") > 0) isNotification = true; pos = req.indexOf("A="); if (pos > 0) { bri = req.substring(pos + 2).toInt(); @@ -184,17 +178,6 @@ boolean handleSet(String req) nightlightStartTime = millis(); } } - if (isNotification) - { - if (receiveNotifications) - { - colorUpdated(3); - server.send(200, "text/plain", ""); - return true; - } - server.send(202, "text/plain", ""); - return true; - } XML_response(); colorUpdated(1); return true; diff --git a/wled00/wled05_init.ino b/wled00/wled05_init.ino index 957aab94..9f6cd373 100644 --- a/wled00/wled05_init.ino +++ b/wled00/wled05_init.ino @@ -57,7 +57,9 @@ void wledInit() Serial.println("Error setting up MDNS responder!"); down(); } - Serial.println("mDNS responder started"); + Serial.println("mDNS responder started"); + + udpConnected = notifierUdp.begin(udpPort); //SERVER INIT //settings page @@ -123,15 +125,12 @@ void wledInit() server.begin(); Serial.println("HTTP server started"); - // Add service to MDNS MDNS.addService("http", "tcp", 80); // Initialize NeoPixel Strip strip.Begin(); colorUpdated(0); pinMode(buttonPin, INPUT_PULLUP); - - Serial.println(otapass); } void initAP(){ diff --git a/wled00/wled07_notify.ino b/wled00/wled07_notify.ino index d016d9e7..7b474c92 100644 --- a/wled00/wled07_notify.ino +++ b/wled00/wled07_notify.ino @@ -1,4 +1,4 @@ -void notify(int callMode) +void notify(uint8_t callMode) { switch (callMode) { @@ -8,35 +8,49 @@ void notify(int callMode) case 4: if (!notifyNightlight) return; break; default: return; } - String snd = "/ajax_inputs&N=1&A="; - snd = snd + bri; - snd = snd + "&R="; - snd = snd + col[0]; - snd = snd + "&G="; - snd = snd + col[1]; - snd = snd + "&B="; - snd = snd + col[2]; - //snd = snd + " HTTP/1.1"; + byte udpOut[16]; + udpOut[0] = 0; //reserved for future "port" feature + udpOut[1] = callMode; + udpOut[2] = bri; + udpOut[3] = col[0]; + udpOut[4] = col[1]; + udpOut[5] = col[2]; + udpOut[6] = nightlightActive; - WiFiClient hclient; - hclient.setTimeout(50); + IPAddress broadcastIp; + broadcastIp = ~WiFi.subnetMask() | WiFi.gatewayIP(); - for (int i = 0; i < notifier_ips_count; i++) - { - - Serial.println("NCON..."); - if (hclient.connect(notifier_ips[i].c_str(), 80)) + notifierUdp.beginPacket(broadcastIp, udpPort); + notifierUdp.write(udpOut, 16); + notifierUdp.endPacket(); +} + +void handleNotifications() +{ + if(udpConnected && receiveNotifications){ + int packetSize = notifierUdp.parsePacket(); + if(packetSize && notifierUdp.remoteIP() != WiFi.localIP()) { - Serial.println("CON!"); - Serial.println(snd); - hclient.print(String("GET ") + snd + " HTTP/1.1\r\n" + - "Host: " + notifier_ips[i] + "\r\n" + - "Connection: close\r\n\r\n"); - - } else - { - Serial.println("NO CONNECTION"); - hclient.stop(); + notifierUdp.read(notifierBuffer, 16); + int bri_r = notifierBuffer[2]*(((float)bri_n)/100); + if (bri_r < 256) + { + bri_n = bri_r; + } else + { + bri_n = 255; + } + col[0] = notifierBuffer[3] + col[1] = notifierBuffer[4]; + col[2] = notifierBuffer[5]; + if (notifierBuffer[6]) + { + nightlightActive = true; + } else { + colorUpdated(3); + } } } } + + diff --git a/wled00/wled08_led.ino b/wled00/wled08_led.ino index c4fa385c..d64c9bdc 100644 --- a/wled00/wled08_led.ino +++ b/wled00/wled08_led.ino @@ -111,6 +111,7 @@ void handleNightlight() { if (!nightlightActive_old) //init { + notify(4); nightlightDelayMs = (int)(nightlightDelayMins*60000); nightlightActive_old = true; if (nightlightFade)