alanswx / ESPAsyncWiFiManager

Port WiFiManager to ESP Async Server
MIT License
222 stars 85 forks source link

ESP32 reconnect when connection lost #92

Open MassiPi opened 3 years ago

MassiPi commented 3 years ago

Hello, ive some esp32cam that properly works with asyncwifimanager, so i can configure them flawlessly. But the problem is that when the wifi connection is lost:

Should i manually insert something in the loop to check if the connection is up end end in a if(!wifiManager.autoConnect()) ? Or what else? i tried setting WiFi.autoReconnect(True), but with no effect Thanks

hmronline commented 3 years ago

WiFi (or ESP8266WiFi) base library is where re-connection is handled. From what I've tested (with ESP8266), it does re-connect after a wifi connection is available again.

You can use a WiFiEventHandler to determine what to do after a wifi connection is lost. This is properly describe here: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-examples.html

Hope this helps, best regards!

MassiPi commented 3 years ago

with esp8266 i have no problems, it's eso32 that loses connection and does not reconnect..

Zimbu98 commented 3 years ago

Old thread, but for the record, the following worked for me:

I added a Wifi disconnected event in setup:

//ESP32 Event to allow change LED lights to show connection was lost
 WiFi.onEvent(Wifi_disconnected, SYSTEM_EVENT_STA_DISCONNECTED);
 WiFi.onEvent(gotIP, SYSTEM_EVENT_STA_GOT_IP);

I added a void for Wifi disconnect event and the GotIP event:

void gotIP(system_event_id_t event) {
  redTickerSetLow.detach();
  redTickerSetHigh.detach(); 
  Serial.print("Got IP event triggered. Local IP address: ");
  Serial.println(WiFi.localIP().toString());
  Serial.println("Green fast LED triggered 1");
  greenVeryFastTickerStarter(); //successful connection celebration
  delay(3000);
  greenVerySlowTickerStarter(); //ongoing connection
}
void Wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info){
    greenTickerSetLow.detach();
    greenTickerSetHigh.detach();
    redFastTickerStarter(); //lost connection warning light
    Serial.println("Wifi is disconnected event has been triggered - Flashing fast red LED for 3 seconds");
    delay(3000);
    redTickerSetLow.detach();
    redTickerSetHigh.detach();
    delay(200);
    WiFi.setAutoReconnect(true); 
}

Most importantly, you may notice above, I added the WiFi.setAutoReconnect(true) in the void Wifi_disconnected NOT in the void setup where I had originally assumed it would go. If I put it in setup void, the reconnect attempt doesn't happen. If I put it in the Wifi_disconnected void, it works.

Also note, it should be WiFi.setAutoReconnect(true), not WiFi.autoReconnect(True)

I solved this issue via my patented high-level coding skills, a.k.a. try every possible combination of random things until something works. Took me three days...

serpinio commented 2 years ago

As of Apr2022 this is how you get and process WiFi events.

tl;dr:

Use this:

void onWifiEvent(WiFiEvent_t event) {
    switch (event) {
        case WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED:
            Serial.println("Connected or reconnected to WiFi");
            break;
        case WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
            Serial.println("WiFi Disconnected. Enabling WiFi autoconnect");
            WiFi.setAutoReconnect(true);
            break;
        default: break;
  }
}

And add the listener in setup()

WiFi.onEvent(onWifiEvent);