From f7998c2ad6305b1adf3ca69bc4c645352e3a887f Mon Sep 17 00:00:00 2001
From: Gunar Schorcht <gunar@schorcht.net>
Date: Sun, 20 Jan 2019 11:14:19 +0100
Subject: [PATCH] cpu/esp8266: automatic reconnect in esp_wifi

---
 cpu/esp8266/esp-wifi/esp_wifi_netdev.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/cpu/esp8266/esp-wifi/esp_wifi_netdev.c b/cpu/esp8266/esp-wifi/esp_wifi_netdev.c
index 67dcc43a37..7ffe51695f 100644
--- a/cpu/esp8266/esp-wifi/esp_wifi_netdev.c
+++ b/cpu/esp8266/esp-wifi/esp_wifi_netdev.c
@@ -194,6 +194,20 @@ static void _esp_wifi_handle_event_cb(System_Event_t *evt)
                               evt->event_info.disconnected.ssid,
                               evt->event_info.disconnected.reason);
             _esp_wifi_dev.connected = false;
+
+            /* call disconnect to reset internal state */
+            if (!wifi_station_disconnect()) {
+                ESP_WIFI_LOG_ERROR("could not disconnect from to AP %s",
+                                   ESP_WIFI_SSID);
+                return;
+            }
+
+            /* try to reconnect */
+            if (!wifi_station_connect()) {
+                ESP_WIFI_LOG_ERROR("could not start connection to AP %s",
+                                   ESP_WIFI_SSID);
+                return;
+            }
             break;
 
         default:
@@ -523,7 +537,7 @@ static void _esp_wifi_setup(void)
 
     /* connect */
     if (!wifi_station_connect()) {
-        ESP_WIFI_DEBUG("could not start connection to AP %s", ESP_WIFI_SSID);
+        ESP_WIFI_LOG_ERROR("could not start connection to AP %s", ESP_WIFI_SSID);
         return;
     }
 
-- 
GitLab