espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.62k stars 7.41k forks source link

WiFi Auto Reconnect Still Not Working - Is there a best practice to guarantee connection? #653

Closed vseven closed 6 years ago

vseven commented 7 years ago

Board: ModeMCU ESP32 Dev Module Core Installation/update date: 15/Sep/2017 IDE name: Arduino IDE

It seems there is no auto reconnect logic allowing the ESP32 to reconnect when the connection drops or if there is its not working properly. I have had completely random results with my ESP32 board, sometimes its up for as little as 30 minutes and at most 18 - 20 hours. WiFi is dropping and doesn't want to reconnect.

The library I am using for WiFi is watching the WiFi event and on a disconnect its trying to reconnect which helps but sometimes it fails the reconnect and then that's it...I have to reboot. There is no sleep/low power mode at all involved and the device is constantly powered by 3.3v in. There was a case opened regarding this, https://github.com/espressif/arduino-esp32/issues/353, but its closed without any real answer (says auto connect is kind of implemented...not sure what that means).

I guess the question comes down to is auto reconnect implemented in the code itself, reliably, and if not what is the best Arduino code way of making sure it is connected?

-Allan

Anton2k commented 3 years ago

Hey, don't mean to bring up an old issue, but currently on my esp32 board Lilygo TTGO T5, if I kill power to it whilst it is connected to the network then power it back up, it will not auto reconnect unless I power it off and on again, it will connect on the second attempt. If I add a delay of 35 seconds before the wifi connection portion it always reconnects. If I do (wifi.disconnect();) to kill the wifi cleanly once I an done using the wifi then kill the power it reconnects fine each time. The delay is a work around but not ideal, would be good to get to the root cause of this issues, is it a access point thing it an esp32 thing?

See here for more detailed info https://github.com/Hieromon/AutoConnect/issues/292

mickeypop commented 3 years ago

@Anton2k if you look at my earlier post( UPDATE) and look for: "complete SmartConfig / WiFi skeleton" on link below in wifiInit(); i have a 3 sec delay as in my earlier post, DHCP takes time to reset and know a bit about the WiFi init process.

When connecting the ESP32 will request the same IP if possible.
Since the DHCP server has not reset yet it will return that IP is taken and fail connect. This is a library issue. On successive reboot the DHCP server has caught up and all goes well.

your delay is a well founded solution

this WiFi skeleton code has served me well for over years thru several processors. [(https://github.com/espressif/arduino-esp32/issues/1100 )]

Anton2k commented 3 years ago

@mickeypop Thanks again for your suggestion, I have had a deeper look into the lib I am using and the auto reconnect time out is set at 30 seconds, mine needs 35, so by changing the time out figure I should be good! thanks again for your help.

One little thing that is still niggling me, if power of the ESP32 and power it back on and then off again and back on (both in quick succession, within 2 or 3 seconds of each other) it will always reconnect on the second attempt regardless of how long I have waiting, again I will really need to test this again to make sure I remember it correctly, but I am fairly certain. Do you think it's possible that if the ESP32 makes a connection attempt soon after another that the router might be auto releasing the DHCP ip?

Thanks

asepms1211 commented 3 years ago

@Anton2k

Hey, don't mean to bring up an old issue, but currently on my esp32 board Lilygo TTGO T5, if I kill power to it whilst it is connected to the network then power it back up, it will not auto reconnect unless I power it off and on again, it will connect on the second attempt. If I add a delay of 35 seconds before the wifi connection portion it always reconnects. If I do (wifi.disconnect();) to kill the wifi cleanly once I an done using the wifi then kill the power it reconnects fine each time. The delay is a work around but not ideal, would be good to get to the root cause of this issues, is it a access point thing it an esp32 thing?

See here for more detailed info Hieromon/AutoConnect#292

thank you thank you thank you very much, I've been looking for this solution for a week ... and now you and this page have helped me ;)

35 second :)

Anton2k commented 3 years ago

@Anton2k

Hey, don't mean to bring up an old issue, but currently on my esp32 board Lilygo TTGO T5, if I kill power to it whilst it is connected to the network then power it back up, it will not auto reconnect unless I power it off and on again, it will connect on the second attempt. If I add a delay of 35 seconds before the wifi connection portion it always reconnects. If I do (wifi.disconnect();) to kill the wifi cleanly once I an done using the wifi then kill the power it reconnects fine each time. The delay is a work around but not ideal, would be good to get to the root cause of this issues, is it a access point thing it an esp32 thing? See here for more detailed info Hieromon/AutoConnect#292

thank you thank you thank you very much, I've been looking for this solution for a week ... and now you and this page have helped me ;)

35 second :)

Very welcome and glad it helped someone! :)

tablatronix commented 1 year ago

The every second connect is typically a deauth protection failure, you need to get debugging wifi failure event codes.

DmitriyButkevich commented 8 months ago

I have the same problem with the inability to reconnect to the access point. device: ESP-WROOM-32 library version: 1.2.7

Symptoms:

Disconnected from WiFi access point
WiFi lost connection. Reason: 200

Followed by endless reconnection. RSSI is good (-37 or nearly)

Solutions that didn't help: 1.Wifi.reconnect()

  1. Wifi.disconnect(), Wifi.reconnect()
  2. Wifi.disconnect(), Wifi.begin(ssid, pass)
  3. Wifi.disconnect(), Wifi.mode(WIFI_OFF), Wifi.mode(WIFI_STA), Wifi.begin(ssid, pass) for cases 1-4 - endless reconnection
  4. WiFi.setAutoReconnect(true) - for this case, debugging shows only a reconnection attempt, where zero addresses were received and subsequent system inaction

solution that works:

I hope it will be useful to someone