From 0e041d7e8906a4e027d4e0acbc0653e3870c317d Mon Sep 17 00:00:00 2001 From: Reza Asghari Date: Mon, 29 Aug 2022 22:30:45 +0430 Subject: [PATCH] for repair get error : No Credentials are Saved, skipping connect on esp32 --- CMakeLists.txt | 8 ++++++ WiFiManager.cpp | 73 +++++++++++++++++++++++++++++++------------------ WiFiManager.h | 2 +- component.mk | 2 ++ 4 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 component.mk diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..9a5f046fc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +set(WiFiManager +WiFiManager.cpp +) + +set(srcs ${WiFiManager}) +# if library is used in library mus be add in requires +# idf_component_register(SRCS ${srcs} INCLUDE_DIRS src REQUIRES arduino ArduinoJson PageBuilder spiffs app_update ) +idf_component_register(SRCS ${srcs} INCLUDE_DIRS . REQUIRES arduino ) \ No newline at end of file diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 6f699fa84..f4fb82e08 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -3628,35 +3628,56 @@ bool WiFiManager::WiFi_hasAutoConnect(){ return WiFi_SSID(true) != ""; } -String WiFiManager::WiFi_SSID(bool persistent) const{ - - #ifdef ESP8266 - struct station_config conf; - if(persistent) wifi_station_get_config_default(&conf); - else wifi_station_get_config(&conf); - - char tmp[33]; //ssid can be up to 32chars, => plus null term - memcpy(tmp, conf.ssid, sizeof(conf.ssid)); - tmp[32] = 0; //nullterm in case of 32 char ssid - return String(reinterpret_cast(tmp)); - - #elif defined(ESP32) - if(persistent){ - wifi_config_t conf; - esp_wifi_get_config(WIFI_IF_STA, &conf); - return String(reinterpret_cast(conf.sta.ssid)); - } - else { - if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){ - return String(); - } - wifi_ap_record_t info; - if(!esp_wifi_sta_get_ap_info(&info)) { - return String(reinterpret_cast(info.ssid)); +String WiFiManager::WiFi_SSID(bool persistent) +{ +#ifdef ESP8266 + struct station_config conf; + if (persistent) + wifi_station_get_config_default(&conf); + else + wifi_station_get_config(&conf); + + char tmp[33]; // ssid can be up to 32chars, => plus null term + memcpy(tmp, conf.ssid, sizeof(conf.ssid)); + tmp[32] = 0; // nullterm in case of 32 char ssid + return String(reinterpret_cast(tmp)); + +#elif defined(ESP32) + if (persistent) + { + wifi_config_t conf; + esp_err_t err; + err = esp_wifi_get_config(WIFI_IF_STA, &conf); + if (err == ESP_ERR_WIFI_NOT_INIT) + { + WiFi_enableSTA(true, true); + // WiFi_enableSTA(true, true); + int timeout = millis() + 1200; + // async loop for mode change + while (WiFi.getMode() != WIFI_STA && millis() < timeout) + { + delay(0); } + if (WiFi.getMode() != WIFI_STA) + return String(); + err = esp_wifi_get_config(WIFI_IF_STA, &conf); + } + return String(reinterpret_cast(conf.sta.ssid)); + } + else + { + if (WiFiGenericClass::getMode() == WIFI_MODE_NULL) + { return String(); } - #endif + wifi_ap_record_t info; + if (!esp_wifi_sta_get_ap_info(&info)) + { + return String(reinterpret_cast(info.ssid)); + } + return String(); + } +#endif } String WiFiManager::WiFi_psk(bool persistent) const { diff --git a/WiFiManager.h b/WiFiManager.h index 4c057ee1f..6ebc0798b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -618,7 +618,7 @@ class WiFiManager uint8_t WiFi_softap_num_stations(); bool WiFi_hasAutoConnect(); void WiFi_autoReconnect(); - String WiFi_SSID(bool persistent = true) const; + String WiFi_SSID(bool persistent = true); String WiFi_psk(bool persistent = true) const; bool WiFi_scanNetworks(); bool WiFi_scanNetworks(bool force,bool async); diff --git a/component.mk b/component.mk new file mode 100644 index 000000000..1c87c022c --- /dev/null +++ b/component.mk @@ -0,0 +1,2 @@ + +COMPONENT_ADD_INCLUDEDIRS := . \ No newline at end of file