udp notifier finished

This commit is contained in:
cschwinne 2016-11-27 22:37:51 +01:00
parent c88b9d6143
commit a3a2c0448f
7 changed files with 53 additions and 62 deletions

View File

@ -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

View File

@ -1,13 +1,13 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <Hash.h>
#include <NeoPixelBus.h>
#include <FS.h>
#include <WiFiUDP.h>
/*
* @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<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> 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();

View File

@ -101,13 +101,7 @@ void XML_response_settings()
resp = resp + bool2int(notifyButton);
resp = resp + "</nsbtn><nsfwd>";
resp = resp + bool2int(notifyForward);
resp = resp + "</nsfwd><nsips>";
for (int i = 0; i < notifier_ips_count; i++)
{
resp = resp + notifier_ips[i];
resp = resp + "\n";
}
resp = resp + "</nsips>";
resp = resp + "</nsfwd><nsips>Legacy</nsips>";
resp = resp + "<noota>";
resp = resp + bool2int(ota_lock);
resp = resp +"</noota>";

View File

@ -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;

View File

@ -59,6 +59,8 @@ void wledInit()
}
Serial.println("mDNS responder started");
udpConnected = notifierUdp.begin(udpPort);
//SERVER INIT
//settings page
server.on("/settings", HTTP_GET, [](){
@ -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(){

View File

@ -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++)
notifierUdp.beginPacket(broadcastIp, udpPort);
notifierUdp.write(udpOut, 16);
notifierUdp.endPacket();
}
void handleNotifications()
{
Serial.println("NCON...");
if (hclient.connect(notifier_ips[i].c_str(), 80))
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");
notifierUdp.read(notifierBuffer, 16);
int bri_r = notifierBuffer[2]*(((float)bri_n)/100);
if (bri_r < 256)
{
bri_n = bri_r;
} else
{
Serial.println("NO CONNECTION");
hclient.stop();
bri_n = 255;
}
col[0] = notifierBuffer[3]
col[1] = notifierBuffer[4];
col[2] = notifierBuffer[5];
if (notifierBuffer[6])
{
nightlightActive = true;
} else {
colorUpdated(3);
}
}
}
}

View File

@ -111,6 +111,7 @@ void handleNightlight()
{
if (!nightlightActive_old) //init
{
notify(4);
nightlightDelayMs = (int)(nightlightDelayMins*60000);
nightlightActive_old = true;
if (nightlightFade)