diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0b33e48..219e8ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,8 +14,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following: -* Arduino IDE version (e.g. 1.8.11) or Platform.io version -* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v2.6.3 or ESP32 v1.0.4) +* Arduino IDE version (e.g. 1.8.13) or Platform.io version +* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v2.7.4 or ESP32 v1.0.4) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce * Anything that might be relevant in your opinion, such as: @@ -26,10 +26,10 @@ Please ensure to specify the following: ### Example ``` -Arduino IDE version: 1.8.11 -ESP8266 Core Version 2.6.3 -OS: Ubuntu 16.04 LTS -Linux Inspiron 4.4.0-170-generic #199-Ubuntu SMP Thu Nov 14 01:45:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux +Arduino IDE version: 1.8.13 +ESP8266 Core Version 2.7.4 +OS: Ubuntu 20.04 LTS +Linux Inspiron 5.4.0-60-generic #67-Ubuntu SMP Tue Jan 5 18:31:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux Context: The board couldn't autoreconnect to Local Blynk Server after router power recycling. diff --git a/README.md b/README.md index bf8ae9f..dc952a4 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ * [Supported boards-modules](#supported-boards-modules) * [To be supported boards-modules in the future](#to-be-supported-boards-modules-in-the-future) * [Changelog](#changelog) + * [Major Releases v1.2.0](#major-releases-v120) * [Major Releases v1.1.0](#major-releases-v110) * [Releases v1.0.10](#releases-v1010) * [Major Releases v1.0.9](#major-releases-v109) @@ -64,6 +65,10 @@ * [1. TTGO_TCALL_GSM_SHT3x using SPIFFS on ESP32_DEV](#1-ttgo_tcall_gsm_sht3x-using-spiffs-on-esp32_dev) * [2. TTGO_TCALL_MRD_GSM using LittleFS on ESP32_DEV](#2-ttgo_tcall_mrd_gsm-using-littlefs-on-esp32_dev) * [3. ESP32_GSM using LittleFS on ESP32_DEV](#3-esp32_gsm-using-littlefs-on-esp32_dev) + * [4. TTGO_TCALL_GSM using LittleFS on TTGO_TCALL](#4-ttgo_tcall_gsm-using-littlefs-on-ttgo_tcall) + * [4.1. Normal](#41-normal) + * [4.2. Enter non-persistent ConfigPortal](#42-enter-non-persistent-configportal) + * [4.3. Enter persistent ConfigPortal](#43-enter-persistent-configportal) * [Debug](#debug) * [Troubleshooting](#troubleshooting) * [Releases](#releases) @@ -174,6 +179,14 @@ More modems may be supported later: ## Changelog +### Major Releases v1.2.0 + +1. To permit autoreset after configurable timeout if DRD/MRD or non-persistent forced-CP. Check [**Good new feature: Blynk.resetAndEnterConfigPortal() Thanks & question #27**](https://github.com/khoih-prog/Blynk_WM/issues/27) +2. Fix rare Config Portal bug not updating Config and dynamic Params data successfully in very noisy or weak WiFi situation +3. 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) +4. Add the new Virtual ConfigPortal SW feature to examples. +5. Disable the GSM/GPRS modem initialization which blocks the operation of Config Portal when using Config Portal. + ### Major Releases v1.1.0 1. Add support to LittleFS for ESP32 using [LittleFS_esp32](https://github.com/lorol/LITTLEFS) Library @@ -750,9 +763,36 @@ void loop() #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -814,7 +854,7 @@ void setup() SerialMon.print(F("\nStart TTGO_TCALL_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -844,7 +884,7 @@ void setup() Blynk_WF.setConfigPortal("TestPortal-ESP32", "TestPortalPass"); // Use configurable AP IP, instead of default IP 192.168.4.1 - Blynk_WF.setConfigPortalIP(IPAddress(192, 168, 232, 1)); + //Blynk_WF.setConfigPortalIP(IPAddress(192, 168, 232, 1)); // Set config portal channel, default = 1. Use 0 => random channel from 1-12 to avoid conflict Blynk_WF.setConfigPortalChannel(0); @@ -878,9 +918,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -906,13 +946,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif @@ -999,6 +1041,9 @@ void loop() #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -1048,7 +1093,8 @@ void loop() #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else @@ -1190,9 +1236,9 @@ void loop() "wapuser1", //char gprsPass [32]; "wap", - //char gprsPin [12]; // A PIN (Personal Identification Number) is a 4-8 digit passcode + //char gprsPin [12]; // A PIN (Personal Identification Number) is a 4-8 digit passcode "1245678", - // END OF YOUR GSM / GPRS RELATED + // END OF YOUR GSM / GPRS RELATED //char board_name [24]; "ESP32-GSM-WiFi", //int checkSum, dummy, not used @@ -1217,7 +1263,11 @@ void loop() #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// @@ -1293,7 +1343,7 @@ This is the terminal debug output when running both WiFi and GSM/GPRS at the sam ``` Start TTGO_TCALL_GSM_SHT3x (Simultaneous WiFi+GSM) using SPIFFS on ESP32_DEV -BlynkGSM_Manager v1.1.0 +BlynkGSM_Manager v1.2.0 ESP_MultiResetDetector v1.1.1 Set GSM module baud rate Use WiFi to connect Blynk @@ -1371,7 +1421,7 @@ This is the terminal debug output when running both WiFi and GSM/GPRS at the sam ``` Start TTGO_TCALL_MRD_GSM (Simultaneous WiFi+GSM) using LittleFS on ESP32_DEV -BlynkGSM_Manager v1.1.0 +BlynkGSM_Manager v1.2.0 ESP_MultiResetDetector v1.1.1 Set GSM module baud rate Use WiFi to connect Blynk @@ -1451,7 +1501,7 @@ This is the terminal debug output when running both WiFi and GSM/GPRS at the sam ``` Start ESP32_GSM (Simultaneous WiFi+GSM) using LittleFS on ESP32_DEV -BlynkGSM_Manager v1.1.0 +BlynkGSM_Manager v1.2.0 ESP_DoubleResetDetector v1.1.1 Set GSM module baud rate Use WiFi to connect Blynk @@ -1518,6 +1568,206 @@ MQTT PWD = mqtt-pass Subs Topics = SubTopic_ESP32_GSM Pubs Topics = PubTopic_ESP32_GSM ``` + +--- + +### 4. TTGO_TCALL_GSM using LittleFS on TTGO_TCALL + +This is the terminal debug output when running both WiFi and GSM/GPRS at the same time, using example [TTGO_TCALL_GSM](examples/TTGO_TCALL_GSM) on ESP32-based TTGO-TCALL + +### 4.1. Normal + +``` +Start TTGO_TCALL_GSM (Simultaneous WiFi+GSM) using LittleFS on TTGO_T1 + +BlynkGSM_Manager v1.2.0 +ESP_DoubleResetDetector v1.1.1 +Set GSM module baud rate +Use WiFi to connect Blynk +LittleFS Flag read = 0xD0D04321 +No doubleResetDetected +Saving config file... +Saving config file OK +[3378] Hostname=TTGO-TCALL-GSM +[3406] LoadCfgFile +[3410] OK +[3410] CCSum=0x5558,RCSum=0x5558 +[3424] LoadCredFile +[3428] OK +[3428] CrCCsum=0x1cda,CrRCsum=0x1cda +[3428] Hdr=ESP32_GSM_WFM,BrdName=TTGO_TCALL +[3428] SSID=HueNet1,PW=12345678 +[3428] SSID1=HueNet2,PW1=12345678 +[3431] APN=rogers-core-appl1.apn,User=wapuser1 +[3435] PW=wap,PIN=12345678 +[3438] Server=account.duckdns.org,WiFi_Token=wifi_token,GSM_Token=gsm_token +[3448] Server1=192.168.2.32,WiFi_Token1=wifi_token,GSM_Token1=gsm_token +[3458] Port=8080 +[3460] ======= End Config Data ======= +[3472] LoadCPFile +[3476] OK +[3476] Connecting MultiWifi... +[9810] WiFi connected after time: 1 +[9810] SSID:HueNet1,RSSI=-33 +[9810] Channel:2,IP address:192.168.2.164 +[9810] bg: WiFi OK. Try Blynk +[9811] + ___ __ __ + / _ )/ /_ _____ / /__ + / _ / / // / _ \/ '_/ + /____/_/\_, /_//_/_/\_\ + /___/ v0.6.1 on ESP32 + +[9964] Ready (ping: 5ms). +[10032] Connected to Blynk Server = account.duckdns.org, Token = wifi_token +[10032] bg: WiFi+Blynk OK +gprs apn = rogers-core-appl1.apn +[10032] + ___ __ __ + / _ )/ /_ _____ / /__ + / _ / / // / _ \/ '_/ + /____/_/\_, /_//_/_/\_\ + /___/ v0.6.1 on ESP32 + +[10091] InitModem +[10113] Con2Network +[10124] Network:Rogers Wireless +[10124] Conn2 rogers-core-appl1.apn +[18955] GPRSConOK +[18965] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080 +[19609] Ready (ping: 315ms). +Your stored Credentials : +MQTT Server = mqtt.duckdns.org +Port = 1883 +MQTT UserName = yourName +MQTT PWD = yourPWD +Subs Topics = SubsTopic1 +Pubs Topics = PubsTopic1 +BGBGBGBGBGBGBGBGBGBG BGBGBGBGBGBGBGBGBGBG BGBGBGBGBGBGBGBGBGBG BGBGBGBGBGBGBGBGBGBG +``` + +#### 4.2 Enter non-persistent ConfigPortal + +**Non-Persistent CP will be removed after first reset or time-out, even you didn't enter the CP**. You can optionally enter CP, input and `Save` config data. + +``` +CP Button Hit. Rebooting +[1459459] SaveCPFile +[1459461] OK +[1459466] SaveBkUpCPFile +[1459468] OK +ets Jun 8 2016 00:22:57 + +Start TTGO_TCALL_GSM (Simultaneous WiFi+GSM) using LittleFS on TTGO_T1 + +BlynkGSM_Manager v1.2.0 +ESP_DoubleResetDetector v1.1.1 +Set GSM module baud rate +Use WiFi to connect Blynk +LittleFS Flag read = 0xD0D04321 +No doubleResetDetected +Saving config file... +Saving config file OK +[3340] Hostname=TTGO-TCALL-GSM +[3357] LoadCfgFile +[3360] OK +[3360] CCSum=0x5558,RCSum=0x5558 +[3368] LoadCredFile +[3371] OK +[3371] CrCCsum=0x1cda,CrRCsum=0x1cda +[3371] Hdr=ESP32_GSM_WFM,BrdName=TTGO_TCALL +[3371] SSID=HueNet1,PW=12345678 +[3372] SSID1=HueNet2,PW1=12345678 +[3375] APN=rogers-core-appl1.apn,User=wapuser1 +[3380] PW=wap,PIN=12345678 +[3382] Server=account.duckdns.org,WiFi_Token=wifi_token,GSM_Token=gsm_token +[3392] Server1=192.168.2.32,WiFi_Token1=wifi_token,GSM_Token1=gsm_token +[3403] Port=8080 +[3404] ======= End Config Data ======= +[3414] LoadCPFile +[3416] OK +[3416] bg:Stay forever in CP:Forced-non-Persistent +[3422] SaveCPFile +[3425] OK +[3432] SaveBkUpCPFile +[3435] OK +[4280] +stConf:SSID=TestPortal-ESP32,PW=TestPortalPass +[4280] IP=192.168.4.1,ch=8 +gprs apn = rogers-core-appl1.apn +DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal +FF +Your stored Credentials : +MQTT Server = mqtt.ddns.net +Port = 1883 +MQTT UserName = mqtt-new_user +MQTT PWD = mqtt-password +Subs Topics = SubTopic_ESP32_GSM +Pubs Topics = PubTopic_ESP32_GSM +Stop doubleResetDetecting +Saving config file... +Saving config file OK +``` + +#### 4.3 Enter persistent ConfigPortal + +**Persistent CP will remain even after resets or time-out**. The only way to get rid of Config Portal is to enter CP, input (even fake data or none) and `Save` config data. So be careful to use this feature. + +``` +Persistent CP Button Hit. Rebooting +[130508] SaveCPFile +[130512] OK +[130522] SaveBkUpCPFile +[130526] OK +ets Jun 8 2016 00:22:57 + +Start TTGO_TCALL_GSM (Simultaneous WiFi+GSM) using LittleFS on TTGO_T1 + +BlynkGSM_Manager v1.2.0 +ESP_DoubleResetDetector v1.1.1 +Set GSM module baud rate +Use WiFi to connect Blynk +LittleFS Flag read = 0xD0D04321 +No doubleResetDetected +Saving config file... +Saving config file OK +[3352] Hostname=TTGO-TCALL-GSM +[3390] LoadCfgFile +[3396] OK +[3396] CCSum=0x5558,RCSum=0x5558 +[3414] LoadCredFile +[3420] OK +[3420] CrCCsum=0x1cda,CrRCsum=0x1cda +[3420] Hdr=ESP32_GSM_WFM,BrdName=TTGO_TCALL +[3420] SSID=HueNet1,PW=12345678 +[3421] SSID1=HueNet2,PW1=12345678 +[3423] APN=rogers-core-appl1.apn,User=wapuser1 +[3427] PW=wap,PIN=12345678 +[3430] Server=account.duckdns.org,WiFi_Token=wifi_token,GSM_Token=gsm_token +[3440] Server1=192.168.2.32,WiFi_Token1=wifi_token,GSM_Token1=gsm_token +[3450] Port=8080 +[3452] ======= End Config Data ======= +[3466] LoadCPFile +[3470] OK +[3470] bg:Stay forever in CP:Forced-Persistent +[4315] +stConf:SSID=TestPortal-ESP32,PW=TestPortalPass +[4315] IP=192.168.4.1,ch=10 +gprs apn = rogers-core-appl1.apn +DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal +FF +Your stored Credentials : +MQTT Server = mqtt.ddns.net +Port = 1883 +MQTT UserName = mqtt-new_user +MQTT PWD = mqtt-password +Subs Topics = SubTopic_ESP32_GSM +Pubs Topics = PubTopic_ESP32_GSM +Stop doubleResetDetecting +Saving config file... +Saving config file OK +``` + --- --- @@ -1556,6 +1806,14 @@ Sometimes, the library will only work if you update the board core to the latest ## Releases +### Major Releases v1.2.0 + +1. To permit autoreset after configurable timeout if DRD/MRD or non-persistent forced-CP. Check [**Good new feature: Blynk.resetAndEnterConfigPortal() Thanks & question #27**](https://github.com/khoih-prog/Blynk_WM/issues/27) +2. Fix rare Config Portal bug not updating Config and dynamic Params data successfully in very noisy or weak WiFi situation +3. 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) +4. Add the new Virtual ConfigPortal SW feature to examples. +5. Disable the GSM/GPRS modem initialization which blocks the operation of Config Portal when using Config Portal. + ### Major Releases v1.1.0 1. Add support to LittleFS for ESP32 using [LittleFS_esp32](https://github.com/lorol/LITTLEFS) Library @@ -1659,6 +1917,7 @@ Submit issues to: [BlynkGSM_Manager issues](https://github.com/khoih-prog/BlynkG 17. Clean-up all compiler warnings possible. 18. Add Table of Contents 19. Add Version String +20. Add functions to control Config Portal from software or Virtual Switches. --- --- diff --git a/examples/ESP32_GSM/Credentials.h b/examples/ESP32_GSM/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/ESP32_GSM/Credentials.h +++ b/examples/ESP32_GSM/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP32_GSM/ESP32_GSM.ino b/examples/ESP32_GSM/ESP32_GSM.ino index 80df933..f4ddd58 100644 --- a/examples/ESP32_GSM/ESP32_GSM.ino +++ b/examples/ESP32_GSM/ESP32_GSM.ino @@ -6,8 +6,8 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - + Version: 1.2.0 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. @@ -24,6 +24,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart ESP32_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -161,9 +190,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -189,13 +218,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP32_GSM/defines.h b/examples/ESP32_GSM/defines.h index 1f2d134..1643198 100644 --- a/examples/ESP32_GSM/defines.h +++ b/examples/ESP32_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -66,6 +48,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -115,6 +100,7 @@ #include #if USE_BLYNK_WM + #define USE_DYNAMIC_PARAMETERS true #include diff --git a/examples/ESP32_GSM/dynamicParams.h b/examples/ESP32_GSM/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/ESP32_GSM/dynamicParams.h +++ b/examples/ESP32_GSM/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/ESP32_GSM_SHT3x/Credentials.h b/examples/ESP32_GSM_SHT3x/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/ESP32_GSM_SHT3x/Credentials.h +++ b/examples/ESP32_GSM_SHT3x/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP32_GSM_SHT3x/ESP32_GSM_SHT3x.ino b/examples/ESP32_GSM_SHT3x/ESP32_GSM_SHT3x.ino index fcaf619..a58084e 100644 --- a/examples/ESP32_GSM_SHT3x/ESP32_GSM_SHT3x.ino +++ b/examples/ESP32_GSM_SHT3x/ESP32_GSM_SHT3x.ino @@ -7,7 +7,7 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -25,17 +25,46 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" +#include //https://github.com/Risele/SHT3x +SHT3x Sensor; + #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" -#endif -#include //https://github.com/Risele/SHT3x -SHT3x Sensor; + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} +#endif void SensorUpdate() { @@ -80,7 +109,7 @@ void SensorUpdate() } } -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -152,7 +181,7 @@ void setup() SerialMon.print(F("\nStart ESP32_GSM_SHT3x (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -220,9 +249,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -248,13 +277,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP32_GSM_SHT3x/defines.h b/examples/ESP32_GSM_SHT3x/defines.h index 50f4ca4..ae8fbf9 100644 --- a/examples/ESP32_GSM_SHT3x/defines.h +++ b/examples/ESP32_GSM_SHT3x/defines.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -67,6 +49,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -116,6 +101,7 @@ #include #if USE_BLYNK_WM + #define USE_DYNAMIC_PARAMETERS true #include diff --git a/examples/ESP32_GSM_SHT3x/dynamicParams.h b/examples/ESP32_GSM_SHT3x/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/ESP32_GSM_SHT3x/dynamicParams.h +++ b/examples/ESP32_GSM_SHT3x/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/ESP32_MRD_GSM/Credentials.h b/examples/ESP32_MRD_GSM/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/ESP32_MRD_GSM/Credentials.h +++ b/examples/ESP32_MRD_GSM/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP32_MRD_GSM/ESP32_MRD_GSM.ino b/examples/ESP32_MRD_GSM/ESP32_MRD_GSM.ino index 2981a35..8576ab2 100644 --- a/examples/ESP32_MRD_GSM/ESP32_MRD_GSM.ino +++ b/examples/ESP32_MRD_GSM/ESP32_MRD_GSM.ino @@ -6,8 +6,8 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - + Version: 1.2.0 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. @@ -24,6 +24,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart ESP32_MRD_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -165,9 +194,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -193,13 +222,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP32_MRD_GSM/defines.h b/examples/ESP32_MRD_GSM/defines.h index 14b3395..e801e4e 100644 --- a/examples/ESP32_MRD_GSM/defines.h +++ b/examples/ESP32_MRD_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -98,6 +80,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -147,7 +132,8 @@ #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else diff --git a/examples/ESP32_MRD_GSM/dynamicParams.h b/examples/ESP32_MRD_GSM/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/ESP32_MRD_GSM/dynamicParams.h +++ b/examples/ESP32_MRD_GSM/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/ESP8266_GSM/Credentials.h b/examples/ESP8266_GSM/Credentials.h index 139810e..0128fdd 100644 --- a/examples/ESP8266_GSM/Credentials.h +++ b/examples/ESP8266_GSM/Credentials.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP8266_GSM/ESP8266_GSM.ino b/examples/ESP8266_GSM/ESP8266_GSM.ino index 85f9087..c4fd7e7 100644 --- a/examples/ESP8266_GSM/ESP8266_GSM.ino +++ b/examples/ESP8266_GSM/ESP8266_GSM.ino @@ -6,7 +6,7 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -23,7 +23,9 @@ Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart ESP8266_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); // Set-up modem reset, enable, power pins @@ -157,9 +186,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP8266_config.apn); - if (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -185,13 +214,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP8266_GSM/defines.h b/examples/ESP8266_GSM/defines.h index 876a95d..633b1e3 100644 --- a/examples/ESP8266_GSM/defines.h +++ b/examples/ESP8266_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -72,6 +54,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include @@ -121,6 +106,7 @@ #include #if USE_BLYNK_WM + #define USE_DYNAMIC_PARAMETERS true #include diff --git a/examples/ESP8266_GSM/dynamicParams.h b/examples/ESP8266_GSM/dynamicParams.h index 40944c9..a2a8e38 100644 --- a/examples/ESP8266_GSM/dynamicParams.h +++ b/examples/ESP8266_GSM/dynamicParams.h @@ -6,30 +6,16 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h #define dynamicParams_h -#define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/ESP8266_GSM_SHT3x/Credentials.h b/examples/ESP8266_GSM_SHT3x/Credentials.h index 17b4673..79c3c46 100644 --- a/examples/ESP8266_GSM_SHT3x/Credentials.h +++ b/examples/ESP8266_GSM_SHT3x/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP8266_GSM_SHT3x/ESP8266_GSM_SHT3x.ino b/examples/ESP8266_GSM_SHT3x/ESP8266_GSM_SHT3x.ino index a20ba86..add36eb 100644 --- a/examples/ESP8266_GSM_SHT3x/ESP8266_GSM_SHT3x.ino +++ b/examples/ESP8266_GSM_SHT3x/ESP8266_GSM_SHT3x.ino @@ -7,7 +7,7 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -25,17 +25,46 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" +#include //https://github.com/Risele/SHT3x +SHT3x Sensor; + #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" -#endif + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 -#include //https://github.com/Risele/SHT3x -SHT3x Sensor; +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} +#endif void SensorUpdate() { @@ -80,7 +109,7 @@ void SensorUpdate() } } -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -152,7 +181,7 @@ void setup() SerialMon.print(F("\nStart ESP8266_GSM_SHT3x (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -220,9 +249,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP8266_config.apn); - if (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -248,13 +277,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP8266_GSM_SHT3x/defines.h b/examples/ESP8266_GSM_SHT3x/defines.h index d1134a0..b7b3e6a 100644 --- a/examples/ESP8266_GSM_SHT3x/defines.h +++ b/examples/ESP8266_GSM_SHT3x/defines.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -74,6 +56,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include @@ -123,7 +108,8 @@ #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else diff --git a/examples/ESP8266_GSM_SHT3x/dynamicParams.h b/examples/ESP8266_GSM_SHT3x/dynamicParams.h index 92a6b79..c164325 100644 --- a/examples/ESP8266_GSM_SHT3x/dynamicParams.h +++ b/examples/ESP8266_GSM_SHT3x/dynamicParams.h @@ -7,30 +7,16 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h #define dynamicParams_h -#define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/ESP8266_MRD_GSM/Credentials.h b/examples/ESP8266_MRD_GSM/Credentials.h index 139810e..0128fdd 100644 --- a/examples/ESP8266_MRD_GSM/Credentials.h +++ b/examples/ESP8266_MRD_GSM/Credentials.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/ESP8266_MRD_GSM/ESP8266_MRD_GSM.ino b/examples/ESP8266_MRD_GSM/ESP8266_MRD_GSM.ino index 2cb05f5..4122cad 100644 --- a/examples/ESP8266_MRD_GSM/ESP8266_MRD_GSM.ino +++ b/examples/ESP8266_MRD_GSM/ESP8266_MRD_GSM.ino @@ -6,7 +6,7 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -23,7 +23,9 @@ Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart ESP8266_MRD_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -165,9 +194,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP8266_config.apn); - if (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP8266_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -193,13 +222,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/ESP8266_MRD_GSM/defines.h b/examples/ESP8266_MRD_GSM/defines.h index 686e97d..49b598e 100644 --- a/examples/ESP8266_MRD_GSM/defines.h +++ b/examples/ESP8266_MRD_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -104,6 +86,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include @@ -153,6 +138,7 @@ #include #if USE_BLYNK_WM + #define USE_DYNAMIC_PARAMETERS true #include diff --git a/examples/ESP8266_MRD_GSM/dynamicParams.h b/examples/ESP8266_MRD_GSM/dynamicParams.h index 40944c9..a2a8e38 100644 --- a/examples/ESP8266_MRD_GSM/dynamicParams.h +++ b/examples/ESP8266_MRD_GSM/dynamicParams.h @@ -6,30 +6,16 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h #define dynamicParams_h -#define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/TTGO_TCALL_GSM/Credentials.h b/examples/TTGO_TCALL_GSM/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/TTGO_TCALL_GSM/Credentials.h +++ b/examples/TTGO_TCALL_GSM/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/TTGO_TCALL_GSM/TTGO_TCALL_GSM.ino b/examples/TTGO_TCALL_GSM/TTGO_TCALL_GSM.ino index c3c966a..1d5cfbb 100644 --- a/examples/TTGO_TCALL_GSM/TTGO_TCALL_GSM.ino +++ b/examples/TTGO_TCALL_GSM/TTGO_TCALL_GSM.ino @@ -6,8 +6,8 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - + Version: 1.2.0 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. @@ -24,6 +24,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart TTGO_TCALL_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -125,7 +154,7 @@ void setup() Blynk_WF.setConfigPortal("TestPortal-ESP32", "TestPortalPass"); // Use configurable AP IP, instead of default IP 192.168.4.1 - Blynk_WF.setConfigPortalIP(IPAddress(192, 168, 232, 1)); + //Blynk_WF.setConfigPortalIP(IPAddress(192, 168, 232, 1)); // Set config portal channel, default = 1. Use 0 => random channel from 1-12 to avoid conflict Blynk_WF.setConfigPortalChannel(0); @@ -159,9 +188,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -187,13 +216,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/TTGO_TCALL_GSM/defines.h b/examples/TTGO_TCALL_GSM/defines.h index d9ff12a..019f72f 100644 --- a/examples/TTGO_TCALL_GSM/defines.h +++ b/examples/TTGO_TCALL_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -66,6 +48,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -115,7 +100,8 @@ #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else diff --git a/examples/TTGO_TCALL_GSM/dynamicParams.h b/examples/TTGO_TCALL_GSM/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/TTGO_TCALL_GSM/dynamicParams.h +++ b/examples/TTGO_TCALL_GSM/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/TTGO_TCALL_MRD_GSM/Credentials.h b/examples/TTGO_TCALL_MRD_GSM/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/TTGO_TCALL_MRD_GSM/Credentials.h +++ b/examples/TTGO_TCALL_MRD_GSM/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/TTGO_TCALL_MRD_GSM/TTGO_TCALL_MRD_GSM.ino b/examples/TTGO_TCALL_MRD_GSM/TTGO_TCALL_MRD_GSM.ino index 6fb9674..ea4cc9f 100644 --- a/examples/TTGO_TCALL_MRD_GSM/TTGO_TCALL_MRD_GSM.ino +++ b/examples/TTGO_TCALL_MRD_GSM/TTGO_TCALL_MRD_GSM.ino @@ -6,8 +6,8 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - + Version: 1.2.0 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. @@ -24,6 +24,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" @@ -31,9 +33,36 @@ #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 + +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} #endif -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -95,7 +124,7 @@ void setup() SerialMon.print(F("\nStart TTGO_TCALL_MRD_GSM (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -163,9 +192,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -191,13 +220,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/TTGO_TCALL_MRD_GSM/defines.h b/examples/TTGO_TCALL_MRD_GSM/defines.h index 0ae5ff0..37c384a 100644 --- a/examples/TTGO_TCALL_MRD_GSM/defines.h +++ b/examples/TTGO_TCALL_MRD_GSM/defines.h @@ -6,24 +6,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -98,6 +80,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -147,7 +132,8 @@ #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else diff --git a/examples/TTGO_TCALL_MRD_GSM/dynamicParams.h b/examples/TTGO_TCALL_MRD_GSM/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/TTGO_TCALL_MRD_GSM/dynamicParams.h +++ b/examples/TTGO_TCALL_MRD_GSM/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/examples/TTGO_TCALL_SHT3x/Credentials.h b/examples/TTGO_TCALL_SHT3x/Credentials.h index 5defb6b..ae526f1 100644 --- a/examples/TTGO_TCALL_SHT3x/Credentials.h +++ b/examples/TTGO_TCALL_SHT3x/Credentials.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef Credentials_h diff --git a/examples/TTGO_TCALL_SHT3x/TTGO_TCALL_SHT3x.ino b/examples/TTGO_TCALL_SHT3x/TTGO_TCALL_SHT3x.ino index abbad2e..9048064 100644 --- a/examples/TTGO_TCALL_SHT3x/TTGO_TCALL_SHT3x.ino +++ b/examples/TTGO_TCALL_SHT3x/TTGO_TCALL_SHT3x.ino @@ -7,7 +7,7 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -25,17 +25,46 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #include "defines.h" +#include //https://github.com/Risele/SHT3x +SHT3x Sensor; + #if USE_BLYNK_WM #include "Credentials.h" #include "dynamicParams.h" -#endif + + #define BLYNK_PIN_FORCED_CONFIG V10 + #define BLYNK_PIN_FORCED_PERS_CONFIG V20 -#include //https://github.com/Risele/SHT3x -SHT3x Sensor; +// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nCP Button Hit. Rebooting") ); + + // This will keep CP once, clear after reset, even you didn't enter CP at all. + Blynk.resetAndEnterConfigPortal(); + } +} + +// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal +BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG) +{ + if (param.asInt()) + { + Serial.println( F("\nPersistent CP Button Hit. Rebooting") ); + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + Blynk.resetAndEnterConfigPortalPersistent(); + } +} +#endif void SensorUpdate() { @@ -80,7 +109,7 @@ void SensorUpdate() } } -void heartBeatPrint(void) +void heartBeatPrint() { static int num = 1; @@ -152,7 +181,7 @@ void setup() SerialMon.print(F("\nStart TTGO_TCALL_GSM_SHT3x (Simultaneous WiFi+GSM) using ")); SerialMon.print(CurrentFileFS); - SerialMon.println(" on " + String(ARDUINO_BOARD)); + SerialMon.print(F(" on ")); SerialMon.println(ARDUINO_BOARD); SerialMon.println(BLYNK_GSM_MANAGER_VERSION); #if USE_BLYNK_WM @@ -219,9 +248,9 @@ void setup() Serial.print(F("gprs apn = ")); Serial.println(localBlynkGSM_ESP32_config.apn); - if (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) + if ( Blynk.inConfigPortal() || (String(localBlynkGSM_ESP32_config.apn) == NO_CONFIG) ) { - Serial.println(F("No valid stored apn. Must run WiFi to Open config portal")); + Serial.println(F("DRD/MRD, Forced Config Portal or No valid stored apn. Must run only WiFi to Open config portal")); valid_apn = false; } else @@ -247,13 +276,15 @@ void setup() } #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS) -void displayCredentials(void) +void displayCredentials() { - Serial.println("\nYour stored Credentials :"); + Serial.println(F("\nYour stored Credentials :")); for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { - Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata); + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); } } #endif diff --git a/examples/TTGO_TCALL_SHT3x/defines.h b/examples/TTGO_TCALL_SHT3x/defines.h index fff7d3f..453e25a 100644 --- a/examples/TTGO_TCALL_SHT3x/defines.h +++ b/examples/TTGO_TCALL_SHT3x/defines.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef defines_h @@ -67,6 +49,9 @@ #define TIMEOUT_RECONNECT_WIFI 10000L #define RESET_IF_CONFIG_TIMEOUT true #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s) +#define CONFIG_TIMEOUT 120000L // Those above #define's must be placed before #include // TTGO T-Call pin definitions @@ -116,7 +101,8 @@ #include #if USE_BLYNK_WM - + #define USE_DYNAMIC_PARAMETERS true + #include #else diff --git a/examples/TTGO_TCALL_SHT3x/dynamicParams.h b/examples/TTGO_TCALL_SHT3x/dynamicParams.h index 67276d9..dd97010 100644 --- a/examples/TTGO_TCALL_SHT3x/dynamicParams.h +++ b/examples/TTGO_TCALL_SHT3x/dynamicParams.h @@ -7,24 +7,6 @@ Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_Manager Licensed under MIT license - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K Hoang 17/01/2020 Initial coding. Add config portal similar to Blynk_WM library. - 1.0.1 K Hoang 27/01/2020 Change Synch XMLHttpRequest to Async (https://xhr.spec.whatwg.org/). Reduce code size - 1.0.2 K Hoang 08/02/2020 Enable GSM/GPRS and WiFi running simultaneously - 1.0.3 K Hoang 18/02/2020 Add checksum. Add clearConfigData() - 1.0.4 K Hoang 14/03/2020 Enhance Config Portal GUI. Reduce code size. - 1.0.5 K Hoang 20/03/2020 Add more modem supports. See the list in README.md - 1.0.6 K Hoang 07/04/2020 Enable adding dynamic custom parameters from sketch - 1.0.7 K Hoang 09/04/2020 SSID password maxlen is 63 now. Permit special chars # and % in input data. - 1.0.8 K Hoang 14/04/2020 Fix bug. - 1.0.9 K Hoang 31/05/2020 Update to use LittleFS for ESP8266 core 2.7.1+. Add Configurable Config Portal Title, - Default Config Data and DRD. Add MultiWiFi/Blynk features for WiFi and GPRS/GSM - 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. - Fix bug and logic of USE_DEFAULT_CONFIG_DATA. - 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD *****************************************************************************************************************************/ #ifndef dynamicParams_h @@ -32,7 +14,11 @@ #if USE_BLYNK_WM - #define USE_DYNAMIC_PARAMETERS true +#if (USE_DYNAMIC_PARAMETERS) + #warning USE_DYNAMIC_PARAMETERS +#endif + +// USE_DYNAMIC_PARAMETERS defined in defined.h /////////////// Start dynamic Credentials /////////////// diff --git a/keywords.txt b/keywords.txt index 5addc9a..5581b8c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -50,7 +50,10 @@ getGPRSPIN KEYWORD2 getHWPort KEYWORD2 setHostname KEYWORD2 getFullConfigData KEYWORD2 +inConfigPortal KEYWORD2 clearConfigData KEYWORD2 +resetAndEnterConfigPortal KEYWORD2 +resetAndEnterConfigPortalPersistent KEYWORD2 ####################################### # Handler helpers diff --git a/library.json b/library.json index c6ef8fd..1f1f91d 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "BlynkGSM_Manager", - "version": "1.1.0", + "version": "1.2.0", "description": "Library for enabling GSM/GPRS and WiFi running simultaneously as well as configuring/auto(re)connecting at runtime GSM shields to Internet and Blynk and ESP8266/ESP32 WiFi modules to best or available MultiWiFi APs and MultiBlynk servers. 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", "authors": @@ -47,6 +47,12 @@ "name": "TinyGSM", "version": "^0.10.9", "platforms": ["espressif8266", "espressif32"] + }, + { + "owner": "lorol", + "name": "LittleFS_esp32", + "version": ">=1.0.5", + "platforms": ["espressif32"] } ], "frameworks": "*", diff --git a/library.properties b/library.properties index 8eb604a..1f0265b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=BlynkGSM_Manager -version=1.1.0 +version=1.2.0 author=Khoi Hoang license=MIT maintainer=Khoi Hoang @@ -8,5 +8,5 @@ paragraph=Library for enabling GSM/GPRS and WiFi running simultaneously as well category=Communication url=https://github.com/khoih-prog/BlynkGSM_Manager architectures=esp32,esp8266 -depends=Blynk,TinyGSM,ESP_DoubleResetDetector,ESP_MultiResetDetector +depends=Blynk,TinyGSM,ESP_DoubleResetDetector,ESP_MultiResetDetector,LittleFS_esp32 includes=BlynkSimpleESP32_GSM_WFM.h,BlynkSimpleESP8266_GSM_WFM.h,BlynkSimpleESP32_GSM_WF.h,BlynkSimpleESP8266_GSM_WF.h,BlynkSimpleTinyGSM_M.h diff --git a/platformio/platformio.ini b/platformio/platformio.ini index 9318ed2..a4eb848 100644 --- a/platformio/platformio.ini +++ b/platformio/platformio.ini @@ -30,12 +30,12 @@ upload_speed = 921600 lib_deps = ; PlatformIO 4.x Blynk@~0.6.1 - TinyGSM@~0.10.8 + TinyGSM@~0.10.9 ESP_DoubleResetDetector@~1.1.1 ESP_MultiResetDetector@~1.1.1 ; PlatformIO 5.x ; blynkkk/Blynk@~0.6.1 -; vshymanskyy/TinyGSM@~0.10.8 +; vshymanskyy/TinyGSM@~0.10.9 ; khoih.prog/ESP_DoubleResetDetector@~1.1.1 ; khoih.prog/ESP_MultiResetDetector@~1.1.1 diff --git a/src/Adapters/BlynkGsm_ESP32M.h b/src/Adapters/BlynkGsm_ESP32M.h index 0ea4130..fe010f5 100644 --- a/src/Adapters/BlynkGsm_ESP32M.h +++ b/src/Adapters/BlynkGsm_ESP32M.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once diff --git a/src/Adapters/BlynkGsm_ESP8266M.h b/src/Adapters/BlynkGsm_ESP8266M.h index 2562c9a..a3317a6 100644 --- a/src/Adapters/BlynkGsm_ESP8266M.h +++ b/src/Adapters/BlynkGsm_ESP8266M.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once diff --git a/src/BlynkSimpleEsp32_GSM_WF.h b/src/BlynkSimpleEsp32_GSM_WF.h index 8214854..05bc427 100644 --- a/src/BlynkSimpleEsp32_GSM_WF.h +++ b/src/BlynkSimpleEsp32_GSM_WF.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once @@ -44,7 +46,7 @@ #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif -#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.1.0" +#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.2.0" #define BLYNK_SEND_ATOMIC diff --git a/src/BlynkSimpleEsp32_GSM_WFM.h b/src/BlynkSimpleEsp32_GSM_WFM.h index 3515065..f2b70c5 100644 --- a/src/BlynkSimpleEsp32_GSM_WFM.h +++ b/src/BlynkSimpleEsp32_GSM_WFM.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once @@ -44,7 +46,7 @@ #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif -#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.1.0" +#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.2.0" #define BLYNK_SEND_ATOMIC @@ -210,8 +212,11 @@ typedef struct } MenuItem; // -extern uint16_t NUM_MENU_ITEMS; -extern MenuItem myMenuItems []; +#if USE_DYNAMIC_PARAMETERS + extern uint16_t NUM_MENU_ITEMS; + extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; +#endif #define SSID_MAX_LEN 32 // WPA2 passwords can be up to 63 characters long. @@ -223,9 +228,13 @@ typedef struct char wifi_pw [PASS_MAX_LEN]; } WiFi_Credentials; +#define HEADER_MAX_LEN 16 + #define BLYNK_SERVER_MAX_LEN 32 #define BLYNK_TOKEN_MAX_LEN 36 +#define BOARD_NAME_MAX_LEN 24 + typedef struct { char blynk_server [BLYNK_SERVER_MAX_LEN]; @@ -240,21 +249,27 @@ typedef struct #define NUM_CONFIGURABLE_ITEMS ( 6 + (2 * NUM_WIFI_CREDENTIALS) + (3 * NUM_BLYNK_CREDENTIALS) ) #define DEFAULT_GPRS_PIN "1234" +#define APN_MAX_LEN 32 +#define GPRS_USER_MAX_LEN 32 +#define GPRS_PASS_MAX_LEN 32 +#define GPRS_PIN_MAX_LEN 12 + typedef struct Configuration { - char header [16]; + char header [HEADER_MAX_LEN]; WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; Blynk_Credentials Blynk_Creds [NUM_BLYNK_CREDENTIALS]; int blynk_port; // YOUR GSM / GPRS RELATED - char apn [32]; - char gprsUser [32]; - char gprsPass [32]; - char gprsPin [12]; // A PIN (Personal Identification Number) is a 4-8 digit passcode + char apn [APN_MAX_LEN]; + char gprsUser [GPRS_USER_MAX_LEN]; + char gprsPass [GPRS_PASS_MAX_LEN]; + char gprsPin [GPRS_PIN_MAX_LEN]; // A PIN (Personal Identification Number) is a 4-8 digit passcode // END OF YOUR GSM / GPRS RELATED - char board_name [24]; + char board_name [BOARD_NAME_MAX_LEN]; int checkSum; } Blynk_WF_Configuration; + // Currently CONFIG_DATA_SIZE = ( 156 + (96 * NUM_WIFI_CREDENTIALS) + (104 * NUM_BLYNK_CREDENTIALS) ) = 556 uint16_t CONFIG_DATA_SIZE = sizeof(Blynk_WF_Configuration); @@ -407,7 +422,7 @@ class BlynkWifi { #if ( BLYNK_WM_DEBUG > 1) BLYNK_LOG1(BLYNK_F("Multi or Double Reset Detected")); -#endif +#endif noConfigPortal = false; } //// New DRD/MRD //// @@ -437,11 +452,15 @@ class BlynkWifi } BLYNK_LOG2(BLYNK_F("Hostname="), RFC952_hostname); - + + hadConfigData = getConfigData(); + + isForcedConfigPortal = isForcedCP(); + //// New DRD/MRD //// // noConfigPortal when getConfigData() OK and no MRD/DRD'ed - if (getConfigData() && noConfigPortal) - //// New DRD/MRD //// + //if (getConfigData() && noConfigPortal) + if (hadConfigData && noConfigPortal && (!isForcedConfigPortal) ) { hadConfigData = true; @@ -449,7 +468,7 @@ class BlynkWifi BLYNK_LOG1(noConfigPortal? BLYNK_F("bg: noConfigPortal = true") : BLYNK_F("bg: noConfigPortal = false")); #endif - for (int i = 0; i < NUM_WIFI_CREDENTIALS; i++) + for (uint16_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) { wifiMulti.addAP(BlynkGSM_ESP32_config.WiFi_Creds[i].wifi_ssid, BlynkGSM_ESP32_config.WiFi_Creds[i].wifi_pw); } @@ -486,11 +505,25 @@ class BlynkWifi } else { - BLYNK_LOG2(BLYNK_F("bg: Stay forever in config portal."), - noConfigPortal ? BLYNK_F("No configDat") : BLYNK_F("DRD/MRD detected")); +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(isForcedConfigPortal? BLYNK_F("bg: isForcedConfigPortal = true") : BLYNK_F("bg: isForcedConfigPortal = false")); +#endif + + // If not persistent => clear the flag so that after reset. no more CP, even CP not entered and saved + if (persForcedConfigPortal) + { + BLYNK_LOG2(BLYNK_F("bg:Stay forever in CP:"), isForcedConfigPortal ? BLYNK_F("Forced-Persistent") : (noConfigPortal ? BLYNK_F("No ConfigDat") : BLYNK_F("DRD/MRD"))); + } + else + { + BLYNK_LOG2(BLYNK_F("bg:Stay forever in CP:"), isForcedConfigPortal ? BLYNK_F("Forced-non-Persistent") : (noConfigPortal ? BLYNK_F("No ConfigDat") : BLYNK_F("DRD/MRD"))); + clearForcedCP(); + } + + hadConfigData = isForcedConfigPortal ? true : (noConfigPortal ? false : true); + // failed to connect to Blynk server, will start configuration mode - hadConfigData = false; startConfigurationMode(); } } @@ -561,7 +594,9 @@ class BlynkWifi retryTimes = 0; if (server) + { server->handleClient(); + } return; } @@ -626,10 +661,15 @@ class BlynkWifi digitalWrite(LED_BUILTIN, LED_OFF); } - Base::run(); + if (connected()) + { + Base::run(); + } } - void setHostname(void) + ////////////////////////////////////////////// + + void setHostname() { if (RFC952_hostname[0] != 0) { @@ -651,7 +691,7 @@ class BlynkWifi } #define MIN_WIFI_CHANNEL 1 -#define MAX_WIFI_CHANNEL 12 +#define MAX_WIFI_CHANNEL 11 int setConfigPortalChannel(int channel = 1) { @@ -807,6 +847,45 @@ class BlynkWifi memset(&BlynkGSM_ESP32_config, 0, sizeof(BlynkGSM_ESP32_config)); saveConfigData(); } + + bool inConfigPortal() + { + return configuration_mode; + } + + // Forced CP => Flag = 0xBEEFBEEF. Else => No forced CP + // Flag to be stored at (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // to avoid corruption to current data + //#define FORCED_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xDEADBEEF) + //#define FORCED_PERS_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xBEEFDEAD) + + const uint32_t FORCED_CONFIG_PORTAL_FLAG_DATA = 0xDEADBEEF; + const uint32_t FORCED_PERS_CONFIG_PORTAL_FLAG_DATA = 0xBEEFDEAD; + + #define FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE 4 + + void resetAndEnterConfigPortal() + { + persForcedConfigPortal = false; + + setForcedCP(false); + + // Delay then reset the ESP32 after save data + delay(1000); + ESP.restart(); + } + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + void resetAndEnterConfigPortalPersistent() + { + persForcedConfigPortal = true; + + setForcedCP(true); + + // Delay then reset the ESP32 after save data + delay(1000); + ESP.restart(); + } private: WebServer *server; @@ -816,6 +895,9 @@ class BlynkWifi unsigned long configTimeout; bool hadConfigData = false; + + bool isForcedConfigPortal = false; + bool persForcedConfigPortal = false; // default to channel 1 int WiFiAPChannel = 1; @@ -885,13 +967,17 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("======= End Config Data =======")); } - void displayWiFiData(void) + ////////////////////////////////////////////// + + void displayWiFiData() { BLYNK_LOG6(BLYNK_F("SSID="), WiFi.SSID(), BLYNK_F(",RSSI="), WiFi.RSSI(), BLYNK_F(",Channel="), WiFi.channel()); BLYNK_LOG6(BLYNK_F("IP="), WiFi.localIP(), BLYNK_F(",GW="), WiFi.gatewayIP(), BLYNK_F(",SN="), WiFi.subnetMask()); BLYNK_LOG4(BLYNK_F("DNS1="), WiFi.dnsIP(0), BLYNK_F(",DNS2="), WiFi.dnsIP(1)); } + + ////////////////////////////////////////////// int calcChecksum() { @@ -903,16 +989,177 @@ class BlynkWifi return checkSum; } + + ////////////////////////////////////////////// + + void NULLTerminateConfig() + { + //#define SSID_MAX_LEN 32 + //#define PASS_MAX_LEN 64 + + //#define HEADER_MAX_LEN 16 + //#define BLYNK_SERVER_MAX_LEN 32 + //#define BLYNK_TOKEN_MAX_LEN 36 + //#define BOARD_NAME_MAX_LEN 24 + + //#define APN_MAX_LEN 32 + //#define GPRS_USER_MAX_LEN 32 + //#define GPRS_PASS_MAX_LEN 32 + //#define GPRS_PIN_MAX_LEN 12 + + // NULL Terminating to be sure + BlynkGSM_ESP32_config.header[HEADER_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_ssid[SSID_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_pw[PASS_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_ssid[SSID_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_pw[PASS_MAX_LEN - 1] = 0; + + BlynkGSM_ESP32_config.apn[APN_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.gprsUser[GPRS_USER_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.gprsPass[GPRS_PASS_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.gprsPin[GPRS_PIN_MAX_LEN - 1] = 0; + + BlynkGSM_ESP32_config.Blynk_Creds[0].blynk_server[BLYNK_SERVER_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.Blynk_Creds[0].wifi_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.Blynk_Creds[0].gsm_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.Blynk_Creds[1].blynk_server[BLYNK_SERVER_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.Blynk_Creds[1].wifi_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.Blynk_Creds[1].gsm_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP32_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; + BlynkGSM_ESP32_config.board_name [BOARD_NAME_MAX_LEN - 1] = 0; + } + +////////////////////////////////////////////// #if ( USE_LITTLEFS || USE_SPIFFS ) -#define CONFIG_FILENAME BLYNK_F("/gsm_config.dat") -#define CONFIG_FILENAME_BACKUP BLYNK_F("/gsm_config.bak") +#define CONFIG_FILENAME BLYNK_F("/gsm_config.dat") +#define CONFIG_FILENAME_BACKUP BLYNK_F("/gsm_config.bak") + +#define CREDENTIALS_FILENAME BLYNK_F("/gsm_cred.dat") +#define CREDENTIALS_FILENAME_BACKUP BLYNK_F("/gsm_cred.bak") + +#define CONFIG_PORTAL_FILENAME BLYNK_F("/gsm_cp.dat") +#define CONFIG_PORTAL_FILENAME_BACKUP BLYNK_F("/gsm_cp.bak") + + ////////////////////////////////////////////// + + void saveForcedCP(uint32_t value) + { + File file = FileFS.open(CONFIG_PORTAL_FILENAME, "w"); + + BLYNK_LOG1(BLYNK_F("SaveCPFile ")); + + if (file) + { + file.write((uint8_t*) &value, sizeof(value)); + file.close(); + BLYNK_LOG1(BLYNK_F("OK")); + } + else + { + BLYNK_LOG1(BLYNK_F("failed")); + } + + // Trying open redundant CP file + file = FileFS.open(CONFIG_PORTAL_FILENAME_BACKUP, "w"); + + BLYNK_LOG1(BLYNK_F("SaveBkUpCPFile ")); + + if (file) + { + file.write((uint8_t *) &value, sizeof(value)); + file.close(); + BLYNK_LOG1(BLYNK_F("OK")); + } + else + { + BLYNK_LOG1(BLYNK_F("failed")); + } + } + + ////////////////////////////////////////////// + + void setForcedCP(bool isPersistent) + { + uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(isPersistent ? BLYNK_F("setForcedCP Persistent") : BLYNK_F("setForcedCP non-Persistent")); +#endif + + saveForcedCP(readForcedConfigPortalFlag); + } + + ////////////////////////////////////////////// + + void clearForcedCP() + { + uint32_t readForcedConfigPortalFlag = 0; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("clearForcedCP")); +#endif + + saveForcedCP(readForcedConfigPortalFlag); + } + + ////////////////////////////////////////////// + + bool isForcedCP() + { + uint32_t readForcedConfigPortalFlag; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("Check if isForcedCP")); +#endif + + File file = FileFS.open(CONFIG_PORTAL_FILENAME, "r"); + BLYNK_LOG1(BLYNK_F("LoadCPFile ")); + + if (!file) + { + BLYNK_LOG1(BLYNK_F("failed")); + + // Trying open redundant config file + file = FileFS.open(CONFIG_PORTAL_FILENAME_BACKUP, "r"); + BLYNK_LOG1(BLYNK_F("LoadBkUpCPFile ")); + + if (!file) + { + BLYNK_LOG1(BLYNK_F("failed")); + return false; + } + } -#define CREDENTIALS_FILENAME BLYNK_F("/gsm_cred.dat") -#define CREDENTIALS_FILENAME_BACKUP BLYNK_F("/gsm_cred.bak") + file.readBytes((char *) &readForcedConfigPortalFlag, sizeof(readForcedConfigPortalFlag)); - bool checkDynamicData(void) + BLYNK_LOG1(BLYNK_F("OK")); + file.close(); + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + if (readForcedConfigPortalFlag == FORCED_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = false; + return true; + } + else if (readForcedConfigPortalFlag == FORCED_PERS_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = true; + return true; + } + else + { + return false; + } + } + + ////////////////////////////////////////////// + +#if USE_DYNAMIC_PARAMETERS + + bool checkDynamicData() { int checkSum = 0; int readCheckSum; @@ -941,7 +1188,7 @@ class BlynkWifi // We dont like to destroy myMenuItems[i].pdata with invalid data uint16_t maxBufferLength = 0; - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { if (myMenuItems[i].maxlen > maxBufferLength) maxBufferLength = myMenuItems[i].maxlen; @@ -965,7 +1212,7 @@ class BlynkWifi #endif } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = readBuffer; @@ -995,10 +1242,7 @@ class BlynkWifi if (readBuffer != NULL) { free(readBuffer); - -#if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Buffer freed")); -#endif } if ( checkSum != readCheckSum) @@ -1008,8 +1252,10 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// - bool loadDynamicData(void) + bool loadDynamicData() { int checkSum = 0; int readCheckSum; @@ -1033,7 +1279,7 @@ class BlynkWifi } } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; totalDataSize += myMenuItems[i].maxlen; @@ -1067,15 +1313,17 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// - void saveDynamicData(void) + void saveDynamicData() { int checkSum = 0; File file = FileFS.open(CREDENTIALS_FILENAME, "w"); BLYNK_LOG1(BLYNK_F("SaveCredFile ")); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1115,7 +1363,7 @@ class BlynkWifi file = FileFS.open(CREDENTIALS_FILENAME_BACKUP, "w"); BLYNK_LOG1(BLYNK_F("SaveBkUpCredFile ")); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1149,8 +1397,11 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("failed")); } } +#endif + + ////////////////////////////////////////////// - void loadConfigData(void) + void loadConfigData() { File file = FileFS.open(CONFIG_FILENAME, "r"); BLYNK_LOG1(BLYNK_F("LoadCfgFile ")); @@ -1174,9 +1425,13 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("OK")); file.close(); + + NULLTerminateConfig(); } + + ////////////////////////////////////////////// - void saveConfigData(void) + void saveConfigData() { File file = FileFS.open(CONFIG_FILENAME, "w"); BLYNK_LOG1(BLYNK_F("SaveCfgFile ")); @@ -1211,24 +1466,31 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("failed")); } - saveDynamicData(); + //saveDynamicData(); } - void saveAllConfigData(void) + ////////////////////////////////////////////// + + void saveAllConfigData() { saveConfigData(); + +#if USE_DYNAMIC_PARAMETERS saveDynamicData(); +#endif } + + ////////////////////////////////////////////// - // Return false if init new EEPROM, or SPIFFS. No more need trying to connect. Go directly to config mode + // Return false if init new EEPROM or SPIFFS/LittleFS. No more need trying to connect. Go directly to config mode bool getConfigData() { - bool dynamicDataValid; + bool dynamicDataValid = true; int calChecksum; hadConfigData = false; - // Format SPIFFS/LittleFS if not yet + // Format SPIFFS if not yet if (!FileFS.begin(true)) { BLYNK_LOG1(BLYNK_F("SPIFFS/LittleFS failed! Formatting.")); @@ -1257,8 +1519,12 @@ class BlynkWifi // Don't need Config Portal anymore return true; } +#if USE_DYNAMIC_PARAMETERS else if ( ( FileFS.exists(CONFIG_FILENAME) || FileFS.exists(CONFIG_FILENAME_BACKUP) ) && ( FileFS.exists(CREDENTIALS_FILENAME) || FileFS.exists(CREDENTIALS_FILENAME_BACKUP) ) ) +#else + else if ( FileFS.exists(CONFIG_FILENAME) || FileFS.exists(CONFIG_FILENAME_BACKUP) ) +#endif { // if config file exists, load loadConfigData(); @@ -1272,21 +1538,23 @@ class BlynkWifi BLYNK_LOG4(BLYNK_F("CCSum=0x"), String(calChecksum, HEX), BLYNK_F(",RCSum=0x"), String(BlynkGSM_ESP32_config.checkSum, HEX)); - + +#if USE_DYNAMIC_PARAMETERS // Load dynamic data dynamicDataValid = loadDynamicData(); if (dynamicDataValid) { -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Valid Stored Dynamic Data")); -#endif + #endif } -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) else { BLYNK_LOG1(BLYNK_F("Invalid Stored Dynamic Data. Ignored")); } + #endif #endif } else @@ -1309,7 +1577,7 @@ class BlynkWifi } else { - memset(&BlynkGSM_ESP32_config, 0, sizeof(BlynkGSM_ESP32_config)); + memset(&BlynkGSM_ESP32_config, 0, sizeof(BlynkGSM_ESP32_config)); strcpy(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG); strcpy(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_pw, NO_CONFIG); @@ -1328,22 +1596,24 @@ class BlynkWifi BlynkGSM_ESP32_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; strcpy(BlynkGSM_ESP32_config.board_name, NO_CONFIG); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { // Actual size of pdata is [maxlen + 1] memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); } +#endif } strcpy(BlynkGSM_ESP32_config.header, BLYNK_BOARD_TYPE); -#if ( BLYNK_WM_DEBUG > 2) - for (int i = 0; i < NUM_MENU_ITEMS; i++) + #if (USE_DYNAMIC_PARAMETERS && ( BLYNK_WM_DEBUG > 2) ) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { BLYNK_LOG4(BLYNK_F("g:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); } -#endif + #endif // Don't need BlynkGSM_ESP32_config.checkSum = 0; @@ -1398,17 +1668,78 @@ class BlynkWifi #endif #ifndef EEPROM_START - #define EEPROM_START 0 + #define EEPROM_START 0 //define 256 in DRD/MRD #else - #if (EEPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE) - #error EPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. + #if (EEPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE > EEPROM_SIZE) + #error EPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE > EEPROM_SIZE. Please adjust. #endif #endif // Stating positon to store BlynkGSM_ESP32_config #define BLYNK_EEPROM_START (EEPROM_START + FLAG_DATA_SIZE) - bool checkDynamicData(void) + ////////////////////////////////////////////// + + void setForcedCP(bool isPersistent) + { + uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("setForcedCP")); +#endif + + EEPROM.put(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, readForcedConfigPortalFlag); + EEPROM.commit(); + } + ////////////////////////////////////////////// + + void clearForcedCP() + { +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("clearForcedCP")); +#endif + + EEPROM.put(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, 0); + EEPROM.commit(); + } + + ////////////////////////////////////////////// + + bool isForcedCP() + { + uint32_t readForcedConfigPortalFlag; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("Check if isForcedCP")); +#endif + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + EEPROM.get(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, readForcedConfigPortalFlag); + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + if (readForcedConfigPortalFlag == FORCED_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = false; + return true; + } + else if (readForcedConfigPortalFlag == FORCED_PERS_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = true; + return true; + } + else + { + return false; + } + } + + ////////////////////////////////////////////// + +#if USE_DYNAMIC_PARAMETERS + + bool checkDynamicData() { int checkSum = 0; int readCheckSum; @@ -1416,13 +1747,13 @@ class BlynkWifi #define BUFFER_LEN 128 char readBuffer[BUFFER_LEN + 1]; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; // Find the longest pdata, then dynamically allocate buffer. Remember to free when done // This is used to store tempo data to calculate checksum to see of data is valid // We dont like to destroy myMenuItems[i].pdata with invalid data - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { if (myMenuItems[i].maxlen > BUFFER_LEN) { @@ -1432,7 +1763,7 @@ class BlynkWifi } } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = readBuffer; @@ -1468,16 +1799,17 @@ class BlynkWifi return true; } - - bool EEPROM_getDynamicData(void) + ////////////////////////////////////////////// + + bool EEPROM_getDynamicData() { int readCheckSum; int checkSum = 0; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; totalDataSize = sizeof(BlynkGSM_ESP32_config) + sizeof(readCheckSum); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; totalDataSize += myMenuItems[i].maxlen; @@ -1507,13 +1839,15 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// - void EEPROM_putDynamicData(void) + void EEPROM_putDynamicData() { int checkSum = 0; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP32_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1534,10 +1868,13 @@ class BlynkWifi BLYNK_LOG2(F("CrWCSum=0x"), String(checkSum, HEX)); } +#endif + + ////////////////////////////////////////////// bool getConfigData() { - bool dynamicDataValid; + bool dynamicDataValid = true; int calChecksum; hadConfigData = false; @@ -1566,6 +1903,7 @@ class BlynkWifi { // Load data from EEPROM EEPROM.get(BLYNK_EEPROM_START, BlynkGSM_ESP32_config); + NULLTerminateConfig(); #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("======= Start Stored Config Data =======")); @@ -1576,21 +1914,24 @@ class BlynkWifi BLYNK_LOG4(BLYNK_F("CCSum=0x"), String(calChecksum, HEX), BLYNK_F(",RCSum=0x"), String(BlynkGSM_ESP32_config.checkSum, HEX)); + +#if USE_DYNAMIC_PARAMETERS // Load dynamic data from EEPROM dynamicDataValid = EEPROM_getDynamicData(); if (dynamicDataValid) { -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Valid Stored Dynamic Data")); -#endif + #endif } -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) else { BLYNK_LOG1(BLYNK_F("Invalid Stored Dynamic Data. Ignored")); } + #endif #endif } @@ -1598,7 +1939,7 @@ class BlynkWifi (calChecksum != BlynkGSM_ESP32_config.checkSum) || !dynamicDataValid ) { // Including Credentials CSum - BLYNK_LOG4(F("InitEEPROM,sz="), EEPROM_SIZE, F(",Datasz="), totalDataSize); + BLYNK_LOG4(F("InitEEPROM,sz="), EEPROM_SIZE, F(",DataSz="), totalDataSize); // doesn't have any configuration if (LOAD_DEFAULT_CONFIG_DATA) @@ -1626,22 +1967,24 @@ class BlynkWifi BlynkGSM_ESP32_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; strcpy(BlynkGSM_ESP32_config.board_name, NO_CONFIG); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { // Actual size of pdata is [maxlen + 1] memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); } +#endif } strcpy(BlynkGSM_ESP32_config.header, BLYNK_BOARD_TYPE); -#if ( BLYNK_WM_DEBUG > 2) - for (int i = 0; i < NUM_MENU_ITEMS; i++) + #if ( USE_DYNAMIC_PARAMETERS && ( BLYNK_WM_DEBUG > 2) ) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { BLYNK_LOG4(BLYNK_F("g:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); } -#endif + #endif // Don't need BlynkGSM_ESP32_config.checkSum = 0; @@ -1675,6 +2018,8 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// void saveConfigData() { @@ -1687,29 +2032,41 @@ class BlynkWifi EEPROM.commit(); } - void saveAllConfigData(void) + ////////////////////////////////////////////// + + void saveAllConfigData() { int calChecksum = calcChecksum(); BlynkGSM_ESP32_config.checkSum = calChecksum; BLYNK_LOG4(BLYNK_F("SaveEEPROM,sz="), EEPROM_SIZE, BLYNK_F(",CSum=0x"), String(calChecksum, HEX)) EEPROM.put(BLYNK_EEPROM_START, BlynkGSM_ESP32_config); + +#if USE_DYNAMIC_PARAMETERS EEPROM_putDynamicData(); +#endif EEPROM.commit(); } #endif - bool connectMultiBlynk(void) + ////////////////////////////////////////////// + + bool connectMultiBlynk() { #define BLYNK_CONNECT_TIMEOUT_MS 10000L - for (int i = 0; i < NUM_BLYNK_CREDENTIALS; i++) + for (uint16_t i = 0; i < NUM_BLYNK_CREDENTIALS; i++) { config(BlynkGSM_ESP32_config.Blynk_Creds[i].wifi_blynk_token, BlynkGSM_ESP32_config.Blynk_Creds[i].blynk_server, BLYNK_SERVER_HARDWARE_PORT); +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("Connecting to Blynk Server = "), BlynkGSM_ESP32_config.Blynk_Creds[i].blynk_server, + BLYNK_F(", Token = "), BlynkGSM_ESP32_config.Blynk_Creds[i].wifi_blynk_token); +#endif + if (connect(BLYNK_CONNECT_TIMEOUT_MS) ) { BLYNK_LOG4(BLYNK_F("Connected to Blynk Server = "), BlynkGSM_ESP32_config.Blynk_Creds[i].blynk_server, @@ -1723,8 +2080,10 @@ class BlynkWifi return false; } + + ////////////////////////////////////////////// - uint8_t connectMultiWiFi(void) + uint8_t connectMultiWiFi() { // For ESP32, this better be 2000 to enable connect the 1st time #define WIFI_MULTI_CONNECT_WAITING_MS 2000L @@ -1763,14 +2122,16 @@ class BlynkWifi return status; } - // NEW + ////////////////////////////////////////////// + void createHTML(String &root_html_template) { String pitem; root_html_template = String(BLYNK_GSM_HTML_HEAD) + BLYNK_GSM_FLDSET_START; - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { pitem = String(BLYNK_GSM_HTML_PARAM); @@ -1780,10 +2141,12 @@ class BlynkWifi root_html_template += pitem; } +#endif root_html_template += String(BLYNK_GSM_FLDSET_END) + BLYNK_GSM_HTML_BUTTON + BLYNK_GSM_HTML_SCRIPT; - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { pitem = String(BLYNK_GSM_HTML_SCRIPT_ITEM); @@ -1791,12 +2154,14 @@ class BlynkWifi root_html_template += pitem; } +#endif root_html_template += String(BLYNK_GSM_HTML_SCRIPT_END) + BLYNK_GSM_HTML_END; return; } - //// + + ////////////////////////////////////////////// void handleRequest() { @@ -1849,11 +2214,17 @@ class BlynkWifi result.replace("[[pt]]", String(BlynkGSM_ESP32_config.blynk_port)); result.replace("[[nm]]", BlynkGSM_ESP32_config.board_name); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + // Load default configuration + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { String toChange = String("[[") + myMenuItems[i].id + "]]"; result.replace(toChange, myMenuItems[i].pdata); + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("h1:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ) + #endif } +#endif server->send(200, "text/html", result); @@ -1865,13 +2236,59 @@ class BlynkWifi memset(&BlynkGSM_ESP32_config, 0, sizeof(BlynkGSM_ESP32_config)); strcpy(BlynkGSM_ESP32_config.header, BLYNK_BOARD_TYPE); } + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) + { + // 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 - if (key == "id") + 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 wtk_Updated = false; + static bool gtk_Updated = false; + + static bool sv1_Updated = false; + static bool wtk1_Updated = false; + static bool gtk1_Updated = false; + + static bool apn_Updated = false; + static bool usr_Updated = false; + static bool pwd_Updated = false; + static bool pin_Updated = false; + + static bool pt_Updated = false; + static bool nm_Updated = false; + + if (!id_Updated && (key == String("id"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("id="), value.c_str()); #endif + id_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_ssid) - 1) @@ -1879,12 +2296,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pw="), value.c_str()); #endif - + + pw_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_pw) - 1) @@ -1892,12 +2310,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(BlynkGSM_ESP32_config.WiFi_Creds[0].wifi_pw) - 1); } - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("id1="), value.c_str()); #endif - + + id1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_ssid) - 1) @@ -1905,12 +2324,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pw1="), value.c_str()); #endif + pw1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_pw) - 1) @@ -1918,12 +2338,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(BlynkGSM_ESP32_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("sv="), value.c_str()); #endif - + + sv_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].blynk_server) - 1) @@ -1931,12 +2352,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "wtk") + else if (!wtk_Updated && (key == String("wtk"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("wtk="), value.c_str()); #endif - + + wtk_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].wifi_blynk_token) - 1) @@ -1944,12 +2366,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[0].wifi_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].wifi_blynk_token) - 1); } - else if (key == "gtk") + else if (!gtk_Updated && (key == String("gtk"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("gtk="), value.c_str()); #endif - + + gtk_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].gsm_blynk_token) - 1) @@ -1957,12 +2380,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[0].gsm_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[0].gsm_blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("sv1="), value.c_str()); #endif - + + sv1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].blynk_server) - 1) @@ -1970,12 +2394,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "wtk1") + else if (!wtk1_Updated && (key == String("wtk1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("wtk1="), value.c_str()); #endif - + + wtk1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].wifi_blynk_token) - 1) @@ -1983,12 +2408,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[1].wifi_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].wifi_blynk_token) - 1); } - else if (key == "gtk1") + else if (!gtk1_Updated && (key == String("gtk1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("gtk1="), value.c_str()); #endif - + + gtk1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].gsm_blynk_token) - 1) @@ -1996,12 +2422,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.Blynk_Creds[1].gsm_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP32_config.Blynk_Creds[1].gsm_blynk_token) - 1); } - else if (key == "apn") + else if (!apn_Updated && (key == String("apn"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("apn="), value.c_str()); #endif - + + apn_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.apn) - 1) @@ -2009,12 +2436,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.apn, value.c_str(), sizeof(BlynkGSM_ESP32_config.apn) - 1); } - else if (key == "usr") + else if (!usr_Updated && (key == String("usr"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("usr="), value.c_str()); #endif - + + usr_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.gprsUser) - 1) @@ -2022,12 +2450,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.gprsUser, value.c_str(), sizeof(BlynkGSM_ESP32_config.gprsUser) - 1); } - else if (key == "pwd") + else if (!pwd_Updated && (key == String("pwd"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pwd="), value.c_str()); #endif - + + pwd_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.gprsPass) - 1) @@ -2035,36 +2464,38 @@ class BlynkWifi else strncpy(BlynkGSM_ESP32_config.gprsPass, value.c_str(), sizeof(BlynkGSM_ESP32_config.gprsPass) - 1); } - else if (key == "pin") + else if (!pin_Updated && (key == String("pin"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pin="), value.c_str()); #endif - + + pin_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.gprsPin) - 1) strcpy(BlynkGSM_ESP32_config.gprsPin, value.c_str()); else strncpy(BlynkGSM_ESP32_config.gprsPin, value.c_str(), sizeof(BlynkGSM_ESP32_config.gprsPin) - 1); - } - - else if (key == "pt") + } + else if (!pt_Updated && (key == String("pt"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pt="), value.toInt()); #endif + pt_Updated = true; number_items_Updated++; BlynkGSM_ESP32_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("nm="), value.c_str()); #endif - + + nm_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP32_config.board_name) - 1) @@ -2073,13 +2504,14 @@ class BlynkWifi strncpy(BlynkGSM_ESP32_config.board_name, value.c_str(), sizeof(BlynkGSM_ESP32_config.board_name) - 1); } - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#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) ) { -#if ( BLYNK_WM_DEBUG > 2) - BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); -#endif + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; number_items_Updated++; @@ -2090,17 +2522,20 @@ class BlynkWifi strcpy(myMenuItems[i].pdata, value.c_str()); else strncpy(myMenuItems[i].pdata, value.c_str(), myMenuItems[i].maxlen); - #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG4(BLYNK_F("h2:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); -#endif +#endif } } +#endif server->send(200, "text/html", "OK"); - // NEW +#if USE_DYNAMIC_PARAMETERS if (number_items_Updated == NUM_CONFIGURABLE_ITEMS + NUM_MENU_ITEMS) +#else + if (number_items_Updated == NUM_CONFIGURABLE_ITEMS) +#endif { #if USE_LITTLEFS BLYNK_LOG2(BLYNK_F("h:Updating LittleFS:"), CONFIG_FILENAME); @@ -2111,20 +2546,31 @@ class BlynkWifi #endif saveAllConfigData(); + + // Done with CP, Clear CP Flag here if forced + if (isForcedConfigPortal) + { + clearForcedCP(); + } BLYNK_LOG1(BLYNK_F("h:Rst")); - // Delay then reset the ESP32 after save data + // Delay then reset the ESP8266 after save data delay(1000); ESP.restart(); } } // if (server) } + + ////////////////////////////////////////////// + +#ifndef CONFIG_TIMEOUT + #warning Default CONFIG_TIMEOUT = 60s + #define CONFIG_TIMEOUT 60000L +#endif void startConfigurationMode() { -#define CONFIG_TIMEOUT 60000L - // turn the LED_BUILTIN ON to tell us we are in configuration mode. digitalWrite(LED_BUILTIN, LED_ON); @@ -2174,9 +2620,20 @@ class BlynkWifi // If there is no saved config Data, stay in config mode forever until having config Data. if (hadConfigData) + { configTimeout = millis() + CONFIG_TIMEOUT; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("s:millis() = "), millis(), BLYNK_F(", configTimeout = "), configTimeout); +#endif + } else + { configTimeout = 0; +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("s:configTimeout = 0")); +#endif + } configuration_mode = true; } diff --git a/src/BlynkSimpleEsp8266_GSM_WF.h b/src/BlynkSimpleEsp8266_GSM_WF.h index bc6c47d..659d28a 100644 --- a/src/BlynkSimpleEsp8266_GSM_WF.h +++ b/src/BlynkSimpleEsp8266_GSM_WF.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once @@ -44,7 +46,7 @@ #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. #endif -#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.1.0" +#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.2.0" #define BLYNK_SEND_ATOMIC diff --git a/src/BlynkSimpleEsp8266_GSM_WFM.h b/src/BlynkSimpleEsp8266_GSM_WFM.h index 7e7a7b2..ed7bd10 100644 --- a/src/BlynkSimpleEsp8266_GSM_WFM.h +++ b/src/BlynkSimpleEsp8266_GSM_WFM.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once @@ -44,7 +46,7 @@ #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. #endif -#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.1.0" +#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.2.0" #define BLYNK_SEND_ATOMIC @@ -201,8 +203,11 @@ typedef struct uint8_t maxlen; } MenuItem; -extern uint16_t NUM_MENU_ITEMS; -extern MenuItem myMenuItems []; +#if USE_DYNAMIC_PARAMETERS + extern uint16_t NUM_MENU_ITEMS; + extern MenuItem myMenuItems []; + bool *menuItemUpdated = NULL; +#endif #define SSID_MAX_LEN 32 // WPA2 passwords can be up to 63 characters long. @@ -214,9 +219,13 @@ typedef struct char wifi_pw [PASS_MAX_LEN]; } WiFi_Credentials; +#define HEADER_MAX_LEN 16 + #define BLYNK_SERVER_MAX_LEN 32 #define BLYNK_TOKEN_MAX_LEN 36 +#define BOARD_NAME_MAX_LEN 24 + typedef struct { char blynk_server [BLYNK_SERVER_MAX_LEN]; @@ -231,21 +240,27 @@ typedef struct #define NUM_CONFIGURABLE_ITEMS ( 6 + (2 * NUM_WIFI_CREDENTIALS) + (3 * NUM_BLYNK_CREDENTIALS) ) #define DEFAULT_GPRS_PIN "1234" +#define APN_MAX_LEN 32 +#define GPRS_USER_MAX_LEN 32 +#define GPRS_PASS_MAX_LEN 32 +#define GPRS_PIN_MAX_LEN 12 + typedef struct Configuration { - char header [16]; + char header [HEADER_MAX_LEN]; WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; Blynk_Credentials Blynk_Creds [NUM_BLYNK_CREDENTIALS]; int blynk_port; // YOUR GSM / GPRS RELATED - char apn [32]; - char gprsUser [32]; - char gprsPass [32]; - char gprsPin [12]; // A PIN (Personal Identification Number) is a 4-8 digit passcode + char apn [APN_MAX_LEN]; + char gprsUser [GPRS_USER_MAX_LEN]; + char gprsPass [GPRS_PASS_MAX_LEN]; + char gprsPin [GPRS_PIN_MAX_LEN]; // A PIN (Personal Identification Number) is a 4-8 digit passcode // END OF YOUR GSM / GPRS RELATED - char board_name [24]; + char board_name [BOARD_NAME_MAX_LEN]; int checkSum; } Blynk_WF_Configuration; + // Currently CONFIG_DATA_SIZE = ( 156 + (96 * NUM_WIFI_CREDENTIALS) + (104 * NUM_BLYNK_CREDENTIALS) ) = 556 uint16_t CONFIG_DATA_SIZE = sizeof(Blynk_WF_Configuration); @@ -407,10 +422,13 @@ class BlynkWifi } //// New DRD/MRD //// -#if ( BLYNK_WM_DEBUG > 2) - BLYNK_LOG1(BLYNK_F("======= Start Default Config Data =======")); - displayConfigData(defaultConfig); -#endif +#if ( BLYNK_WM_DEBUG > 2) + if (LOAD_DEFAULT_CONFIG_DATA) + { + BLYNK_LOG1(BLYNK_F("======= Start Default Config Data =======")); + displayConfigData(defaultConfig); + } +#endif WiFi.mode(WIFI_STA); @@ -420,7 +438,6 @@ class BlynkWifi _hostname.toUpperCase(); getRFC952_hostname(_hostname.c_str()); - } else { @@ -428,27 +445,28 @@ class BlynkWifi getRFC952_hostname(iHostname); } - BLYNK_LOG2(BLYNK_F("Hostname="), RFC952_hostname); - + BLYNK_LOG2(BLYNK_F("Hostname="), RFC952_hostname); + + hadConfigData = getConfigData(); + + isForcedConfigPortal = isForcedCP(); + //// New DRD/MRD //// // noConfigPortal when getConfigData() OK and no MRD/DRD'ed - if (getConfigData() && noConfigPortal) - //// New DRD/MRD //// + //if (getConfigData() && noConfigPortal) + if (hadConfigData && noConfigPortal && (!isForcedConfigPortal) ) { hadConfigData = true; - + #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(noConfigPortal? BLYNK_F("bg: noConfigPortal = true") : BLYNK_F("bg: noConfigPortal = false")); #endif - for (int i = 0; i < NUM_WIFI_CREDENTIALS; i++) + for (uint16_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) { wifiMulti.addAP(BlynkGSM_ESP8266_config.WiFi_Creds[i].wifi_ssid, BlynkGSM_ESP8266_config.WiFi_Creds[i].wifi_pw); } - //Base::begin(BlynkGSM_ESP8266_config.blynk_token); - //this->conn.begin(BlynkGSM_ESP8266_config.blynk_server, BlynkGSM_ESP8266_config.blynk_port); - if (connectMultiWiFi() == WL_CONNECTED) { BLYNK_LOG1(BLYNK_F("bg: WiFi OK. Try Blynk")); @@ -478,11 +496,25 @@ class BlynkWifi } else { - BLYNK_LOG2(BLYNK_F("bg: Stay forever in config portal."), - noConfigPortal ? BLYNK_F("No configDat") : BLYNK_F("DRD/MRD detected")); +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(isForcedConfigPortal? BLYNK_F("bg: isForcedConfigPortal = true") : BLYNK_F("bg: isForcedConfigPortal = false")); +#endif + + // If not persistent => clear the flag so that after reset. no more CP, even CP not entered and saved + if (persForcedConfigPortal) + { + BLYNK_LOG2(BLYNK_F("bg:Stay forever in CP:"), isForcedConfigPortal ? BLYNK_F("Forced-Persistent") : (noConfigPortal ? BLYNK_F("No ConfigDat") : BLYNK_F("DRD/MRD"))); + } + else + { + BLYNK_LOG2(BLYNK_F("bg:Stay forever in CP:"), isForcedConfigPortal ? BLYNK_F("Forced-non-Persistent") : (noConfigPortal ? BLYNK_F("No ConfigDat") : BLYNK_F("DRD/MRD"))); + clearForcedCP(); + + } + hadConfigData = isForcedConfigPortal ? true : (noConfigPortal ? false : true); + // failed to connect to Blynk server, will start configuration mode - hadConfigData = false; startConfigurationMode(); } } @@ -618,10 +650,15 @@ class BlynkWifi digitalWrite(LED_BUILTIN, LED_OFF); } - Base::run(); + if (connected()) + { + Base::run(); + } } - void setHostname(void) + ////////////////////////////////////////////// + + void setHostname() { if (RFC952_hostname[0] != 0) { @@ -641,7 +678,7 @@ class BlynkWifi } #define MIN_WIFI_CHANNEL 1 -#define MAX_WIFI_CHANNEL 12 +#define MAX_WIFI_CHANNEL 11 int setConfigPortalChannel(int channel = 1) { @@ -797,6 +834,45 @@ class BlynkWifi memset(&BlynkGSM_ESP8266_config, 0, sizeof(BlynkGSM_ESP8266_config)); saveConfigData(); } + + bool inConfigPortal() + { + return configuration_mode; + } + + // Forced CP => Flag = 0xBEEFBEEF. Else => No forced CP + // Flag to be stored at (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // to avoid corruption to current data + //#define FORCED_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xDEADBEEF) + //#define FORCED_PERS_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xBEEFDEAD) + + const uint32_t FORCED_CONFIG_PORTAL_FLAG_DATA = 0xDEADBEEF; + const uint32_t FORCED_PERS_CONFIG_PORTAL_FLAG_DATA = 0xBEEFDEAD; + + #define FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE 4 + + void resetAndEnterConfigPortal() + { + persForcedConfigPortal = false; + + setForcedCP(false); + + // Delay then reset the ESP8266 after save data + delay(1000); + ESP.reset(); + } + + // This will keep CP forever, until you successfully enter CP, and Save data to clear the flag. + void resetAndEnterConfigPortalPersistent() + { + persForcedConfigPortal = true; + + setForcedCP(true); + + // Delay then reset the ESP8266 after save data + delay(1000); + ESP.reset(); + } private: ESP8266WebServer *server; @@ -806,6 +882,9 @@ class BlynkWifi unsigned long configTimeout; bool hadConfigData = false; + + bool isForcedConfigPortal = false; + bool persForcedConfigPortal = false; // default to channel 1 int WiFiAPChannel = 1; @@ -875,13 +954,15 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("======= End Config Data =======")); } - void displayWiFiData(void) + void displayWiFiData() { BLYNK_LOG6(BLYNK_F("SSID="), WiFi.SSID(), BLYNK_F(",RSSI="), WiFi.RSSI(), BLYNK_F(",Channel="), WiFi.channel()); BLYNK_LOG6(BLYNK_F("IP="), WiFi.localIP(), BLYNK_F(",GW="), WiFi.gatewayIP(), BLYNK_F(",SN="), WiFi.subnetMask()); BLYNK_LOG4(BLYNK_F("DNS1="), WiFi.dnsIP(0), BLYNK_F(",DNS2="), WiFi.dnsIP(1)); } + + ////////////////////////////////////////////// int calcChecksum() { @@ -893,17 +974,177 @@ class BlynkWifi return checkSum; } + + ////////////////////////////////////////////// + + void NULLTerminateConfig() + { + //#define SSID_MAX_LEN 32 + //#define PASS_MAX_LEN 64 + + //#define HEADER_MAX_LEN 16 + //#define BLYNK_SERVER_MAX_LEN 32 + //#define BLYNK_TOKEN_MAX_LEN 36 + //#define BOARD_NAME_MAX_LEN 24 + + //#define APN_MAX_LEN 32 + //#define GPRS_USER_MAX_LEN 32 + //#define GPRS_PASS_MAX_LEN 32 + //#define GPRS_PIN_MAX_LEN 12 + + // NULL Terminating to be sure + BlynkGSM_ESP8266_config.header[HEADER_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_ssid[SSID_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_pw[PASS_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_ssid[SSID_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_pw[PASS_MAX_LEN - 1] = 0; + + BlynkGSM_ESP8266_config.apn[APN_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.gprsUser[GPRS_USER_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.gprsPass[GPRS_PASS_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.gprsPin[GPRS_PIN_MAX_LEN - 1] = 0; + + BlynkGSM_ESP8266_config.Blynk_Creds[0].blynk_server[BLYNK_SERVER_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.Blynk_Creds[0].wifi_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.Blynk_Creds[0].gsm_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.Blynk_Creds[1].blynk_server[BLYNK_SERVER_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.Blynk_Creds[1].wifi_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.Blynk_Creds[1].gsm_blynk_token[BLYNK_TOKEN_MAX_LEN - 1] = 0; + BlynkGSM_ESP8266_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; + BlynkGSM_ESP8266_config.board_name [BOARD_NAME_MAX_LEN - 1] = 0; + } + + ////////////////////////////////////////////// #if ( USE_LITTLEFS || USE_SPIFFS ) -#define CONFIG_FILENAME BLYNK_F("/gsm_config.dat") -#define CONFIG_FILENAME_BACKUP BLYNK_F("/gsm_config.bak") +#define CONFIG_FILENAME BLYNK_F("/gsm_config.dat") +#define CONFIG_FILENAME_BACKUP BLYNK_F("/gsm_config.bak") + +#define CREDENTIALS_FILENAME BLYNK_F("/gsm_cred.dat") +#define CREDENTIALS_FILENAME_BACKUP BLYNK_F("/gsm_cred.bak") + +#define CONFIG_PORTAL_FILENAME BLYNK_F("/gsm_cp.dat") +#define CONFIG_PORTAL_FILENAME_BACKUP BLYNK_F("/gsm_cp.bak") + + ////////////////////////////////////////////// + + void saveForcedCP(uint32_t value) + { + File file = FileFS.open(CONFIG_PORTAL_FILENAME, "w"); + + BLYNK_LOG1(BLYNK_F("SaveCPFile ")); + + if (file) + { + file.write((uint8_t*) &value, sizeof(value)); + file.close(); + BLYNK_LOG1(BLYNK_F("OK")); + } + else + { + BLYNK_LOG1(BLYNK_F("failed")); + } + + // Trying open redundant CP file + file = FileFS.open(CONFIG_PORTAL_FILENAME_BACKUP, "w"); + + BLYNK_LOG1(BLYNK_F("SaveBkUpCPFile ")); + + if (file) + { + file.write((uint8_t *) &value, sizeof(value)); + file.close(); + BLYNK_LOG1(BLYNK_F("OK")); + } + else + { + BLYNK_LOG1(BLYNK_F("failed")); + } + } + + ////////////////////////////////////////////// + + void setForcedCP(bool isPersistent) + { + uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(isPersistent ? BLYNK_F("setForcedCP Persistent") : BLYNK_F("setForcedCP non-Persistent")); +#endif + + saveForcedCP(readForcedConfigPortalFlag); + } + + ////////////////////////////////////////////// + + void clearForcedCP() + { + uint32_t readForcedConfigPortalFlag = 0; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("clearForcedCP")); +#endif + + saveForcedCP(readForcedConfigPortalFlag); + } + + ////////////////////////////////////////////// + + bool isForcedCP() + { + uint32_t readForcedConfigPortalFlag; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("Check if isForcedCP")); +#endif + + File file = FileFS.open(CONFIG_PORTAL_FILENAME, "r"); + BLYNK_LOG1(BLYNK_F("LoadCPFile ")); + + if (!file) + { + BLYNK_LOG1(BLYNK_F("failed")); + + // Trying open redundant config file + file = FileFS.open(CONFIG_PORTAL_FILENAME_BACKUP, "r"); + BLYNK_LOG1(BLYNK_F("LoadBkUpCPFile ")); -#define CREDENTIALS_FILENAME BLYNK_F("/gsm_cred.dat") -#define CREDENTIALS_FILENAME_BACKUP BLYNK_F("/gsm_cred.bak") + if (!file) + { + BLYNK_LOG1(BLYNK_F("failed")); + return false; + } + } + + file.readBytes((char *) &readForcedConfigPortalFlag, sizeof(readForcedConfigPortalFlag)); + + BLYNK_LOG1(BLYNK_F("OK")); + file.close(); + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + if (readForcedConfigPortalFlag == FORCED_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = false; + return true; + } + else if (readForcedConfigPortalFlag == FORCED_PERS_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = true; + return true; + } + else + { + return false; + } + } + + ////////////////////////////////////////////// +#if USE_DYNAMIC_PARAMETERS - bool checkDynamicData(void) + bool checkDynamicData() { int checkSum = 0; int readCheckSum; @@ -932,7 +1173,7 @@ class BlynkWifi // We dont like to destroy myMenuItems[i].pdata with invalid data uint16_t maxBufferLength = 0; - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { if (myMenuItems[i].maxlen > maxBufferLength) maxBufferLength = myMenuItems[i].maxlen; @@ -948,7 +1189,7 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("ChkCrR: Error can't allocate buffer.")); return false; } -#if ( BLYNK_WM_DEBUG > 2) +#if ( BLYNK_WM_DEBUG > 2) else { BLYNK_LOG2(BLYNK_F("ChkCrR: Buffer allocated, sz="), maxBufferLength + 1); @@ -956,7 +1197,7 @@ class BlynkWifi #endif } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = readBuffer; @@ -986,10 +1227,7 @@ class BlynkWifi if (readBuffer != NULL) { free(readBuffer); - -#if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Buffer freed")); -#endif } if ( checkSum != readCheckSum) @@ -999,8 +1237,10 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// - bool loadDynamicData(void) + bool loadDynamicData() { int checkSum = 0; int readCheckSum; @@ -1024,7 +1264,7 @@ class BlynkWifi } } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; totalDataSize += myMenuItems[i].maxlen; @@ -1059,14 +1299,16 @@ class BlynkWifi return true; } - void saveDynamicData(void) + ////////////////////////////////////////////// + + void saveDynamicData() { int checkSum = 0; File file = FileFS.open(CREDENTIALS_FILENAME, "w"); BLYNK_LOG1(BLYNK_F("SaveCredFile ")); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1106,7 +1348,7 @@ class BlynkWifi file = FileFS.open(CREDENTIALS_FILENAME_BACKUP, "w"); BLYNK_LOG1(BLYNK_F("SaveBkUpCredFile ")); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1140,8 +1382,11 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("failed")); } } +#endif - void loadConfigData(void) + ////////////////////////////////////////////// + + void loadConfigData() { File file = FileFS.open(CONFIG_FILENAME, "r"); BLYNK_LOG1(BLYNK_F("LoadCfgFile ")); @@ -1165,9 +1410,13 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("OK")); file.close(); + + NULLTerminateConfig(); } + + ////////////////////////////////////////////// - void saveConfigData(void) + void saveConfigData() { File file = FileFS.open(CONFIG_FILENAME, "w"); BLYNK_LOG1(BLYNK_F("SaveCfgFile ")); @@ -1202,20 +1451,27 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("failed")); } - saveDynamicData(); + //saveDynamicData(); } - void saveAllConfigData(void) + ////////////////////////////////////////////// + + void saveAllConfigData() { saveConfigData(); + +#if USE_DYNAMIC_PARAMETERS saveDynamicData(); +#endif } + + ////////////////////////////////////////////// // Return false if init new EEPROM, LittleFS or SPIFFS. No more need trying to connect. Go directly to config mode bool getConfigData() { - bool dynamicDataValid; - int calChecksum; + bool dynamicDataValid = true; + int calChecksum; hadConfigData = false; @@ -1251,8 +1507,12 @@ class BlynkWifi // Don't need Config Portal anymore return true; } +#if USE_DYNAMIC_PARAMETERS else if ( ( FileFS.exists(CONFIG_FILENAME) || FileFS.exists(CONFIG_FILENAME_BACKUP) ) && ( FileFS.exists(CREDENTIALS_FILENAME) || FileFS.exists(CREDENTIALS_FILENAME_BACKUP) ) ) +#else + else if ( FileFS.exists(CONFIG_FILENAME) || FileFS.exists(CONFIG_FILENAME_BACKUP) ) +#endif { // if config file exists, load loadConfigData(); @@ -1266,21 +1526,23 @@ class BlynkWifi BLYNK_LOG4(BLYNK_F("CCSum=0x"), String(calChecksum, HEX), BLYNK_F(",RCSum=0x"), String(BlynkGSM_ESP8266_config.checkSum, HEX)); - + +#if USE_DYNAMIC_PARAMETERS // Load dynamic data dynamicDataValid = loadDynamicData(); if (dynamicDataValid) { -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Valid Stored Dynamic Data")); -#endif + #endif } -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) else { BLYNK_LOG1(BLYNK_F("Invalid Stored Dynamic Data. Ignored")); } + #endif #endif } else @@ -1322,22 +1584,24 @@ class BlynkWifi BlynkGSM_ESP8266_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; strcpy(BlynkGSM_ESP8266_config.board_name, NO_CONFIG); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { // Actual size of pdata is [maxlen + 1] memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); } +#endif } strcpy(BlynkGSM_ESP8266_config.header, BLYNK_BOARD_TYPE); -#if ( BLYNK_WM_DEBUG > 2) - for (int i = 0; i < NUM_MENU_ITEMS; i++) + #if (USE_DYNAMIC_PARAMETERS && ( BLYNK_WM_DEBUG > 2) ) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { BLYNK_LOG4(BLYNK_F("g:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); } -#endif + #endif // Don't need BlynkGSM_ESP8266_config.checkSum = 0; @@ -1392,10 +1656,10 @@ class BlynkWifi #endif #ifndef EEPROM_START - #define EEPROM_START 0 + #define EEPROM_START 0 //define 256 in DRD/MRD #else - #if (EEPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE) - #error EPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. + #if (EEPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE > EEPROM_SIZE) + #error EPROM_START + FLAG_DATA_SIZE + CONFIG_DATA_SIZE + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE > EEPROM_SIZE. Please adjust. #endif #endif @@ -1403,7 +1667,68 @@ class BlynkWifi #define BLYNK_EEPROM_START (EEPROM_START + FLAG_DATA_SIZE) - bool checkDynamicData(void) + ////////////////////////////////////////////// + + void setForcedCP(bool isPersistent) + { + uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("setForcedCP")); +#endif + + EEPROM.put(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, readForcedConfigPortalFlag); + EEPROM.commit(); + } + ////////////////////////////////////////////// + + void clearForcedCP() + { +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("clearForcedCP")); +#endif + + EEPROM.put(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, 0); + EEPROM.commit(); + } + + ////////////////////////////////////////////// + + bool isForcedCP() + { + uint32_t readForcedConfigPortalFlag; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("Check if isForcedCP")); +#endif + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + EEPROM.get(BLYNK_EEPROM_START + CONFIG_DATA_SIZE, readForcedConfigPortalFlag); + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + if (readForcedConfigPortalFlag == FORCED_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = false; + return true; + } + else if (readForcedConfigPortalFlag == FORCED_PERS_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = true; + return true; + } + else + { + return false; + } + } + + ////////////////////////////////////////////// + +#if USE_DYNAMIC_PARAMETERS + + bool checkDynamicData() { int checkSum = 0; int readCheckSum; @@ -1411,13 +1736,13 @@ class BlynkWifi #define BUFFER_LEN 128 char readBuffer[BUFFER_LEN + 1]; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; // Find the longest pdata, then dynamically allocate buffer. Remember to free when done // This is used to store tempo data to calculate checksum to see of data is valid // We dont like to destroy myMenuItems[i].pdata with invalid data - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { if (myMenuItems[i].maxlen > BUFFER_LEN) { @@ -1427,7 +1752,7 @@ class BlynkWifi } } - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = readBuffer; @@ -1463,16 +1788,17 @@ class BlynkWifi return true; } - - bool EEPROM_getDynamicData(void) + ////////////////////////////////////////////// + + bool EEPROM_getDynamicData() { int readCheckSum; int checkSum = 0; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; totalDataSize = sizeof(BlynkGSM_ESP8266_config) + sizeof(readCheckSum); - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; totalDataSize += myMenuItems[i].maxlen; @@ -1502,13 +1828,15 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// - void EEPROM_putDynamicData(void) + void EEPROM_putDynamicData() { int checkSum = 0; - uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config); + uint16_t offset = BLYNK_EEPROM_START + sizeof(BlynkGSM_ESP8266_config) + FORCED_CONFIG_PORTAL_FLAG_DATA_SIZE; - for (int i = 0; i < NUM_MENU_ITEMS; i++) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -1529,10 +1857,13 @@ class BlynkWifi BLYNK_LOG2(F("CrWCSum=0x"), String(checkSum, HEX)); } +#endif + + ////////////////////////////////////////////// bool getConfigData() { - bool dynamicDataValid; + bool dynamicDataValid = true; int calChecksum; hadConfigData = false; @@ -1561,6 +1892,7 @@ class BlynkWifi { // Load data from EEPROM EEPROM.get(BLYNK_EEPROM_START, BlynkGSM_ESP8266_config); + NULLTerminateConfig(); #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("======= Start Stored Config Data =======")); @@ -1571,21 +1903,24 @@ class BlynkWifi BLYNK_LOG4(BLYNK_F("CCSum=0x"), String(calChecksum, HEX), BLYNK_F(",RCSum=0x"), String(BlynkGSM_ESP8266_config.checkSum, HEX)); + +#if USE_DYNAMIC_PARAMETERS // Load dynamic data from EEPROM dynamicDataValid = EEPROM_getDynamicData(); if (dynamicDataValid) { -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG1(BLYNK_F("Valid Stored Dynamic Data")); -#endif + #endif } -#if ( BLYNK_WM_DEBUG > 2) + #if ( BLYNK_WM_DEBUG > 2) else { BLYNK_LOG1(BLYNK_F("Invalid Stored Dynamic Data. Ignored")); } + #endif #endif } @@ -1593,7 +1928,7 @@ class BlynkWifi (calChecksum != BlynkGSM_ESP8266_config.checkSum) || !dynamicDataValid ) { // Including Credentials CSum - BLYNK_LOG4(F("InitEEPROM,sz="), EEPROM_SIZE, F(",Datasz="), totalDataSize); + BLYNK_LOG4(F("InitEEPROM,sz="), EEPROM_SIZE, F(",DataSz="), totalDataSize); // doesn't have any configuration if (LOAD_DEFAULT_CONFIG_DATA) @@ -1621,22 +1956,24 @@ class BlynkWifi BlynkGSM_ESP8266_config.blynk_port = BLYNK_SERVER_HARDWARE_PORT; strcpy(BlynkGSM_ESP8266_config.board_name, NO_CONFIG); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { // Actual size of pdata is [maxlen + 1] memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); } +#endif } strcpy(BlynkGSM_ESP8266_config.header, BLYNK_BOARD_TYPE); -#if ( BLYNK_WM_DEBUG > 2) - for (int i = 0; i < NUM_MENU_ITEMS; i++) + #if ( USE_DYNAMIC_PARAMETERS && ( BLYNK_WM_DEBUG > 2) ) + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { BLYNK_LOG4(BLYNK_F("g:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); } -#endif + #endif // Don't need BlynkGSM_ESP8266_config.checkSum = 0; @@ -1670,6 +2007,8 @@ class BlynkWifi return true; } + + ////////////////////////////////////////////// void saveConfigData() { @@ -1683,29 +2022,41 @@ class BlynkWifi EEPROM.commit(); } - void saveAllConfigData(void) + ////////////////////////////////////////////// + + void saveAllConfigData() { int calChecksum = calcChecksum(); BlynkGSM_ESP8266_config.checkSum = calChecksum; BLYNK_LOG4(BLYNK_F("SaveEEPROM,sz="), EEPROM_SIZE, BLYNK_F(",CSum=0x"), String(calChecksum, HEX)) EEPROM.put(BLYNK_EEPROM_START, BlynkGSM_ESP8266_config); + +#if USE_DYNAMIC_PARAMETERS EEPROM_putDynamicData(); +#endif EEPROM.commit(); } #endif - bool connectMultiBlynk(void) + ////////////////////////////////////////////// + + bool connectMultiBlynk() { #define BLYNK_CONNECT_TIMEOUT_MS 10000L - for (int i = 0; i < NUM_BLYNK_CREDENTIALS; i++) + for (uint16_t i = 0; i < NUM_BLYNK_CREDENTIALS; i++) { config(BlynkGSM_ESP8266_config.Blynk_Creds[i].wifi_blynk_token, BlynkGSM_ESP8266_config.Blynk_Creds[i].blynk_server, BlynkGSM_ESP8266_config.blynk_port); +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("Connecting to Blynk Server = "), BlynkGSM_ESP32_config.Blynk_Creds[i].blynk_server, + BLYNK_F(", Token = "), BlynkGSM_ESP32_config.Blynk_Creds[i].wifi_blynk_token); +#endif + if (connect(BLYNK_CONNECT_TIMEOUT_MS) ) { BLYNK_LOG4(BLYNK_F("Connected to BlynkServer="), BlynkGSM_ESP8266_config.Blynk_Creds[i].blynk_server, @@ -1717,10 +2068,11 @@ class BlynkWifi BLYNK_LOG1(BLYNK_F("Blynk not connected")); return false; - } + + ////////////////////////////////////////////// - uint8_t connectMultiWiFi(void) + uint8_t connectMultiWiFi() { // For ESP8266, this better be 3000 to enable connect the 1st time #define WIFI_MULTI_CONNECT_WAITING_MS 3000L @@ -1758,14 +2110,16 @@ class BlynkWifi return status; } - // NEW + ////////////////////////////////////////////// + void createHTML(String &root_html_template) { String pitem; root_html_template = String(BLYNK_GSM_HTML_HEAD) + BLYNK_GSM_FLDSET_START; - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { pitem = String(BLYNK_GSM_HTML_PARAM); @@ -1775,10 +2129,12 @@ class BlynkWifi root_html_template += pitem; } +#endif root_html_template += String(BLYNK_GSM_FLDSET_END) + BLYNK_GSM_HTML_BUTTON + BLYNK_GSM_HTML_SCRIPT; - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { pitem = String(BLYNK_GSM_HTML_SCRIPT_ITEM); @@ -1786,12 +2142,14 @@ class BlynkWifi root_html_template += pitem; } +#endif root_html_template += String(BLYNK_GSM_HTML_SCRIPT_END) + BLYNK_GSM_HTML_END; return; } - //// + + ////////////////////////////////////////////// void handleRequest() { @@ -1844,11 +2202,17 @@ class BlynkWifi result.replace("[[pt]]", String(BlynkGSM_ESP8266_config.blynk_port)); result.replace("[[nm]]", BlynkGSM_ESP8266_config.board_name); - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#if USE_DYNAMIC_PARAMETERS + // Load default configuration + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) { String toChange = String("[[") + myMenuItems[i].id + "]]"; result.replace(toChange, myMenuItems[i].pdata); + #if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("h1:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ) + #endif } +#endif server->send(200, "text/html", result); @@ -1860,13 +2224,59 @@ class BlynkWifi memset(&BlynkGSM_ESP8266_config, 0, sizeof(BlynkGSM_ESP8266_config)); strcpy(BlynkGSM_ESP8266_config.header, BLYNK_BOARD_TYPE); } + +#if USE_DYNAMIC_PARAMETERS + if (!menuItemUpdated) + { + // 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 wtk_Updated = false; + static bool gtk_Updated = false; + + static bool sv1_Updated = false; + static bool wtk1_Updated = false; + static bool gtk1_Updated = false; + + static bool apn_Updated = false; + static bool usr_Updated = false; + static bool pwd_Updated = false; + static bool pin_Updated = false; + + static bool pt_Updated = false; + static bool nm_Updated = false; - if (key == "id") + if (!id_Updated && (key == String("id"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("id="), value.c_str()); #endif + id_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_ssid) - 1) @@ -1874,12 +2284,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_ssid, value.c_str(), sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_ssid) - 1); } - else if (key == "pw") + else if (!pw_Updated && (key == String("pw"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pw="), value.c_str()); #endif - + + pw_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_pw) - 1) @@ -1887,12 +2298,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_pw, value.c_str(), sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[0].wifi_pw) - 1); } - else if (key == "id1") + else if (!id1_Updated && (key == String("id1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("id1="), value.c_str()); #endif - + + id1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_ssid) - 1) @@ -1900,12 +2312,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_ssid, value.c_str(), sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_ssid) - 1); } - else if (key == "pw1") + else if (!pw1_Updated && (key == String("pw1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pw1="), value.c_str()); #endif + pw1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_pw) - 1) @@ -1913,12 +2326,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(BlynkGSM_ESP8266_config.WiFi_Creds[1].wifi_pw) - 1); } - else if (key == "sv") + else if (!sv_Updated && (key == String("sv"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("sv="), value.c_str()); #endif - + + sv_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].blynk_server) - 1) @@ -1926,12 +2340,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[0].blynk_server, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].blynk_server) - 1); } - else if (key == "wtk") + else if (!wtk_Updated && (key == String("wtk"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("wtk="), value.c_str()); #endif - + + wtk_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].wifi_blynk_token) - 1) @@ -1939,12 +2354,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[0].wifi_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].wifi_blynk_token) - 1); } - else if (key == "gtk") + else if (!gtk_Updated && (key == String("gtk"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("gtk="), value.c_str()); #endif - + + gtk_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].gsm_blynk_token) - 1) @@ -1952,12 +2368,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[0].gsm_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[0].gsm_blynk_token) - 1); } - else if (key == "sv1") + else if (!sv1_Updated && (key == String("sv1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("sv1="), value.c_str()); #endif - + + sv1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].blynk_server) - 1) @@ -1965,12 +2382,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[1].blynk_server, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].blynk_server) - 1); } - else if (key == "wtk1") + else if (!wtk1_Updated && (key == String("wtk1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("wtk1="), value.c_str()); #endif - + + wtk1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].wifi_blynk_token) - 1) @@ -1978,12 +2396,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[1].wifi_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].wifi_blynk_token) - 1); } - else if (key == "gtk1") + else if (!gtk1_Updated && (key == String("gtk1"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("gtk1="), value.c_str()); #endif - + + gtk1_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].gsm_blynk_token) - 1) @@ -1991,12 +2410,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.Blynk_Creds[1].gsm_blynk_token, value.c_str(), sizeof(BlynkGSM_ESP8266_config.Blynk_Creds[1].gsm_blynk_token) - 1); } - else if (key == "apn") + else if (!apn_Updated && (key == String("apn"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("apn="), value.c_str()); #endif - + + apn_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.apn) - 1) @@ -2004,12 +2424,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.apn, value.c_str(), sizeof(BlynkGSM_ESP8266_config.apn) - 1); } - else if (key == "usr") + else if (!usr_Updated && (key == String("usr"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("usr="), value.c_str()); #endif - + + usr_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.gprsUser) - 1) @@ -2017,12 +2438,13 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.gprsUser, value.c_str(), sizeof(BlynkGSM_ESP8266_config.gprsUser) - 1); } - else if (key == "pwd") + else if (!pwd_Updated && (key == String("pwd"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pwd="), value.c_str()); #endif - + + pwd_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.gprsPass) - 1) @@ -2030,36 +2452,38 @@ class BlynkWifi else strncpy(BlynkGSM_ESP8266_config.gprsPass, value.c_str(), sizeof(BlynkGSM_ESP8266_config.gprsPass) - 1); } - else if (key == "pin") + else if (!pin_Updated && (key == String("pin"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pin="), value.c_str()); #endif - + + pin_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.gprsPin) - 1) strcpy(BlynkGSM_ESP8266_config.gprsPin, value.c_str()); else strncpy(BlynkGSM_ESP8266_config.gprsPin, value.c_str(), sizeof(BlynkGSM_ESP8266_config.gprsPin) - 1); - } - - else if (key == "pt") + } + else if (!pt_Updated && (key == String("pt"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("pt="), value.toInt()); #endif + pt_Updated = true; number_items_Updated++; BlynkGSM_ESP8266_config.blynk_port = value.toInt(); } - else if (key == "nm") + else if (!nm_Updated && (key == String("nm"))) { #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG2(BLYNK_F("nm="), value.c_str()); #endif - + + nm_Updated = true; number_items_Updated++; if (strlen(value.c_str()) < sizeof(BlynkGSM_ESP8266_config.board_name) - 1) @@ -2068,13 +2492,14 @@ class BlynkWifi strncpy(BlynkGSM_ESP8266_config.board_name, value.c_str(), sizeof(BlynkGSM_ESP8266_config.board_name) - 1); } - for (int i = 0; i < NUM_MENU_ITEMS; i++) +#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) ) { -#if ( BLYNK_WM_DEBUG > 2) - BLYNK_LOG4(F("h:"), myMenuItems[i].id, F("="), value.c_str() ); -#endif + BLYNK_LOG4(BLYNK_F("h:"), myMenuItems[i].id, BLYNK_F("="), value.c_str() ); + + menuItemUpdated[i] = true; number_items_Updated++; @@ -2085,17 +2510,20 @@ class BlynkWifi strcpy(myMenuItems[i].pdata, value.c_str()); else strncpy(myMenuItems[i].pdata, value.c_str(), myMenuItems[i].maxlen); - #if ( BLYNK_WM_DEBUG > 2) BLYNK_LOG4(BLYNK_F("h2:myMenuItems["), i, BLYNK_F("]="), myMenuItems[i].pdata ); -#endif +#endif } } +#endif server->send(200, "text/html", "OK"); - // NEW +#if USE_DYNAMIC_PARAMETERS if (number_items_Updated == NUM_CONFIGURABLE_ITEMS + NUM_MENU_ITEMS) +#else + if (number_items_Updated == NUM_CONFIGURABLE_ITEMS) +#endif { #if USE_LITTLEFS BLYNK_LOG2(BLYNK_F("h:Updating LittleFS:"), CONFIG_FILENAME); @@ -2106,6 +2534,12 @@ class BlynkWifi #endif saveAllConfigData(); + + // Done with CP, Clear CP Flag here if forced + if (isForcedConfigPortal) + { + clearForcedCP(); + } BLYNK_LOG1(BLYNK_F("h:Rst")); @@ -2115,11 +2549,16 @@ class BlynkWifi } } // if (server) } + + ////////////////////////////////////////////// + +#ifndef CONFIG_TIMEOUT + #warning Default CONFIG_TIMEOUT = 60s + #define CONFIG_TIMEOUT 60000L +#endif void startConfigurationMode() { -#define CONFIG_TIMEOUT 60000L - // turn the LED_BUILTIN ON to tell us we are in configuration mode. digitalWrite(LED_BUILTIN, LED_ON); @@ -2169,9 +2608,20 @@ class BlynkWifi // If there is no saved config Data, stay in config mode forever until having config Data. if (hadConfigData) + { configTimeout = millis() + CONFIG_TIMEOUT; + +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG4(BLYNK_F("s:millis() = "), millis(), BLYNK_F(", configTimeout = "), configTimeout); +#endif + } else + { configTimeout = 0; +#if ( BLYNK_WM_DEBUG > 2) + BLYNK_LOG1(BLYNK_F("s:configTimeout = 0")); +#endif + } configuration_mode = true; } diff --git a/src/BlynkSimpleTinyGSM_M.h b/src/BlynkSimpleTinyGSM_M.h index fe1a58e..baf7757 100644 --- a/src/BlynkSimpleTinyGSM_M.h +++ b/src/BlynkSimpleTinyGSM_M.h @@ -15,7 +15,7 @@ @date Oct 2016 @brief - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,8 @@ 1.0.10 K Hoang 26/08/2020 Use MultiWiFi. Auto format SPIFFS/LittleFS for first time usage. Fix bug and logic of USE_DEFAULT_CONFIG_DATA. 1.1.0 K Hoang 01/01/2021 Add support to ESP32 LittleFS. Remove possible compiler warnings. Update examples. Add MRD + 1.2.0 K Hoang 01/02/2021 Add functions to control Config Portal (CP) from software or Virtual Switches + Fix CP and Dynamic Params bugs. To permit autoreset after timeout if DRD/MRD or forced CP *****************************************************************************************************************************/ #pragma once @@ -40,7 +42,7 @@ #ifndef BlynkSimpleTinyGSM_M_h #define BlynkSimpleTinyGSM_M_h -#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.1.0" +#define BLYNK_GSM_MANAGER_VERSION "BlynkGSM_Manager v1.2.0" #if defined(ESP32)