From df5e82b43fc56e4868df767faf14eb41b5cff464 Mon Sep 17 00:00:00 2001 From: sticilface Date: Mon, 16 Apr 2018 16:05:52 +0100 Subject: [PATCH 1/2] Add `SYSTEM_EVENT_WIFI_READY` call back once wifi service is init. allows you to hook in, as the sdk does not generate this event for you. As it stands the SDK does not appear to set `WIFI_MODE_NULL` correctly. if the wifi is initialised and set to `WIFI_MODE_NULL` it actually defaults to AP mode. This fix keeps `WIFI_MODE_NULL` within the ESP class if the wifi has not been init yet, and works in my testing. albeit a one sided conversation. https://github.com/espressif/arduino-esp32/issues/1306 --- libraries/WiFi/src/WiFiGeneric.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index 3ba6aae6814..4b93f356fd4 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -147,6 +147,10 @@ static bool espWiFiStart(){ return false; } _esp_wifi_started = true; + system_event_t event; + event.event_id = SYSTEM_EVENT_WIFI_READY; + WiFiGenericClass::_eventCallback(nullptr, &event); + return true; } @@ -369,6 +373,9 @@ void WiFiGenericClass::persistent(bool persistent) */ bool WiFiGenericClass::mode(wifi_mode_t m) { + if (!_esp_wifi_started) { + wifiLowLevelInit(); + } wifi_mode_t cm = getMode(); if(cm == WIFI_MODE_MAX){ return false; @@ -376,16 +383,20 @@ bool WiFiGenericClass::mode(wifi_mode_t m) if(cm == m) { return true; } + if(m){ + espWiFiStart(); + } else { + return espWiFiStop(); + } + esp_err_t err; err = esp_wifi_set_mode(m); if(err){ log_e("Could not set mode! %u", err); return false; } - if(m){ - return espWiFiStart(); - } - return espWiFiStop(); + return true; + } /** @@ -394,8 +405,8 @@ bool WiFiGenericClass::mode(wifi_mode_t m) */ wifi_mode_t WiFiGenericClass::getMode() { - if(!wifiLowLevelInit()){ - return WIFI_MODE_MAX; + if(!_esp_wifi_started){ + return WIFI_MODE_NULL; } uint8_t mode; esp_wifi_get_mode((wifi_mode_t*)&mode); From 6170e8213dbb4b8caada5b1969af9a11cd25fe8b Mon Sep 17 00:00:00 2001 From: sticilface Date: Tue, 22 May 2018 18:44:14 +0100 Subject: [PATCH 2/2] make changes compatible with new _persistent behaviour. --- libraries/WiFi/src/WiFiGeneric.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index 022a005f1ec..9123644e8bf 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -381,7 +381,7 @@ void WiFiGenericClass::persistent(bool persistent) bool WiFiGenericClass::mode(wifi_mode_t m) { if (!_esp_wifi_started) { - wifiLowLevelInit(); + wifiLowLevelInit(_persistent); } wifi_mode_t cm = getMode(); if(cm == WIFI_MODE_MAX){ @@ -391,7 +391,7 @@ bool WiFiGenericClass::mode(wifi_mode_t m) return true; } if(m){ - espWiFiStart(); + espWiFiStart(_persistent); } else { return espWiFiStop(); }