JohnySeven / TWatchSK

TTGO T-Watch 2020 v1 supporting Signal K features for displaying boat data and controlling devices on board.
MIT License
9 stars 1 forks source link

Add periodic retries if watch disconnects from wifi #56

Closed ba58smith closed 3 years ago

ba58smith commented 3 years ago

If wifi is enabled on the watch (that is, if the watch is supposed to connect to wifi), and it can't for any reason - or if it does connect, but then loses the connection - it should try to reconnect periodically. Maybe once a minute, or if that will consume too much battery power, once every 3 or 5 minutes.

In fact, maybe it should be a user-configurable period, because the reason that the watch might disconnect from wifi will be different for different users. If the user doesn't want it to try to reconnect for some reason, they can set the period to 0.

JohnySeven commented 3 years ago

Good point - if I would be sleeping and my watch gets disconnected and want to reconnect if Wi-Fi is enabled. I would do it only after success connection with limited number of attempts? And every attempt will raise the reconnect time to twice? So we start with 10 second reconnect, if that fails, we'll do it in 20 sec, 40 sec, 80 sec and then we give up? Let me know what you think.

Good place to add reconnection functions is around line 36 in Wifi.cpp there is already determination if disconnection is in connecting phase or Wifi is already connected.

Let me know :-)

JohnySeven commented 3 years ago

Here is nice paragraph from ESP-IDF docs: WIFI_EVENT_STA_DISCONNECTED ... The most common event handle code for this event in application is to call esp_wifi_connect() to reconnect the Wi-Fi. However, if the event is raised because esp_wifi_disconnect() is called, the application should not call esp_wifi_connect() to reconnect. It’s application’s responsibility to distinguish whether the event is caused by esp_wifi_disconnect() or other reasons. Sometimes a better reconnect strategy is required, refer to Wi-Fi Reconnect and Scan When Wi-Fi Is Connecting. Link: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wifi-event-sta-disconnected

ba58smith commented 3 years ago

Because my main "house" batteries are failing, I turn off as much as I can when I go to bed, and that includes the router, switch, and access points. In the morning, I turn them back on again (as the solar panels have started charging the batteries again). But some people might turn off their wifi overnight, for whatever reason, and turn it back on in the morning. So I was thinking that the wifi_reconnect() should never completely give up, but rather, go into a much longer delay for the retry attempts.

I can see the logic of the 10, 20, 40, 80 second retries, but even after that, I would still want to keep retrying, until the battery dies or we reconnect. The reason is that the watch isn't very valuable if it's not connected to wifi - it's good only for telling time.

Let me know :-)

JohnySeven commented 3 years ago

Yes, you're right, let's double the time and do it until battery dies. And set some interval maximum? 180 seconds? 3 minutes that would be OK I think 🤔

ba58smith commented 3 years ago

OK, I'll set the max interval at 180 seconds, and do some battery testing - see how long it lasts when it's trying to reconnect every 3 minutes.

ba58smith commented 3 years ago

Basic wifi reconnection is now working - see PR #57 . Still want to add some related functionality, but I will put that in a new issue.