Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 reconnect when connection lost #92

Open
MassiPi opened this issue Aug 6, 2021 · 4 comments
Open

ESP32 reconnect when connection lost #92

MassiPi opened this issue Aug 6, 2021 · 4 comments

Comments

@MassiPi
Copy link

MassiPi commented Aug 6, 2021

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:

  • it is not restored even if the wifi comes back
  • the captive portal is not started

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
Copy link
Contributor

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
Copy link
Author

MassiPi commented Aug 7, 2021

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

@Zimbu98
Copy link

Zimbu98 commented Oct 19, 2021

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
Copy link

serpinio commented Apr 29, 2022

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants