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
291 stars 73 forks source link

ConfigOnSwitch example flawed? #18

Closed marcelstoer closed 3 years ago

marcelstoer commented 3 years ago

I think your ConfigOnSwitch example might be flawed but I cannot be exactly sure (yet). The issue is that if the device cannot connect to one of the configured WiFi networks it keeps looping ignoring that pressed button to start the config portal.

I took your example and simplified it for my needs:

-> see Gist https://gist.github.com/marcelstoer/914ff2ac58a42a957dfa071c9c5c8acd -> use as below

void setup() {
  Serial.begin(115200);

  initWifiManager(String(deviceId));
  log_i("WiFi manager initialized.");
}

void loop() {
  // check button status here (to see if to start config portal)

  // internally checks WiFi connectivity in an interval i.e. not necessarily every time you call
  // this function
  checkWifiStatus();

  // app functions go here
}

As far as I understand the problem is the while-loop in connectMultiWiFi() at https://github.com/khoih-prog/ESPAsync_WiFiManager/blob/master/examples/Async_ConfigOnSwitch/Async_ConfigOnSwitch.ino#L388.

The function sequence is like this: loop() -> check_status() -> check_WiFi() -> connectMultiWiFi(). While trying to connect to one of the WiFis in that while-loop pressing the button to start the config portal has no effect! You would have to press the button for a really long time until the loop ends, the sequence above returns to loop() and the button status is evaluated again.

khoih-prog commented 3 years ago

Thanks again for your interest in the library and your nice and clean code. The library's examples are normally over-crowded with too many definitions trying to demonstrate to as many use cases as possible.

The Async_ConfigOnSwitch example is just a simple example to demonstrate the ESPAsync_WiFiManager library's feature and certainly can not be expected to be perfect in all use cases.

To make sure to enter the Config Portal whenever a button is pressed, it's suggested you use either

  1. Interrupt for the button to change a flag, then use the flag to exit the WiFi while loop when the button is pressed.
  2. Async_ConfigOnDoubleReset example

Please check these examples

  1. ISR_Timer_4_Switches
  2. ISR_Switch
  3. SwitchDebounce

for how to use Button Interrupt in ESP8266/ESP32.