From caef289b9b24df610bc700d6fdd6677d9262030c Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 20 Nov 2022 15:50:42 +0100 Subject: [PATCH 1/4] Autosave enable/disable UI button --- .../usermod_v2_auto_save.h | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h index 41e0a7ecf..8283aeed1 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h @@ -91,6 +91,10 @@ class AutoSaveUsermod : public Usermod { #endif } + void enable(bool enable) { + enabled = enable; + } + public: // gets called once at boot. Do all initialization that doesn't depend on @@ -155,12 +159,24 @@ class AutoSaveUsermod : public Usermod { * Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. * Below it is shown how this could be used for e.g. a light sensor */ - //void addToJsonInfo(JsonObject& root) { - //JsonObject user = root["u"]; - //if (user.isNull()) user = root.createNestedObject("u"); - //JsonArray data = user.createNestedArray(F("Autosave")); - //data.add(F("Loaded.")); - //} + void addToJsonInfo(JsonObject& root) { + JsonObject user = root["u"]; + if (user.isNull()) { + user = root.createNestedObject("u"); + } + + JsonArray infoArr = user.createNestedArray(FPSTR(_name)); // name + + String uiDomString = F(""); + infoArr.add(uiDomString); + } /* * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). @@ -173,9 +189,20 @@ class AutoSaveUsermod : public Usermod { * readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). * Values in the state object may be modified by connected clients */ - //void readFromJsonState(JsonObject& root) { - // if (!initDone) return; // prevent crash on boot applyPreset() - //} + void readFromJsonState(JsonObject& root) { + if (!initDone) return; // prevent crash on boot applyPreset() + bool en = enabled; + JsonObject um = root[FPSTR(_name)]; + if (!um.isNull()) { + if (um[FPSTR(_autoSaveEnabled)].is()) { + en = um[FPSTR(_autoSaveEnabled)].as(); + } else { + String str = um[FPSTR(_autoSaveEnabled)]; // checkbox -> off or on + en = (bool)(str!="off"); // off is guaranteed to be present + } + if (en != enabled) enable(en); + } + } /* * addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object. From e409bd298a32ed9606c7b379b2d3896a35e7ad34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jason=20K=C3=B6lker?= Date: Sun, 20 Nov 2022 08:55:38 -0600 Subject: [PATCH 2/4] feat(wifi): add compile time configurtation (#2889) * feat(wifi): add compile time configurtation Add `WLED_AP_SSID` and `WLED_AP_PASS` defines to allow configuring the SoftAP SSID and Password at compile time. Default to existing values. Add `WLED_AP_SSID_UNIQUE` flag to append the device portion of the mac address to `WLED_AP_SSID`. Exampleof all flags (note the quoting to preserve "stringification"): ``` build_flags = -D WLED_AP_SSID='"MyAP"' -D WLED_AP_PASS='"MyPassword"' -D WLED_AP_SSID_UNIQUE ``` * Removed two error defines Co-authored-by: Christian Schwinne --- wled00/const.h | 1 + wled00/wled.cpp | 16 +++++++++------- wled00/wled.h | 32 +++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/wled00/const.h b/wled00/const.h index f092fe63b..7d32b200a 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -9,6 +9,7 @@ //Defaults #define DEFAULT_CLIENT_SSID "Your_Network" +#define DEFAULT_AP_SSID "WLED-AP" #define DEFAULT_AP_PASS "wled1234" #define DEFAULT_OTA_PASS "wledota" diff --git a/wled00/wled.cpp b/wled00/wled.cpp index c26ad85a3..594a41d03 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -366,7 +366,13 @@ void WLED::setup() #endif updateFSInfo(); - strcpy_P(apSSID, PSTR("WLED-AP")); // otherwise it is empty on first boot until config is saved + // generate module IDs must be done before AP setup + escapedMac = WiFi.macAddress(); + escapedMac.replace(":", ""); + escapedMac.toLowerCase(); + + WLED_SET_AP_SSID(); // otherwise it is empty on first boot until config is saved + DEBUG_PRINTLN(F("Reading config")); deserializeConfigFromFS(); @@ -400,10 +406,6 @@ void WLED::setup() } #endif - // generate module IDs - escapedMac = WiFi.macAddress(); - escapedMac.replace(":", ""); - escapedMac.toLowerCase(); // fill in unique mdns default if (strcmp(cmDNS, "x") == 0) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6); if (mqttDeviceTopic[0] == 0) sprintf_P(mqttDeviceTopic, PSTR("wled/%*s"), 6, escapedMac.c_str() + 6); @@ -480,8 +482,8 @@ void WLED::initAP(bool resetAP) return; if (resetAP) { - strcpy_P(apSSID, PSTR("WLED-AP")); - strcpy_P(apPass, PSTR(DEFAULT_AP_PASS)); + WLED_SET_AP_SSID(); + strcpy_P(apPass, PSTR(WLED_AP_PASS)); } DEBUG_PRINT(F("Opening access point ")); DEBUG_PRINTLN(apSSID); diff --git a/wled00/wled.h b/wled00/wled.h index f7dd1703c..44e080007 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -175,6 +175,19 @@ using PSRAMDynamicJsonDocument = BasicJsonDocument; #define CLIENT_PASS "" #endif +#if defined(WLED_AP_PASS) && !defined(WLED_AP_SSID) + #error WLED_AP_PASS is defined but WLED_AP_SSID is still the default. \ + Please change WLED_AP_SSID to something unique. +#endif + +#ifndef WLED_AP_SSID + #define WLED_AP_SSID DEFAULT_AP_SSID +#endif + +#ifndef WLED_AP_PASS + #define WLED_AP_PASS DEFAULT_AP_PASS +#endif + #ifndef SPIFFS_EDITOR_AIRCOOOKIE #error You are not using the Aircoookie fork of the ESPAsyncWebserver library.\ Using upstream puts your WiFi password at risk of being served by the filesystem.\ @@ -229,7 +242,7 @@ WLED_GLOBAL char versionString[] _INIT(TOSTRING(WLED_VERSION)); #define WLED_CODENAME "Hoshi" // AP and OTA default passwords (for maximum security change them!) -WLED_GLOBAL char apPass[65] _INIT(DEFAULT_AP_PASS); +WLED_GLOBAL char apPass[65] _INIT(WLED_AP_PASS); WLED_GLOBAL char otaPass[33] _INIT(DEFAULT_OTA_PASS); // Hardware and pin config @@ -727,6 +740,23 @@ WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); #define WLED_WIFI_CONFIGURED (strlen(clientSSID) >= 1 && strcmp(clientSSID, DEFAULT_CLIENT_SSID) != 0) #define WLED_MQTT_CONNECTED (mqtt != nullptr && mqtt->connected()) +#ifndef WLED_AP_SSID_UNIQUE + #define WLED_SET_AP_SSID() do { \ + strcpy_P(apSSID, PSTR(WLED_AP_SSID)); \ + } while(0) +#else + #define WLED_SET_AP_SSID() do { \ + strcpy_P(apSSID, PSTR(WLED_AP_SSID)); \ + snprintf_P(\ + apSSID+strlen(WLED_AP_SSID), \ + sizeof(apSSID)-strlen(WLED_AP_SSID), \ + PSTR("-%*s"), \ + 6, \ + escapedMac.c_str() + 6\ + ); \ + } while(0) +#endif + //macro to convert F to const #define SET_F(x) (const char*)F(x) From 1b351b7743194747e146fcfec29f370772d3293e Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 20 Nov 2022 18:12:01 +0100 Subject: [PATCH 3/4] Broadcast presence on WiFi (re)connect immediately --- wled00/wled.cpp | 3 ++- wled00/wled.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 594a41d03..0442e3c57 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -133,7 +133,7 @@ void WLED::loop() ntpLastSyncTime = 0; strip.restartRuntime(); } - if (millis() - lastMqttReconnectAttempt > 30000) { + if (millis() - lastMqttReconnectAttempt > 30000 || lastMqttReconnectAttempt == 0) { // lastMqttReconnectAttempt==0 forces immediate broadcast lastMqttReconnectAttempt = millis(); initMqtt(); yield(); @@ -804,6 +804,7 @@ void WLED::handleConnection() initInterfaces(); userConnected(); usermods.connected(); + lastMqttReconnectAttempt = 0; // force immediate update // shut down AP if (apBehavior != AP_BEHAVIOR_ALWAYS && apActive) { diff --git a/wled00/wled.h b/wled00/wled.h index 44e080007..6fd4523cc 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2211190 +#define VERSION 2211200 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 0a1bd748d72771d2b2b70a9a8e8f09f3eb264f38 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 20 Nov 2022 19:40:54 +0100 Subject: [PATCH 4/4] Fix loading transitionDelay on boot --- wled00/cfg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index d12176eeb..6849e273e 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -318,7 +318,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { JsonObject light_tr = light["tr"]; CJSON(fadeTransition, light_tr["mode"]); int tdd = light_tr["dur"] | -1; - if (tdd >= 0) transitionDelayDefault = tdd * 100; + if (tdd >= 0) transitionDelay = transitionDelayDefault = tdd * 100; CJSON(strip.paletteFade, light_tr["pal"]); JsonObject light_nl = light["nl"];