From c24a3404c638ababb7d8ded5f2c8612ec109952a Mon Sep 17 00:00:00 2001 From: sticilface Date: Wed, 23 May 2018 09:53:01 +0100 Subject: [PATCH] Add `SYSTEM_EVENT_WIFI_READY` call back + WiFiMode fixes (#1322) * 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 * make changes compatible with new _persistent behaviour. --- libraries/WiFi/src/WiFiGeneric.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index af56e79c..9123644e 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -149,6 +149,10 @@ static bool espWiFiStart(bool persistent){ return false; } _esp_wifi_started = true; + system_event_t event; + event.event_id = SYSTEM_EVENT_WIFI_READY; + WiFiGenericClass::_eventCallback(nullptr, &event); + return true; } @@ -376,6 +380,9 @@ void WiFiGenericClass::persistent(bool persistent) */ bool WiFiGenericClass::mode(wifi_mode_t m) { + if (!_esp_wifi_started) { + wifiLowLevelInit(_persistent); + } wifi_mode_t cm = getMode(); if(cm == WIFI_MODE_MAX){ return false; @@ -383,6 +390,12 @@ bool WiFiGenericClass::mode(wifi_mode_t m) if(cm == m) { return true; } + if(m){ + espWiFiStart(_persistent); + } else { + return espWiFiStop(); + } + esp_err_t err; err = esp_wifi_set_mode(m); if(err){ @@ -403,6 +416,7 @@ wifi_mode_t WiFiGenericClass::getMode() { if(!wifiLowLevelInit(_persistent)){ return WIFI_MODE_MAX; + } uint8_t mode; esp_wifi_get_mode((wifi_mode_t*)&mode);