diff --git a/wled00/wled00.ino b/wled00/wled00.ino index 114dd2c2e..fda198663 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -61,7 +61,7 @@ #define ESPALEXA_ASYNC #define ESPALEXA_NO_SUBPAGE #define ESPALEXA_MAXDEVICES 1 - #define ESPALEXA_DEBUG + //#define ESPALEXA_DEBUG #include "src/dependencies/espalexa/Espalexa.h" #endif #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 apHide = 0; //hidden AP SSID 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) IPAddress staticIP(0, 0, 0, 0); //static IP of ESP 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 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 nightlightDelayMins = 60; 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; bool doReboot = false; //flag to initiate reboot from async handlers +bool doPublishMqtt = false; +bool doSendHADiscovery = true; //server library objects AsyncWebServer server(80); @@ -465,6 +464,7 @@ WS2812FX strip = WS2812FX(); #include "SPIFFSEditor.h" #endif + //function prototypes void serveMessage(AsyncWebServerRequest*,uint16_t,String,String,byte); @@ -525,7 +525,7 @@ void loop() { handleAlexa(); handleOverlays(); - + if (doSendHADiscovery) sendHADiscoveryMQTT(); yield(); if (doReboot) reset(); @@ -533,7 +533,7 @@ void loop() { { if (apActive) dnsServer.processNextRequest(); #ifndef WLED_DISABLE_OTA - if (aOtaEnabled) ArduinoOTA.handle(); + if (WLED_CONNECTED && aOtaEnabled) ArduinoOTA.handle(); #endif handleNightlight(); yield(); diff --git a/wled00/wled05_init.ino b/wled00/wled05_init.ino index 70ac7d7ee..38acf32c7 100644 --- a/wled00/wled05_init.ino +++ b/wled00/wled05_init.ino @@ -40,21 +40,13 @@ void wledInit() DEBUG_PRINTLN("Load EEPROM"); loadSettingsFromEEPROM(true); beginStrip(); - userBeginPreConnection(); + userSetup(); if (strcmp(clientSSID,"Your_Network") == 0) showWelcomePage = true; WiFi.persistent(false); if (macroBoot>0) applyMacro(macroBoot); 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 escapedMac = WiFi.macAddress(); escapedMac.replace(":", ""); @@ -77,51 +69,25 @@ void wledInit() strip.service(); - //HTTP server page init - initServer(); - - strip.service(); - - server.begin(); - DEBUG_PRINTLN("HTTP server started"); - - //init ArduinoOTA - if (true) { - #ifndef WLED_DISABLE_OTA + #ifndef WLED_DISABLE_OTA if (aOtaEnabled) { ArduinoOTA.onStart([]() { - #ifndef ARDUINO_ARCH_ESP32 + #ifdef ESP8266 wifi_set_sleep_type(NONE_SLEEP_T); #endif DEBUG_PRINTLN("Start ArduinoOTA"); }); if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS); - ArduinoOTA.begin(); } - #endif + #endif + + //HTTP server page init + initServer(); - 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; - } + strip.service(); initConnection(); - userBegin(); } @@ -168,6 +134,14 @@ void initAP(bool resetAP=false){ 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.start(53, "*", WiFi.softAPIP()); } @@ -188,7 +162,12 @@ void initConnection() 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) { initAP(); @@ -198,20 +177,13 @@ void initConnection() WiFi.softAPdisconnect(true); } } - - if (!WLED_WIFI_CONFIGURED) - { - DEBUG_PRINT("No connection configured. "); - initAP(); //instantly go to ap mode - return; - } showWelcomePage = false; DEBUG_PRINT("Connecting to "); DEBUG_PRINT(clientSSID); DEBUG_PRINTLN("..."); - #ifndef ARDUINO_ARCH_ESP32 + #ifdef ESP8266 WiFi.hostname(serverDescription); #endif WiFi.begin(clientSSID, clientPass); @@ -221,6 +193,9 @@ void initConnection() } void initInterfaces() { + DEBUG_PRINTLN("Init STA interfaces"); + server.begin(); + if (hueIP[0] == 0) { hueIP[0] = WiFi.localIP()[0]; @@ -231,45 +206,43 @@ void initInterfaces() { //init Alexa hue emulation if (alexaEnabled) alexaInit(); - initMqtt(); - #ifndef WLED_DISABLE_OTA - if (aOtaEnabled) - { - 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(); - } + if (aOtaEnabled) ArduinoOTA.begin(); #endif strip.service(); // Set up mDNS responder: if (strlen(cmDNS) > 0) { - MDNS.begin(cmDNS); - DEBUG_PRINTLN("mDNS responder started"); - // Add service to MDNS - MDNS.addService("http", "tcp", 80); - MDNS.addService("wled", "tcp", 80); + if (MDNS.begin(cmDNS)) + { + MDNS.addService("http", "tcp", 80); + MDNS.addService("wled", "tcp", 80); + DEBUG_PRINTLN("mDNS started"); + } else { + DEBUG_PRINTLN("mDNS failed!"); + } + DEBUG_PRINTLN("mDNS started"); } strip.service(); + if (ntpEnabled && WLED_CONNECTED) + ntpConnected = ntpUdp.begin(ntpLocalPort); + initBlynk(blynkApiKey); initE131(); reconnectHue(); - + initMqtt(); interfacesInited = true; } byte stacO = 0; void handleConnection() { - byte stac = wifi_softap_get_station_num(); + byte stac = 0; + #ifdef ESP8266 + stac = wifi_softap_get_station_num(); + #endif if (stac != stacO) { stacO = stac; @@ -300,6 +273,7 @@ void handleConnection() { DEBUG_PRINT("Connected! IP address: "); DEBUG_PRINTLN(WiFi.localIP()); initInterfaces(); + userConnected(); //shut down AP if (!apAlwaysOn && apActive) diff --git a/wled00/wled06_usermod.ino b/wled00/wled06_usermod.ino index 71f6853b9..010d0f386 100644 --- a/wled00/wled06_usermod.ino +++ b/wled00/wled06_usermod.ino @@ -7,16 +7,19 @@ //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() { diff --git a/wled00/wled07_notify.ino b/wled00/wled07_notify.ino index 21e441556..68c9d9ff8 100644 --- a/wled00/wled07_notify.ino +++ b/wled00/wled07_notify.ino @@ -99,7 +99,7 @@ void initE131(){ void handleE131(){ //E1.31 protocol support - if(e131Enabled) { + if(WLED_CONNECTED && e131Enabled) { uint16_t len = e131->parsePacket(); if (!len || e131->universe < e131Universe || e131->universe > e131Universe +4) return; len /= 3; //one LED is 3 DMX channels diff --git a/wled00/wled08_led.ino b/wled00/wled08_led.ino index 58399a220..49b6e0c55 100644 --- a/wled00/wled08_led.ino +++ b/wled00/wled08_led.ino @@ -142,7 +142,7 @@ void updateInterfaces(uint8_t callMode) } #endif if (callMode != 9 && callMode != 5) updateBlynk(); - publishMqtt(); + doPublishMqtt = true; lastInterfaceUpdate = millis(); } @@ -155,6 +155,7 @@ void handleTransitions() updateInterfaces(interfaceUpdateCallMode); interfaceUpdateCallMode = 0; //disable } + if (doPublishMqtt) publishMqtt(); if (transitionActive && transitionDelayTemp > 0) { diff --git a/wled00/wled17_mqtt.ino b/wled00/wled17_mqtt.ino index 7672508e7..114a88779 100644 --- a/wled00/wled17_mqtt.ino +++ b/wled00/wled17_mqtt.ino @@ -43,15 +43,15 @@ void onMqttConnect(bool sessionPresent) mqtt->subscribe(subuf, 0); } - sendHADiscoveryMQTT(); - publishMqtt(); + doSendHADiscovery = true; + //doPublishMqtt = true; DEBUG_PRINTLN("MQTT ready"); } 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(payload); @@ -72,6 +72,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties void publishMqtt() { + doPublishMqtt = false; if (mqtt == nullptr || !mqtt->connected()) return; 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]; strcpy(bufc, mqttDeviceTopic); @@ -158,7 +162,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB): root["fx_stat_t"] = bufapi; size_t jlen = measureJson(root); - DEBUG_PRINTLN(jlen); + //DEBUG_PRINTLN(jlen); serializeJson(root, buffer, jlen); //add values which don't change @@ -190,7 +194,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB): mdn[namelen] = 0; snprintf(mdnfx, 64, "\"[FX=%02d] %s\",", i, mdn); oappend(mdnfx); - DEBUG_PRINTLN(mdnfx); + //DEBUG_PRINTLN(mdnfx); i++; } isNameStart = !isNameStart; @@ -206,7 +210,8 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB): strcpy(pubt, "homeassistant/light/"); strcat(pubt, mqttClientID); strcat(pubt, "/config"); - mqtt->publish(pubt, 0, true, buffer); + DEBUG_PRINTLN(mqtt->publish(pubt, 0, true, buffer)); + yield(); #endif }