From 7480b739708fa3cc327906db9e98654b210b5385 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Fri, 29 Jan 2021 00:58:23 -0500 Subject: [PATCH] v1.2.2 ### Releases v1.2.2 1. Fix rare Config Portal bug not updating Config and dynamic Params data successfully in very noisy or weak WiFi situation --- README.md | 35 +++++--- library.json | 2 +- library.properties | 2 +- src/BlynkSimpleEsp32_Async_WM.h | 109 +++++++++++++++++-------- src/BlynkSimpleEsp32_SSL_Async_WM.h | 109 +++++++++++++++++-------- src/BlynkSimpleEsp8266_Async_WM.h | 111 +++++++++++++++++--------- src/BlynkSimpleEsp8266_SSL_Async_WM.h | 109 +++++++++++++++++-------- 7 files changed, 321 insertions(+), 156 deletions(-) diff --git a/README.md b/README.md index f773c77..45b0960 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ * [Why using Async](#why-using-async) * [Currently supported Boards](#currently-supported-boards) * [Changelog](#changelog) + * [Releases v1.2.2](#releases-v122) * [Releases v1.2.1](#releases-v121) * [Major Releases v1.2.0](#major-releases-v120) * [Releases v1.1.0](#releases-v110) @@ -143,6 +144,10 @@ This [**BlynkESP32_BT_WF** library](https://github.com/khoih-prog/BlynkESP32_BT_ ## Changelog +### Releases v1.2.2 + +1. Fix rare Config Portal bug not updating Config and dynamic Params data successfully in very noisy or weak WiFi situation + ### Releases v1.2.1 1. Add functions to control Config Portal from software or Virtual Switches. Check [How to trigger a Config Portal from code #25](https://github.com/khoih-prog/Blynk_WM/issues/25) @@ -1203,7 +1208,7 @@ The following is the sample terminal output when running example [Async_ESP8266W ``` Starting Async_ESP8266WM_MRD_Config using LittleFS with SSL on ESP8266_NODEMCU -Blynk_Async_WM SSL for ESP8266 v1.2.1 +Blynk_Async_WM SSL for ESP8266 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1283,7 +1288,7 @@ BBBBBB ``` Starting Async_ESP8266WM_MRD_Config using LittleFS with SSL on ESP8266_NODEMCU -Blynk_Async_WM SSL for ESP8266 v1.2.1 +Blynk_Async_WM SSL for ESP8266 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1394,7 +1399,7 @@ F[229521] id: = HueNet1 ``` Starting Async_ESP8266WM_MRD_Config using LittleFS with SSL on ESP8266_NODEMCU -Blynk_Async_WM SSL for ESP8266 v1.2.1 +Blynk_Async_WM SSL for ESP8266 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1480,7 +1485,7 @@ The following is the sample terminal output when running example [Async_ESP32WM_ ``` Starting Async_ESP32WM_MRD_Config using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1559,7 +1564,7 @@ Pubs Topics = default-mqtt-PubTopic ``` Starting Async_ESP32WM_MRD_Config using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1664,7 +1669,7 @@ RFRFRF[188660] id: = HueNet1 ``` Starting Async_ESP32WM_MRD_Config using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1801,7 +1806,7 @@ The following is the sample terminal output when running example [Async_ESP32_Mu ``` Starting Async_ESP32_MultiTask using LittleFS without SSL on ESP32_DEV -Blynk_Async_WM for ESP32 v1.2.1 +Blynk_Async_WM for ESP32 v1.2.2 ESP_DoubleResetDetector v1.1.1 [1431] Hostname=ESP32-Async-MTask [1517] LoadCfgFile @@ -2013,7 +2018,7 @@ Blynk.resetAndEnterConfigPortal(); ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2104,7 +2109,7 @@ Non-Persistent CP will be removed after first reset, even you didn't enter the C ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2219,7 +2224,7 @@ RF[66298] id: = HueNet1 ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2318,7 +2323,7 @@ Blynk.resetAndEnterConfigPortalPersistent(); ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2409,7 +2414,7 @@ Persistent CP will remain after resets. The only way to get rid of Config Portal ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2521,7 +2526,7 @@ Enter CP, input (even fake data or none) and `Save` config data to exit persiste ``` Starting Async_ESP32WM_MRD_ForcedConfig using LittleFS with SSL on ESP32_DEV -Blynk_Async_WM SSL for ESP32 v1.2.1 +Blynk_Async_WM SSL for ESP32 v1.2.2 ESP_MultiResetDetector v1.1.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2635,6 +2640,10 @@ Sometimes, the library will only work if you update the board core to the latest ## Releases +### Releases v1.2.2 + +1. Fix rare Config Portal bug not updating Config and dynamic Params data successfully in very noisy or weak WiFi situation + ### Releases v1.2.1 1. Add functions to control Config Portal from software or Virtual Switches. Check [How to trigger a Config Portal from code #25](https://github.com/khoih-prog/Blynk_WM/issues/25) diff --git a/library.json b/library.json index 010a7c6..9ea42d4 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Blynk_Async_WM", - "version": "1.2.1", + "version": "1.2.2", "description": "Library, using AsyncWebServer instead of (ESP8266)WebServer, for configuring/auto(re)connecting ESP8266/ESP32 modules to best or available MultiWiFi APs and MultiBlynk servers at runtime. Enable adding dynamic custom parameters from sketch and input using the same Config Portal. Config Portal will be auto-adjusted to match the number of dynamic parameters. Optional default Credentials to be autoloaded into Config Portal to use or change instead of manually input. Static STA IP and DHCP Hostname as well as Config Portal AP channel, IP, SSID, Password can be configured. Multi or Double DetectDetector feature permits entering Config Portal as requested.", "keywords": "sensors, control, device, smartphone, mobile, app, web, cloud, communication, protocol, iot, m2m, wifi, ble, bluetooth, ethernet, usb, serial, gsm, gprs, 3g, data, esp8266, esp32, http, drd, mrd, double-reset, multi-reset, configportal, portal, credentials, async", "authors": diff --git a/library.properties b/library.properties index 1fe176b..399c4f2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Blynk_Async_WM -version=1.2.1 +version=1.2.2 author=Khoi Hoang license=MIT maintainer=Khoi Hoang diff --git a/src/BlynkSimpleEsp32_Async_WM.h b/src/BlynkSimpleEsp32_Async_WM.h index d5ee1a7..dbf5f51 100644 --- a/src/BlynkSimpleEsp32_Async_WM.h +++ b/src/BlynkSimpleEsp32_Async_WM.h @@ -17,7 +17,7 @@ @date Jan 2015 @brief - Version: 1.2.1 + Version: 1.2.2 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -26,6 +26,7 @@ 1.1.0 K Hoang 26/11/2020 Add examples using RTOS MultiTask to avoid blocking in operation. 1.2.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD 1.2.1 K Hoang 16/01/2021 Add functions to control Config Portal from software or Virtual Switches + 1.2.2 K Hoang 28/01/2021 Fix Config Portal and Dynamic Params bugs ********************************************************************************************************************************/ #pragma once @@ -34,7 +35,7 @@ #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif -#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM for ESP32 v1.2.1" +#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM for ESP32 v1.2.2" #define BLYNK_SEND_ATOMIC @@ -197,6 +198,7 @@ typedef struct #if USE_DYNAMIC_PARAMETERS extern uint16_t NUM_MENU_ITEMS; extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; #endif #define SSID_MAX_LEN 32 @@ -2033,100 +2035,134 @@ class BlynkWifi memset(&BlynkESP32_WM_config, 0, sizeof(BlynkESP32_WM_config)); strcpy(BlynkESP32_WM_config.header, BLYNK_BOARD_TYPE); } - - if (key == "id") + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) { - BLYNK_LOG2(BLYNK_F("id: = "), value.c_str()); + // Don't need to free + menuItemUpdated = new bool[NUM_MENU_ITEMS]; + if (menuItemUpdated) + { + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + // To flag item is not yet updated + menuItemUpdated[i] = false; + } + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("h: Init menuItemUpdated" )); + #endif + } + else + { + BLYNK_LOG1(BLYNK_F("h: Error can't alloc memory for menuItemUpdated" )); + } + } +#endif + + static bool id_Updated = false; + static bool pw_Updated = false; + static bool id1_Updated = false; + static bool pw1_Updated = false; + static bool sv_Updated = false; + static bool tk_Updated = false; + static bool sv1_Updated = false; + static bool tk1_Updated = false; + static bool pt_Updated = false; + static bool nm_Updated = false; + + if (!id_Updated && (key == String("id"))) + { + id_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { - BLYNK_LOG2(BLYNK_F("pw = "), value.c_str()); - + pw_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw) - 1); } - - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { - BLYNK_LOG2(BLYNK_F("id1 = "), value.c_str()); - + id1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { - BLYNK_LOG2(BLYNK_F("pw1 = "), value.c_str()); - + pw1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { - BLYNK_LOG2(BLYNK_F("sv = "), value.c_str()); - + sv_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "tk") + else if (!tk_Updated && (key == String("tk"))) { - BLYNK_LOG2(BLYNK_F("tk = "), value.c_str()); - + tk_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { - BLYNK_LOG2(BLYNK_F("sv1 = "), value.c_str()); - + sv1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "tk1") + else if (!tk1_Updated && (key == String("tk1"))) { - BLYNK_LOG2(BLYNK_F("tk1 = "), value.c_str()); - + tk1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token) - 1); } - else if (key == "pt") + else if (!pt_Updated && (key == String("pt"))) { - BLYNK_LOG2(BLYNK_F("pt = "), value.toInt()); - + pt_Updated = true; number_items_Updated++; + BlynkESP32_WM_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { - BLYNK_LOG2(BLYNK_F("nm = "), value.c_str()); - + nm_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.board_name) - 1) strcpy(BlynkESP32_WM_config.board_name, value.c_str()); else @@ -2136,9 +2172,12 @@ class BlynkWifi #if USE_DYNAMIC_PARAMETERS for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - if (key == myMenuItems[i].id) + if ( !menuItemUpdated[i] && (key == myMenuItems[i].id) ) { - //BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; + number_items_Updated++; // Actual size of pdata is [maxlen + 1] diff --git a/src/BlynkSimpleEsp32_SSL_Async_WM.h b/src/BlynkSimpleEsp32_SSL_Async_WM.h index 31eb03d..94618c0 100644 --- a/src/BlynkSimpleEsp32_SSL_Async_WM.h +++ b/src/BlynkSimpleEsp32_SSL_Async_WM.h @@ -17,7 +17,7 @@ @date Jan 2015 @brief - Version: 1.2.1 + Version: 1.2.2 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -26,6 +26,7 @@ 1.1.0 K Hoang 26/11/2020 Add examples using RTOS MultiTask to avoid blocking in operation. 1.2.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD 1.2.1 K Hoang 16/01/2021 Add functions to control Config Portal from software or Virtual Switches + 1.2.2 K Hoang 28/01/2021 Fix Config Portal and Dynamic Params bugs ********************************************************************************************************************************/ #pragma once @@ -34,7 +35,7 @@ #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif -#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM SSL for ESP32 v1.2.1" +#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM SSL for ESP32 v1.2.2" #if defined(BLYNK_SSL_USE_LETSENCRYPT) static const char BLYNK_DEFAULT_ROOT_CA[] = @@ -261,6 +262,7 @@ typedef struct #if USE_DYNAMIC_PARAMETERS extern uint16_t NUM_MENU_ITEMS; extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; #endif #define SSID_MAX_LEN 32 @@ -2100,100 +2102,134 @@ class BlynkWifi memset(&BlynkESP32_WM_config, 0, sizeof(BlynkESP32_WM_config)); strcpy(BlynkESP32_WM_config.header, BLYNK_BOARD_TYPE); } - - if (key == "id") + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) { - BLYNK_LOG2(BLYNK_F("id: = "), value.c_str()); + // Don't need to free + menuItemUpdated = new bool[NUM_MENU_ITEMS]; + if (menuItemUpdated) + { + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + // To flag item is not yet updated + menuItemUpdated[i] = false; + } + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("h: Init menuItemUpdated" )); + #endif + } + else + { + BLYNK_LOG1(BLYNK_F("h: Error can't alloc memory for menuItemUpdated" )); + } + } +#endif + + static bool id_Updated = false; + static bool pw_Updated = false; + static bool id1_Updated = false; + static bool pw1_Updated = false; + static bool sv_Updated = false; + static bool tk_Updated = false; + static bool sv1_Updated = false; + static bool tk1_Updated = false; + static bool pt_Updated = false; + static bool nm_Updated = false; + + if (!id_Updated && (key == String("id"))) + { + id_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { - BLYNK_LOG2(BLYNK_F("pw = "), value.c_str()); - + pw_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[0].wifi_pw) - 1); } - - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { - BLYNK_LOG2(BLYNK_F("id1 = "), value.c_str()); - + id1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { - BLYNK_LOG2(BLYNK_F("pw1 = "), value.c_str()); - + pw1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw) - 1) strcpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw, value.c_str()); else strncpy(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(BlynkESP32_WM_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { - BLYNK_LOG2(BLYNK_F("sv = "), value.c_str()); - + sv_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "tk") + else if (!tk_Updated && (key == String("tk"))) { - BLYNK_LOG2(BLYNK_F("tk = "), value.c_str()); - + tk_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[0].blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { - BLYNK_LOG2(BLYNK_F("sv1 = "), value.c_str()); - + sv1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "tk1") + else if (!tk1_Updated && (key == String("tk1"))) { - BLYNK_LOG2(BLYNK_F("tk1 = "), value.c_str()); - + tk1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token) - 1) strcpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token, value.c_str()); else strncpy(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token, value.c_str(), sizeof(BlynkESP32_WM_config.Blynk_Creds[1].blynk_token) - 1); } - else if (key == "pt") + else if (!pt_Updated && (key == String("pt"))) { - BLYNK_LOG2(BLYNK_F("pt = "), value.toInt()); - + pt_Updated = true; number_items_Updated++; + BlynkESP32_WM_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { - BLYNK_LOG2(BLYNK_F("nm = "), value.c_str()); - + nm_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(BlynkESP32_WM_config.board_name) - 1) strcpy(BlynkESP32_WM_config.board_name, value.c_str()); else @@ -2203,9 +2239,12 @@ class BlynkWifi #if USE_DYNAMIC_PARAMETERS for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - if (key == myMenuItems[i].id) + if ( !menuItemUpdated[i] && (key == myMenuItems[i].id) ) { - //BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; + number_items_Updated++; // Actual size of pdata is [maxlen + 1] diff --git a/src/BlynkSimpleEsp8266_Async_WM.h b/src/BlynkSimpleEsp8266_Async_WM.h index f3ea077..aa6fd27 100644 --- a/src/BlynkSimpleEsp8266_Async_WM.h +++ b/src/BlynkSimpleEsp8266_Async_WM.h @@ -17,7 +17,7 @@ @date Jan 2015 @brief - Version: 1.2.1 + Version: 1.2.2 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -26,6 +26,7 @@ 1.1.0 K Hoang 26/11/2020 Add examples using RTOS MultiTask to avoid blocking in operation. 1.2.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD 1.2.1 K Hoang 16/01/2021 Add functions to control Config Portal from software or Virtual Switches + 1.2.2 K Hoang 28/01/2021 Fix Config Portal and Dynamic Params bugs ********************************************************************************************************************************/ #pragma once @@ -34,7 +35,7 @@ #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. #endif -#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM for ESP8266 v1.2.1" +#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM for ESP8266 v1.2.2" #include @@ -187,6 +188,7 @@ typedef struct #if USE_DYNAMIC_PARAMETERS extern uint16_t NUM_MENU_ITEMS; extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; #endif #define SSID_MAX_LEN 32 @@ -1495,7 +1497,7 @@ class BlynkWifi #endif #endif -// Stating positon to store BlynkESP32_WM_config +// Stating positon to store Blynk8266_WM_config #define BLYNK_EEPROM_START (EEPROM_START + FLAG_DATA_SIZE) ////////////////////////////////////////////// @@ -2015,100 +2017,134 @@ class BlynkWifi memset(&Blynk8266_WM_config, 0, sizeof(Blynk8266_WM_config)); strcpy(Blynk8266_WM_config.header, BLYNK_BOARD_TYPE); } - - if (key == "id") + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) { - BLYNK_LOG2(BLYNK_F("id: = "), value.c_str()); + // Don't need to free + menuItemUpdated = new bool[NUM_MENU_ITEMS]; + if (menuItemUpdated) + { + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + // To flag item is not yet updated + menuItemUpdated[i] = false; + } + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("h: Init menuItemUpdated" )); + #endif + } + else + { + BLYNK_LOG1(BLYNK_F("h: Error can't alloc memory for menuItemUpdated" )); + } + } +#endif + + static bool id_Updated = false; + static bool pw_Updated = false; + static bool id1_Updated = false; + static bool pw1_Updated = false; + static bool sv_Updated = false; + static bool tk_Updated = false; + static bool sv1_Updated = false; + static bool tk1_Updated = false; + static bool pt_Updated = false; + static bool nm_Updated = false; + + if (!id_Updated && (key == String("id"))) + { + id_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { - BLYNK_LOG2(BLYNK_F("pw = "), value.c_str()); - + pw_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw) - 1); } - - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { - BLYNK_LOG2(BLYNK_F("id1 = "), value.c_str()); - + id1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { - BLYNK_LOG2(BLYNK_F("pw1 = "), value.c_str()); - + pw1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { - BLYNK_LOG2(BLYNK_F("sv = "), value.c_str()); - + sv_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_server) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_server, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "tk") + else if (!tk_Updated && (key == String("tk"))) { - BLYNK_LOG2(BLYNK_F("tk = "), value.c_str()); - + tk_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_token) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_token, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_token, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { - BLYNK_LOG2(BLYNK_F("sv1 = "), value.c_str()); - + sv1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_server) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_server, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "tk1") + else if (!tk1_Updated && (key == String("tk1"))) { - BLYNK_LOG2(BLYNK_F("tk1 = "), value.c_str()); - + tk1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_token) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_token, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_token, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_token) - 1); } - else if (key == "pt") + else if (!pt_Updated && (key == String("pt"))) { - BLYNK_LOG2(BLYNK_F("pt = "), value.toInt()); - + pt_Updated = true; number_items_Updated++; + Blynk8266_WM_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { - BLYNK_LOG2(BLYNK_F("nm = "), value.c_str()); - + nm_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.board_name) - 1) strcpy(Blynk8266_WM_config.board_name, value.c_str()); else @@ -2118,9 +2154,12 @@ class BlynkWifi #if USE_DYNAMIC_PARAMETERS for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - if (key == myMenuItems[i].id) + if ( !menuItemUpdated[i] && (key == myMenuItems[i].id) ) { - //BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; + number_items_Updated++; // Actual size of pdata is [maxlen + 1] diff --git a/src/BlynkSimpleEsp8266_SSL_Async_WM.h b/src/BlynkSimpleEsp8266_SSL_Async_WM.h index 0fb3bc0..0cc32ca 100644 --- a/src/BlynkSimpleEsp8266_SSL_Async_WM.h +++ b/src/BlynkSimpleEsp8266_SSL_Async_WM.h @@ -17,7 +17,7 @@ @date Jan 2015 @brief - Version: 1.2.1 + Version: 1.2.2 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -26,6 +26,7 @@ 1.1.0 K Hoang 26/11/2020 Add examples using RTOS MultiTask to avoid blocking in operation. 1.2.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD 1.2.1 K Hoang 16/01/2021 Add functions to control Config Portal from software or Virtual Switches + 1.2.2 K Hoang 28/01/2021 Fix Config Portal and Dynamic Params bugs ********************************************************************************************************************************/ #pragma once @@ -34,7 +35,7 @@ #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. #endif -#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM SSL for ESP8266 v1.2.1" +#define BLYNK_ASYNC_WM_VERSION "Blynk_Async_WM SSL for ESP8266 v1.2.2" #include @@ -278,6 +279,7 @@ typedef struct #if USE_DYNAMIC_PARAMETERS extern uint16_t NUM_MENU_ITEMS; extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; #endif #define SSID_MAX_LEN 32 @@ -2125,100 +2127,134 @@ class BlynkWifi memset(&Blynk8266_WM_config, 0, sizeof(Blynk8266_WM_config)); strcpy(Blynk8266_WM_config.header, BLYNK_BOARD_TYPE); } - - if (key == "id") + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) { - BLYNK_LOG2(BLYNK_F("id: = "), value.c_str()); + // Don't need to free + menuItemUpdated = new bool[NUM_MENU_ITEMS]; + if (menuItemUpdated) + { + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + // To flag item is not yet updated + menuItemUpdated[i] = false; + } + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("h: Init menuItemUpdated" )); + #endif + } + else + { + BLYNK_LOG1(BLYNK_F("h: Error can't alloc memory for menuItemUpdated" )); + } + } +#endif + + static bool id_Updated = false; + static bool pw_Updated = false; + static bool id1_Updated = false; + static bool pw1_Updated = false; + static bool sv_Updated = false; + static bool tk_Updated = false; + static bool sv1_Updated = false; + static bool tk1_Updated = false; + static bool pt_Updated = false; + static bool nm_Updated = false; + + if (!id_Updated && (key == String("id"))) + { + id_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { - BLYNK_LOG2(BLYNK_F("pw = "), value.c_str()); - + pw_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[0].wifi_pw) - 1); } - - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { - BLYNK_LOG2(BLYNK_F("id1 = "), value.c_str()); - + id1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { - BLYNK_LOG2(BLYNK_F("pw1 = "), value.c_str()); - + pw1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw) - 1) strcpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw, value.c_str()); else strncpy(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(Blynk8266_WM_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { - BLYNK_LOG2(BLYNK_F("sv = "), value.c_str()); - + sv_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_server) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_server, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "tk") + else if (!tk_Updated && (key == String("tk"))) { - BLYNK_LOG2(BLYNK_F("tk = "), value.c_str()); - + tk_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_token) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_token, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[0].blynk_token, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[0].blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { - BLYNK_LOG2(BLYNK_F("sv1 = "), value.c_str()); - + sv1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_server) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_server, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "tk1") + else if (!tk1_Updated && (key == String("tk1"))) { - BLYNK_LOG2(BLYNK_F("tk1 = "), value.c_str()); - + tk1_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_token) - 1) strcpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_token, value.c_str()); else strncpy(Blynk8266_WM_config.Blynk_Creds[1].blynk_token, value.c_str(), sizeof(Blynk8266_WM_config.Blynk_Creds[1].blynk_token) - 1); } - else if (key == "pt") + else if (!pt_Updated && (key == String("pt"))) { - BLYNK_LOG2(BLYNK_F("pt = "), value.toInt()); - + pt_Updated = true; number_items_Updated++; + Blynk8266_WM_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { - BLYNK_LOG2(BLYNK_F("nm = "), value.c_str()); - + nm_Updated = true; number_items_Updated++; + if (strlen(value.c_str()) < sizeof(Blynk8266_WM_config.board_name) - 1) strcpy(Blynk8266_WM_config.board_name, value.c_str()); else @@ -2228,9 +2264,12 @@ class BlynkWifi #if USE_DYNAMIC_PARAMETERS for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - if (key == myMenuItems[i].id) + if ( !menuItemUpdated[i] && (key == myMenuItems[i].id) ) { - //BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; + number_items_Updated++; // Actual size of pdata is [maxlen + 1]