HA discovery wdt reset

This commit is contained in:
cschwinne 2019-10-20 17:38:25 +02:00
parent 0d3a8ce31b
commit 31e4e7c709
6 changed files with 71 additions and 88 deletions

View File

@ -61,7 +61,7 @@
#define ESPALEXA_ASYNC #define ESPALEXA_ASYNC
#define ESPALEXA_NO_SUBPAGE #define ESPALEXA_NO_SUBPAGE
#define ESPALEXA_MAXDEVICES 1 #define ESPALEXA_MAXDEVICES 1
#define ESPALEXA_DEBUG //#define ESPALEXA_DEBUG
#include "src/dependencies/espalexa/Espalexa.h" #include "src/dependencies/espalexa/Espalexa.h"
#endif #endif
#ifndef WLED_DISABLE_BLYNK #ifndef WLED_DISABLE_BLYNK
@ -124,7 +124,7 @@ char apSSID[33] = ""; //AP off by default (unless setup)
byte apChannel = 1; //2.4GHz WiFi AP channel (1-13) byte apChannel = 1; //2.4GHz WiFi AP channel (1-13)
byte apHide = 0; //hidden AP SSID byte apHide = 0; //hidden AP SSID
byte apWaitTimeSecs = 32; //time to wait for connection before opening AP byte apWaitTimeSecs = 32; //time to wait for connection before opening AP
bool apAlwaysOn = true; bool apAlwaysOn = false;
bool recoveryAPDisabled = false; //never open AP (not recommended) bool recoveryAPDisabled = false; //never open AP (not recommended)
IPAddress staticIP(0, 0, 0, 0); //static IP of ESP IPAddress staticIP(0, 0, 0, 0); //static IP of ESP
IPAddress staticGateway(0, 0, 0, 0); //gateway (router) IP IPAddress staticGateway(0, 0, 0, 0); //gateway (router) IP
@ -146,9 +146,6 @@ byte effectSpeedDefault = 75;
byte effectIntensityDefault = 128; //intensity is supported on some effects as an additional parameter (e.g. for blink you can change the duty cycle) byte effectIntensityDefault = 128; //intensity is supported on some effects as an additional parameter (e.g. for blink you can change the duty cycle)
byte effectPaletteDefault = 0; //palette is supported on the FastLED effects, otherwise it has no effect byte effectPaletteDefault = 0; //palette is supported on the FastLED effects, otherwise it has no effect
//bool strip.gammaCorrectBri = false; //gamma correct brightness (not recommended) --> edit in WS2812FX.h
//bool strip.gammaCorrectCol = true; //gamma correct colors (strongly recommended)
byte nightlightTargetBri = 0; //brightness after nightlight is over byte nightlightTargetBri = 0; //brightness after nightlight is over
byte nightlightDelayMins = 60; byte nightlightDelayMins = 60;
bool nightlightFade = true; //if enabled, light will gradually dim towards the target bri. Otherwise, it will instantly set after delay over bool nightlightFade = true; //if enabled, light will gradually dim towards the target bri. Otherwise, it will instantly set after delay over
@ -424,6 +421,8 @@ String messageHead, messageSub;
byte optionType; byte optionType;
bool doReboot = false; //flag to initiate reboot from async handlers bool doReboot = false; //flag to initiate reboot from async handlers
bool doPublishMqtt = false;
bool doSendHADiscovery = true;
//server library objects //server library objects
AsyncWebServer server(80); AsyncWebServer server(80);
@ -465,6 +464,7 @@ WS2812FX strip = WS2812FX();
#include "SPIFFSEditor.h" #include "SPIFFSEditor.h"
#endif #endif
//function prototypes //function prototypes
void serveMessage(AsyncWebServerRequest*,uint16_t,String,String,byte); void serveMessage(AsyncWebServerRequest*,uint16_t,String,String,byte);
@ -525,7 +525,7 @@ void loop() {
handleAlexa(); handleAlexa();
handleOverlays(); handleOverlays();
if (doSendHADiscovery) sendHADiscoveryMQTT();
yield(); yield();
if (doReboot) reset(); if (doReboot) reset();
@ -533,7 +533,7 @@ void loop() {
{ {
if (apActive) dnsServer.processNextRequest(); if (apActive) dnsServer.processNextRequest();
#ifndef WLED_DISABLE_OTA #ifndef WLED_DISABLE_OTA
if (aOtaEnabled) ArduinoOTA.handle(); if (WLED_CONNECTED && aOtaEnabled) ArduinoOTA.handle();
#endif #endif
handleNightlight(); handleNightlight();
yield(); yield();

View File

@ -40,21 +40,13 @@ void wledInit()
DEBUG_PRINTLN("Load EEPROM"); DEBUG_PRINTLN("Load EEPROM");
loadSettingsFromEEPROM(true); loadSettingsFromEEPROM(true);
beginStrip(); beginStrip();
userBeginPreConnection(); userSetup();
if (strcmp(clientSSID,"Your_Network") == 0) showWelcomePage = true; if (strcmp(clientSSID,"Your_Network") == 0) showWelcomePage = true;
WiFi.persistent(false); WiFi.persistent(false);
if (macroBoot>0) applyMacro(macroBoot); if (macroBoot>0) applyMacro(macroBoot);
Serial.println("Ada"); Serial.println("Ada");
if (udpPort > 0 && udpPort != ntpLocalPort)
{
udpConnected = notifierUdp.begin(udpPort);
if (udpConnected && udpRgbPort != udpPort) udpRgbConnected = rgbUdp.begin(udpRgbPort);
}
if (ntpEnabled && WLED_CONNECTED)
ntpConnected = ntpUdp.begin(ntpLocalPort);
//generate module IDs //generate module IDs
escapedMac = WiFi.macAddress(); escapedMac = WiFi.macAddress();
escapedMac.replace(":", ""); escapedMac.replace(":", "");
@ -77,51 +69,25 @@ void wledInit()
strip.service(); strip.service();
//HTTP server page init #ifndef WLED_DISABLE_OTA
initServer();
strip.service();
server.begin();
DEBUG_PRINTLN("HTTP server started");
//init ArduinoOTA
if (true) {
#ifndef WLED_DISABLE_OTA
if (aOtaEnabled) if (aOtaEnabled)
{ {
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
#ifndef ARDUINO_ARCH_ESP32 #ifdef ESP8266
wifi_set_sleep_type(NONE_SLEEP_T); wifi_set_sleep_type(NONE_SLEEP_T);
#endif #endif
DEBUG_PRINTLN("Start ArduinoOTA"); DEBUG_PRINTLN("Start ArduinoOTA");
}); });
if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS); if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS);
ArduinoOTA.begin();
} }
#endif #endif
//HTTP server page init
initServer();
strip.service(); strip.service();
// Set up mDNS responder:
if (strlen(cmDNS) > 0 && WLED_CONNECTED)
{
MDNS.begin(cmDNS);
DEBUG_PRINTLN("mDNS responder started");
// Add service to MDNS
MDNS.addService("http", "tcp", 80);
MDNS.addService("wled", "tcp", 80);
}
strip.service();
initBlynk(blynkApiKey);
initE131();
reconnectHue();
} else {
e131Enabled = false;
}
initConnection(); initConnection();
userBegin();
} }
@ -168,6 +134,14 @@ void initAP(bool resetAP=false){
if (!apActive) //start captive portal if AP active if (!apActive) //start captive portal if AP active
{ {
DEBUG_PRINTLN("Init AP interfaces");
server.begin();
if (udpPort > 0 && udpPort != ntpLocalPort)
{
udpConnected = notifierUdp.begin(udpPort);
if (udpConnected && udpRgbPort != udpPort) udpRgbConnected = rgbUdp.begin(udpRgbPort);
}
dnsServer.setErrorReplyCode(DNSReplyCode::NoError); dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP()); dnsServer.start(53, "*", WiFi.softAPIP());
} }
@ -188,7 +162,12 @@ void initConnection()
lastReconnectAttempt = millis(); lastReconnectAttempt = millis();
if (!apActive) { if (!WLED_WIFI_CONFIGURED)
{
DEBUG_PRINT("No connection configured. ");
if (!apActive) initAP(); //instantly go to ap mode
return;
} else if (!apActive) {
if (apAlwaysOn) if (apAlwaysOn)
{ {
initAP(); initAP();
@ -198,20 +177,13 @@ void initConnection()
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
} }
} }
if (!WLED_WIFI_CONFIGURED)
{
DEBUG_PRINT("No connection configured. ");
initAP(); //instantly go to ap mode
return;
}
showWelcomePage = false; showWelcomePage = false;
DEBUG_PRINT("Connecting to "); DEBUG_PRINT("Connecting to ");
DEBUG_PRINT(clientSSID); DEBUG_PRINT(clientSSID);
DEBUG_PRINTLN("..."); DEBUG_PRINTLN("...");
#ifndef ARDUINO_ARCH_ESP32 #ifdef ESP8266
WiFi.hostname(serverDescription); WiFi.hostname(serverDescription);
#endif #endif
WiFi.begin(clientSSID, clientPass); WiFi.begin(clientSSID, clientPass);
@ -221,6 +193,9 @@ void initConnection()
} }
void initInterfaces() { void initInterfaces() {
DEBUG_PRINTLN("Init STA interfaces");
server.begin();
if (hueIP[0] == 0) if (hueIP[0] == 0)
{ {
hueIP[0] = WiFi.localIP()[0]; hueIP[0] = WiFi.localIP()[0];
@ -231,45 +206,43 @@ void initInterfaces() {
//init Alexa hue emulation //init Alexa hue emulation
if (alexaEnabled) alexaInit(); if (alexaEnabled) alexaInit();
initMqtt();
#ifndef WLED_DISABLE_OTA #ifndef WLED_DISABLE_OTA
if (aOtaEnabled) if (aOtaEnabled) ArduinoOTA.begin();
{
ArduinoOTA.onStart([]() {
#ifndef ARDUINO_ARCH_ESP32
wifi_set_sleep_type(NONE_SLEEP_T);
#endif
DEBUG_PRINTLN("Start ArduinoOTA");
});
if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS);
ArduinoOTA.begin();
}
#endif #endif
strip.service(); strip.service();
// Set up mDNS responder: // Set up mDNS responder:
if (strlen(cmDNS) > 0) if (strlen(cmDNS) > 0)
{ {
MDNS.begin(cmDNS); if (MDNS.begin(cmDNS))
DEBUG_PRINTLN("mDNS responder started"); {
// Add service to MDNS MDNS.addService("http", "tcp", 80);
MDNS.addService("http", "tcp", 80); MDNS.addService("wled", "tcp", 80);
MDNS.addService("wled", "tcp", 80); DEBUG_PRINTLN("mDNS started");
} else {
DEBUG_PRINTLN("mDNS failed!");
}
DEBUG_PRINTLN("mDNS started");
} }
strip.service(); strip.service();
if (ntpEnabled && WLED_CONNECTED)
ntpConnected = ntpUdp.begin(ntpLocalPort);
initBlynk(blynkApiKey); initBlynk(blynkApiKey);
initE131(); initE131();
reconnectHue(); reconnectHue();
initMqtt();
interfacesInited = true; interfacesInited = true;
} }
byte stacO = 0; byte stacO = 0;
void handleConnection() { void handleConnection() {
byte stac = wifi_softap_get_station_num(); byte stac = 0;
#ifdef ESP8266
stac = wifi_softap_get_station_num();
#endif
if (stac != stacO) if (stac != stacO)
{ {
stacO = stac; stacO = stac;
@ -300,6 +273,7 @@ void handleConnection() {
DEBUG_PRINT("Connected! IP address: "); DEBUG_PRINT("Connected! IP address: ");
DEBUG_PRINTLN(WiFi.localIP()); DEBUG_PRINTLN(WiFi.localIP());
initInterfaces(); initInterfaces();
userConnected();
//shut down AP //shut down AP
if (!apAlwaysOn && apActive) if (!apAlwaysOn && apActive)

View File

@ -7,16 +7,19 @@
//Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t) //Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t)
void userBeginPreConnection() //gets called once at boot. Do all initialization that doesn't depend on network here
void userSetup()
{ {
} }
void userBegin() //gets called every time WiFi is (re-)connected. Initialize own network interfaces here
void userConnected()
{ {
} }
//loop. You can use "if (WLED_CONNECTED)" to check for successful connection
void userLoop() void userLoop()
{ {

View File

@ -99,7 +99,7 @@ void initE131(){
void handleE131(){ void handleE131(){
//E1.31 protocol support //E1.31 protocol support
if(e131Enabled) { if(WLED_CONNECTED && e131Enabled) {
uint16_t len = e131->parsePacket(); uint16_t len = e131->parsePacket();
if (!len || e131->universe < e131Universe || e131->universe > e131Universe +4) return; if (!len || e131->universe < e131Universe || e131->universe > e131Universe +4) return;
len /= 3; //one LED is 3 DMX channels len /= 3; //one LED is 3 DMX channels

View File

@ -142,7 +142,7 @@ void updateInterfaces(uint8_t callMode)
} }
#endif #endif
if (callMode != 9 && callMode != 5) updateBlynk(); if (callMode != 9 && callMode != 5) updateBlynk();
publishMqtt(); doPublishMqtt = true;
lastInterfaceUpdate = millis(); lastInterfaceUpdate = millis();
} }
@ -155,6 +155,7 @@ void handleTransitions()
updateInterfaces(interfaceUpdateCallMode); updateInterfaces(interfaceUpdateCallMode);
interfaceUpdateCallMode = 0; //disable interfaceUpdateCallMode = 0; //disable
} }
if (doPublishMqtt) publishMqtt();
if (transitionActive && transitionDelayTemp > 0) if (transitionActive && transitionDelayTemp > 0)
{ {

View File

@ -43,15 +43,15 @@ void onMqttConnect(bool sessionPresent)
mqtt->subscribe(subuf, 0); mqtt->subscribe(subuf, 0);
} }
sendHADiscoveryMQTT(); doSendHADiscovery = true;
publishMqtt(); //doPublishMqtt = true;
DEBUG_PRINTLN("MQTT ready"); DEBUG_PRINTLN("MQTT ready");
} }
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
DEBUG_PRINT("MQ callb rec: "); DEBUG_PRINT("MQTT msg: ");
DEBUG_PRINTLN(topic); DEBUG_PRINTLN(topic);
DEBUG_PRINTLN(payload); DEBUG_PRINTLN(payload);
@ -72,6 +72,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
void publishMqtt() void publishMqtt()
{ {
doPublishMqtt = false;
if (mqtt == nullptr || !mqtt->connected()) return; if (mqtt == nullptr || !mqtt->connected()) return;
DEBUG_PRINTLN("Publish MQTT"); DEBUG_PRINTLN("Publish MQTT");
@ -134,6 +135,9 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
} }
*/ */
doSendHADiscovery = false;
if (mqtt == nullptr || !mqtt->connected()) return;
char bufc[36], bufcol[38], bufg[36], bufapi[38], buffer[2500]; char bufc[36], bufcol[38], bufg[36], bufapi[38], buffer[2500];
strcpy(bufc, mqttDeviceTopic); strcpy(bufc, mqttDeviceTopic);
@ -158,7 +162,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
root["fx_stat_t"] = bufapi; root["fx_stat_t"] = bufapi;
size_t jlen = measureJson(root); size_t jlen = measureJson(root);
DEBUG_PRINTLN(jlen); //DEBUG_PRINTLN(jlen);
serializeJson(root, buffer, jlen); serializeJson(root, buffer, jlen);
//add values which don't change //add values which don't change
@ -190,7 +194,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
mdn[namelen] = 0; mdn[namelen] = 0;
snprintf(mdnfx, 64, "\"[FX=%02d] %s\",", i, mdn); snprintf(mdnfx, 64, "\"[FX=%02d] %s\",", i, mdn);
oappend(mdnfx); oappend(mdnfx);
DEBUG_PRINTLN(mdnfx); //DEBUG_PRINTLN(mdnfx);
i++; i++;
} }
isNameStart = !isNameStart; isNameStart = !isNameStart;
@ -206,7 +210,8 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
strcpy(pubt, "homeassistant/light/"); strcpy(pubt, "homeassistant/light/");
strcat(pubt, mqttClientID); strcat(pubt, mqttClientID);
strcat(pubt, "/config"); strcat(pubt, "/config");
mqtt->publish(pubt, 0, true, buffer); DEBUG_PRINTLN(mqtt->publish(pubt, 0, true, buffer));
yield();
#endif #endif
} }