khoih-prog / ESPAsync_WiFiManager

This is an ESP32 (including ESP32-S2 and ESP32-C3) / ESP8266 WiFi Connection Manager, using ESPAsyncWebServer, with fallback web configuration portal. Use this library for configuring ESP32, ESP8266 modules' WiFi, etc. Credentials at runtime. You can also specify static DNS servers, personalized HostName, fixed or random AP WiFi channel. With examples supporting ArduinoJson 6.0.0+ as well as 5.13.5- . Using AsyncDNSServer instead of DNSServer now.
MIT License
290 stars 73 forks source link

Update ESPAsync_WiFiManager-Impl.h #82

Closed AlexMarcov closed 2 years ago

AlexMarcov commented 2 years ago

added Reset ESP after timeout (120sec) in config portal if there are stored Credentials

khoih-prog commented 2 years ago

Hi @AlexMarcov

Thanks for your interest in the library and your contribution.

I'm sorry I don't think the PR is necessary because as design, whenever you entered the Config Portal (CP), it must stay there forever until user intentionally exits the CP by exiting or reset manually, no matter what.

If reset is necessary, users can write it in the users' code, not to force the behaviour on everybody by changing the library.

Moreover, it'll be beneficial next time for you PR to pay attention to these following details

  1. The library is written for both ESP32 and ESP8266, so it's better to use
#if ESP8266      
    ESP.reset();
#else
    ESP.restart();
#endif

instead of only for ESP32

ESP.restart(); // Exit loop and restart ESP try to reconect to stored configs
  1. Using the same style for debugging messages, for example to use
LOGINFO("Restart");

instead of

Serial.println("Restart"); 
  1. Are you sure your calculation is OK???
if ( ((millis() - (_configPortalStart + _configPortalTimeout)) >= _configPortalTimeout - 2)
          && ((millis() - (_configPortalStart + _configPortalTimeout)) >= _configPortalTimeout - 1)) 
{ 
    Serial.println("Restart"); 
    delay(2000); 
    ESP.restart(); // Exit loop and restart ESP try to reconect to stored configs
}

Anyway, you seem to be very good and interested in coding and have read thru the library's code. I expect you have more contributions in the future.

Best Regards,

AlexMarcov commented 2 years ago

Thank you for your work on the library!

The creation of this PR was pushed by the fact that I use the Double Reset Detector option to enter the CP. Sometimes a situation occurs when the device is powered on or power reset several times for various reasons. Something like a bounce of contacts when the power jack is connected, this event is recognized as a signal to enter CP. It really becomes an infinite loop, only a reset helps if you are there to click on it. In this case, I would really like the CP timeout to work, so I added the following lines.

(1, 2) I understand that I did not quite correctly design these small parts of the code, this is my first PR ) But this code solve my problem, i tested it on ESP8266, it works, I have not yet checked for ESP32.

(3) Yes, calculations are correct, tested on ESP8266

I can rewrite code as you suggest, and make it as one of the library settings that can be turned on or off if user desired. How do you think it makes sense for someone else or it is just my unique case? Or maybe you have another solution to my problem.

As I see it, the library was planned to be able to connect in parallel with CP to the saved AP Credentials when the specified timeout is reached, but for some reason this does not work, as i understand this part of code is unfinished. I was not able to fully figure out what the problem was, until now I just found out that the wifi scan works in parallel with CP, but the connection does not occur when timeout is reached. I will dig further.

Thank you Best Regards,

khoih-prog commented 2 years ago

Something like a bounce of contacts when the power jack is connected, this event is recognized as a signal to enter CP

If this is the case, either

  1. try to shorten the DRD_TIMEOUT and check which value is working for your use case
#define DRD_TIMEOUT             3

or

  1. Use ESP_MultiResetDetector library and set and check which values ar working for your use case. For example
#define MRD_TIMES                     3
#define MRD_TIMEOUT                   10
  1. You can also modify the example code to disable the stay-forever feature when DRD or MRD is detected, such as replacing Async_ConfigOnDoubleReset.ino#L848

with

// CP now timeout is 120s. Change as necessary
ESPAsync_wifiManager.setConfigPortalTimeout(120);

in

if (drd->detectDoubleReset())
{
  // DRD, disable timeout.
  ESPAsync_wifiManager.setConfigPortalTimeout(0);

  Serial.println(F("Open Config Portal without Timeout: Double Reset Detected"));
  initialConfig = true;
}

Please spend more time to test and use the designed features before adding more to already complex ones.