Good new feature: Blynk.resetAndEnterConfigPortal() Thanks & question
thorathome opened this issue · comments
Hi @khoih-prog
Nice work adding this feature. I now use it and find it handy. I've set up a (hidden) Blynk button widget that, when pushed, forces the reset and Config Portal. Much appreciated.
Questions you may have already answered.
How do I set the time-out for the Config Portal, whether it was triggered by DRD or by this new feature, so it automatically turns off and reboots the ESP? I am trying to avoid any physical resetting of the device.
I see this code in your doc and examples, not sure how to get it to do what I want it to do.
Kindly explain when you have a moment.
// Force some params in Blynk, only valid for library version 1.0.1 and later
#define TIMEOUT_RECONNECT_WIFI 10000L
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5
Best wishes. Thanks again.
@thorathome
I'm glad that the new feature is working for you. I've updated that soft-CP-forcing
feature into several libraries already. Thanks for your always good-and-novel ideas I couldn't think of without real use cases.
The design for CP, I remember, has no timeout. But depending on what is the real cause, you can write some simple code using timer and the resetFunc() to take care of the auto-reboot. For example, after the Virtual SW is pressed, you start an auto-resetting timer, then call resetAndEnterConfigPortal().
resetFunction() can use ESP.reset() for ESP8266 or ESP.restart() for ESP32
Depending on which is the cause of CP:
- MRD/DRD: the CP of MRD/DRD is not persistent. The system will run normally after being reset.
- Non-persistent soft-CP: The system will run normally after being reset.
- Persistent soft-CP: The system still requests to enter CP and save before clearing this situation (persistent). It's not cleared even after being reset. It's better that you add certain password or hide this button, if used, to avoid complications.
Hope to receive more issues, leading to new ideas, from you,
Good Luck,
BR
Thanks @khoih-prog for taking the time on this.
I think resetAndEnterConfigPortal()
ALWAYS causes a reboot.
Where can I set a timer? How do I know on reboot that the CP is up?
Thanks.
You can write a flag to LittleFS, SPIFFS or EEPROM, then after reboot, check that flag to start a rebooting timer.
#define BLYNK_PIN_FORCED_CONFIG V10
// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced non-persistent Config Portal
BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG)
{
if (param.asInt())
{
Serial.println( F("\nCP Button Hit. Rebooting") );
// set Forced_CP_Timeout flag, and save into FS or EEPROM
////
// This will keep CP once, clear after reset, even you didn't enter CP at all.
Blynk.resetAndEnterConfigPortal();
}
}
...
void setup()
{
...
// read Forced_CP_Timeout flag (from DRD, soft CP, etc.)
// If flag == true => start a timer to reset after predetermined time
...
}
Later, I'll think about introducing this feature (thanks to you again) into the libraries (already reserved data for it)
- resetAndEnterConfigPortalWithTimeout(uint32_t timeOut)
- DRDWithTimeout(uint32_t timeOut)
What else do you suggest?
Oh, you're right. I forget we have this feature
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 10
Try test and use this to see if OK for you.
I have tested the following code inserted before the #include <BlynkSimpleEsp8266_SSL_WM.h>
statement, and I have tested with these three lines after the #include <BlynkSimpleEsp8266_SSL_WM.h>
statement.
#define TIMEOUT_RECONNECT_WIFI 3000L // originally 10000L in your doc
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 2 // originally 5 in your doc
How were these three lines supposed to work?
Either way, the new resetAndEnterConfigPortalWithTimeout()
command works perfectly, the ESP reboots, and the Config Portal comes up as expected.
Even with the three lines, above, the Config Portal stays lit for a long time unless I access the CP and SAVE it.
I understand your suggestion of setting a flag in persistent memory to pick up on reboot, was trying to remember if this feature was already built into the library. If not, I'll set the LittleFS flag and read it. Should work fine.
I have tested the following code inserted before the #include <BlynkSimpleEsp8266_SSL_WM.h> statement, and I have tested with these three lines after the #include <BlynkSimpleEsp8266_SSL_WM.h> statement.
#define TIMEOUT_RECONNECT_WIFI 3000L // originally 10000L in your doc
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 2 // originally 5 in your doc
To be considered, those lines must be before all the #include <BlynkSimpleEsp????.h> statements, not after.
Currently, they are already in the defines.h
examples as
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5
The operation is as follows:
- If you already have valid data (config / dynamicParams), the CP has a hardcoded timeout of 60s as defined in
#define CONFIG_TIMEOUT 60000L
// If there is no saved config Data, stay in config mode forever until having config Data.
// or SSID, PW, Server,Token ="nothing"
if (hadConfigData)
configTimeout = millis() + CONFIG_TIMEOUT;
else
configTimeout = 0;
if you've been in CP for more than (CONFIG_TIMEOUT * CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) = (60s * 5) = 300s, the ESP will reset itself. If the forced CP is non-persistent or DRD/MRD, the reset will return the ESP to normal operation.
It's good that this feature, designed from long time ago without good usage, now can be used for this purpose.
- If you accidentally press persistent-CP button or there is no Config Data or dynamicParams, the CP will stay forever, until you enter the CP and save the valid data.
So please try to see if the RESET_IF_CONFIG_TIMEOUT
is working OK for you. Quite a long time since I re-read the code, and forget lots of features without your reminder.
You're great, @khoih-prog
Thanks for spending time on this. This is NOT urgent.
OK, I am following your doc and suggestions.
On the call to Blynk.resetAndEnterConfigPortal();
, it works. The ESP resets and the CP goes live. I can connect to it and save parameters. If I do not connect to or save the CP, it appears the CP stays on past the CONFIG_TIMEOUT, does not shut off automatically following a Blynk.resetAndEnterConfigPortal();
.
Here is the setup code. I am on an ESP8266 with #define USE_LITTLEFS true
set BEFORE this code.
// Force some params in Blynk, only valid for library version 1.0.1 and later
#define TIMEOUT_RECONNECT_WIFI 1000L
#define RESET_IF_CONFIG_TIMEOUT true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 2
// AUTOMATIC Blynk_WiFiManager LIBRARY SELECTION
// This is BLYNK_WiFiManager library selection for ESP32 or 8266
#ifdef ESP8266
#include <BlynkSimpleEsp8266_SSL_WM.h>
#endif
#ifdef ESP32
#include <BlynkSimpleEsp32_SSL_WM.h>
#endif
Here is the reset call
Blynk.resetAndEnterConfigPortal();
Any thoughts?
Thank you.
OK now, just change a line of code as this new feature is not in my mind in the original design.
Will post to master within 10 mins.
You are too much!
Of course, I will try your updated library immediately. Amazing!
Here is how I interpret your parameters
#define TIMEOUT_RECONNECT_WIFI 1000L // Microseconds the Config Portal stays up before turning off (or restarting)
#define RESET_IF_CONFIG_TIMEOUT true // On Config Portal TIMEOUT, reset the ESP if true.
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 2 // Not sure what this does.
Kindly correct me if I have any (or all!) of these wrong. Thanks again.
And please signal me when the update is usable. Of course, I'll try it out.
New release v.1.1.3 thanks to your contribution
Releases v1.1.3
- To permit autoreset after configurable timeout if DRD/MRD or non-persistent forced-CP. Check Good new feature: Blynk.resetAndEnterConfigPortal() Thanks & question #27
--
Test result OK
Starting ESP8266WM_ForcedConfig using LittleFS without SSL on ESP8266_NODEMCU
Blynk_WM for ESP8266 v1.1.3
ESP_DoubleResetDetector v1.1.1
[297] Multi or Double Reset Detected
[297] Hostname=8266-Master-Controller
[330] LoadCfgFile
[330] OK
[331] ======= Start Stored Config Data =======
[331] Hdr=ESP8266,BrdName=ESP8266_ForcedCP
[331] SSID=HueNet1,PW=12345678
[333] SSID1=HueNet2,PW1=12345678
[335] Server=account.duckdns.org,Token=token
[342] Server1=account.duckdns.org,Token1=token1
[348] Port=8080
[349] ======= End Config Data =======
[353] CCSum=0x3680,RCSum=0x3680
[362] LoadCredFile
[363] CrR:pdata=new-mqtt-server,len=34
[363] CrR:pdata=1883,len=6
[363] CrR:pdata=new-mqtt-username,len=34
[367] CrR:pdata=new-mqtt-password,len=34
[371] CrR:pdata=new-mqtt-SubTopic,len=34
[374] CrR:pdata=new-mqtt-PubTopic,len=34
[378] OK
[379] CrCCsum=0x219f,CrRCsum=0x219f
[382] Valid Stored Dynamic Data
[385] Hdr=ESP8266,BrdName=ESP8266_ForcedCP
[389] SSID=HueNet1,PW=12345678
[392] SSID1=HueNet2,PW1=12345678
[394] Server=account.duckdns.org,Token=token
[401] Server1=account.duckdns.org,Token1=token1
[407] Port=8080
[408] ======= End Config Data =======
[412] Check if isForcedCP
[419] LoadCPFile
[419] OK
[419] bg: isForcedConfigPortal = true
[420] bg:Stay forever in CP:Forced-non-Persistent
[424] clearForcedCP
[435] SaveCPFile
[442] OK
[452] SaveBkUpCPFile
[460] OK
[2512]
stConf:SSID=TestPortal-ESP8266,PW=TestPortalPass
[2512] IP=192.168.200.1,ch=10
[2614] s:millis() = 2614, configTimeout = 62614
F
Your stored Credentials :
MQTT Server = new-mqtt-server
Port = 1883
MQTT UserName = new-mqtt-username
MQTT PWD = new-mqtt-password
Subs Topics = new-mqtt-SubTopic
Pubs Topics = new-mqtt-PubTopic
FFFFFRF
RF[129887] r:Check RESET_IF_CONFIG_TIMEOUT
[129887] run: WiFi lost, configTimeout. Connect WiFi+Blynk. Retry#:3
[129887] run: WiFi lost. Reconnect WiFi+Blynk
[129889] Connecting MultiWifi...
[163457] WiFi not connected
F [163458] r:Check RESET_IF_CONFIG_TIMEOUT
[163458] run: WiFi lost, configTimeout. Connect WiFi+Blynk. Retry#:4
[163459] run: WiFi lost. Reconnect WiFi+Blynk
[163463] Connecting MultiWifi...
[196939] WiFi not connected
RF[197015] r:Check RESET_IF_CONFIG_TIMEOUT
[197015] run: WiFi lost, configTimeout. Connect WiFi+Blynk. Retry#:5
[197015] run: WiFi lost. Reconnect WiFi+Blynk
[197018] Connecting MultiWifi...
[230593] WiFi not connected
F[230594] r:Check RESET_IF_CONFIG_TIMEOUT
ets Jan 8 2013,rst cause:2, boot mode:(3,6) <===== auto reset
Starting ESP8266WM_ForcedConfig using LittleFS without SSL on ESP8266_NODEMCU
Blynk_WM for ESP8266 v1.1.3
ESP_DoubleResetDetector v1.1.1
[295] Hostname=8266-Master-Controller
[329] LoadCfgFile
[329] OK
[329] ======= Start Stored Config Data =======
[329] Hdr=ESP8266,BrdName=ESP8266_ForcedCP
[329] SSID=HueNet1,PW=12345678
[331] SSID1=HueNet2,PW1=12345678
[334] Server=account.duckdns.org,Token=token
[340] Server1=account.duckdns.org,Token1=token1
[346] Port=8080
[348] ======= End Config Data =======
[351] CCSum=0x3680,RCSum=0x3680
[361] LoadCredFile
[361] CrR:pdata=new-mqtt-server,len=34
[361] CrR:pdata=1883,len=6
[362] CrR:pdata=new-mqtt-username,len=34
[366] CrR:pdata=new-mqtt-password,len=34
[369] CrR:pdata=new-mqtt-SubTopic,len=34
[373] CrR:pdata=new-mqtt-PubTopic,len=34
[376] OK
[377] CrCCsum=0x219f,CrRCsum=0x219f
[381] Valid Stored Dynamic Data
[383] Hdr=ESP8266,BrdName=ESP8266_ForcedCP
[387] SSID=HueNet1,PW=12345678
[390] SSID1=HueNet2,PW1=12345678
[393] Server=account.duckdns.org,Token=token
[399] Server1=account.duckdns.org,Token1=token1
[405] Port=8080
[407] ======= End Config Data =======
[410] Check if isForcedCP
[417] LoadCPFile
[417] OK
[417] bg: noConfigPortal = true
[418] Connecting MultiWifi...
[6677] WiFi connected after time: 1
[6677] SSID=HueNet1,RSSI=-46
[6677] Channel=2,IP=192.168.2.87
[6677] bg: WiFi OK. Try Blynk
[6677]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU
[6690] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080
[6799] Ready (ping: 7ms).
[6866] Connected to BlynkServer=account.duckdns.org,Token=token
[6866] bg: WiFi+Blynk OK
Blynk ESP8288 using LittleFS connected.
Board Name : ESP8266_ForcedCP
B
Your stored Credentials :
MQTT Server = new-mqtt-server
Port = 1883
MQTT UserName = new-mqtt-username
MQTT PWD = new-mqtt-password
Subs Topics = new-mqtt-SubTopic
Pubs Topics = new-mqtt-PubTopic
BBBBBRBBBB BBRBBB