diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index 3889f4dff49ac3135520607c37d8ebb9afb09308..1b9b2b7daa1fd3e313cb911bd4664fbf395bf7e1 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -8,15 +8,11 @@ extern "C" void initArduino(); extern void loop(); extern void setup(); -void startWiFi() __attribute__((weak)); -void startWiFi() {} - void loopTask(void *pvParameters) { bool setup_done = false; for(;;) { if(!setup_done) { - startWiFi(); setup(); setup_done = true; } diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index adbc1d46cfdbf90d4e1870bef1d38fb48ef00c37..2fde8e4fad414ee539eec042da1e9067ee6c22d7 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -40,14 +40,75 @@ extern "C" { #include "lwip/opt.h" #include "lwip/err.h" #include "lwip/dns.h" +#include "esp_ipc.h" #include "esp32-hal-log.h" + +/** + * Boot and start WiFi + * This method get's called on boot if you use any of the WiFi methods. + * If you do not link to this library, WiFi will not be started. + * */ +static bool _esp_wifi_initalized = false; +extern void initWiFi() +{ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + tcpip_adapter_init(); + esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL); + esp_wifi_init(&cfg); + esp_wifi_set_storage(WIFI_STORAGE_RAM); + _esp_wifi_initalized = true; } +} //extern "C" + #undef min #undef max #include +static bool _esp_wifi_start() +{ + static bool started = false; + esp_err_t err; + + if(!_esp_wifi_initalized){ + initWiFi(); + if(!_esp_wifi_initalized){ + log_w("not initialized"); + return false; + } + } + if(started){ + return true; + } + started = true; + err = esp_wifi_start(); + if (err != ESP_OK) { + log_e("%d", err); + return false; + } +#if CONFIG_AUTOCONNECT_WIFI + wifi_mode_t mode = WIFI_MODE_NULL; + bool auto_connect = false; + + err = esp_wifi_get_mode(&mode); + if (err != ESP_OK) { + log_e("esp_wifi_get_mode: %d", err); + return false; + } + + err = esp_wifi_get_auto_connect(&auto_connect); + if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) { + err = esp_wifi_connect(); + if (err != ESP_OK) { + log_e("esp_wifi_connect: %d", err); + return false; + } + } +#endif + return true; +} + // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- @@ -167,7 +228,11 @@ void WiFiGenericClass::persistent(bool persistent) */ bool WiFiGenericClass::mode(wifi_mode_t m) { - if(getMode() == m) { + wifi_mode_t cm = getMode(); + if(cm == WIFI_MODE_MAX){ + return false; + } + if(cm == m) { return true; } return esp_wifi_set_mode(m) == ESP_OK; @@ -180,6 +245,9 @@ bool WiFiGenericClass::mode(wifi_mode_t m) wifi_mode_t WiFiGenericClass::getMode() { uint8_t mode; + if(!_esp_wifi_start()){ + return WIFI_MODE_MAX; + } esp_wifi_get_mode((wifi_mode_t*)&mode); return (wifi_mode_t)mode; } @@ -275,48 +343,3 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca _dns_busy = false; } -/** - * Boot and start WiFi - * This method get's called on boot if you use any of the WiFi methods. - * If you do not link to this library, WiFi will not be started. - * */ -#include "nvs_flash.h" - -extern "C" void initWiFi() -{ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - nvs_flash_init(); - tcpip_adapter_init(); - esp_event_loop_init(WiFiGenericClass::_eventCallback, NULL); - esp_wifi_init(&cfg); -} - -void startWiFi() -{ - esp_err_t err; - - err = esp_wifi_start(); - if (err != ESP_OK) { - log_e("esp_wifi_start: %d", err); - return; - } -#if CONFIG_AUTOCONNECT_WIFI - wifi_mode_t mode = WIFI_MODE_NULL; - bool auto_connect = false; - - err = esp_wifi_get_mode(&mode); - if (err != ESP_OK) { - log_e("esp_wifi_get_mode: %d", err); - return; - } - - err = esp_wifi_get_auto_connect(&auto_connect); - if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) { - err = esp_wifi_connect(); - if (err != ESP_OK) { - log_e("esp_wifi_connect: %d", err); - } - } -#endif -} -