From 999da6d1a17bd864e0732bd878e549dcf11fbe48 Mon Sep 17 00:00:00 2001 From: tablatronix Date: Wed, 11 Sep 2019 12:00:09 -0500 Subject: [PATCH] fixes #924 wifi info getters gets ssid and psk with persistent param, needs testing --- WiFiManager.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++----- WiFiManager.h | 8 ++++- 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 55e285f..47862fd 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -2362,9 +2362,29 @@ void WiFiManager::setCountry(String cc){ void WiFiManager::setClass(String str){ _bodyClass = str; } - + // HELPERS +/** + * getWiFiSSID + * @since $dev + * @param bool persistent + * @return String + */ +String WiFiManager::getWiFiSSID(bool persistent){ + return WiFi_SSID(persistent); +} + +/** + * getWiFiPass + * @since $dev + * @param bool persistent + * @return String + */ +String WiFiManager::getWiFiPass(bool persistent){ + return WiFi_psk(persistent); +} + // DEBUG // @todo fix DEBUG_WM(0,0); template @@ -2665,15 +2685,58 @@ bool WiFiManager::WiFi_hasAutoConnect(){ return WiFi_SSID() != ""; } -String WiFiManager::WiFi_SSID(){ - #ifdef ESP8266 - return WiFi.SSID(); - #elif defined(ESP32) - //@todo , workaround only when not connected +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)); + } + return String(); + } + #endif +} + +String WiFiManager::WiFi_psk(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[65]; //psk is 64 bytes hex => plus null term + memcpy(tmp, conf.password, sizeof(conf.password)); + tmp[64] = 0; //null term in case of 64 byte psk + return String(reinterpret_cast(tmp)); + + #elif defined(ESP32) + // only if wifi is init + if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){ + return String(); + } wifi_config_t conf; esp_wifi_get_config(WIFI_IF_STA, &conf); - return String(reinterpret_cast(conf.sta.ssid)); - #endif + return String(reinterpret_cast(conf.sta.password)); + #endif } #ifdef ESP32 diff --git a/WiFiManager.h b/WiFiManager.h index c373501..72e4c3a 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -269,6 +269,11 @@ class WiFiManager // check if the module has a saved ap to connect to bool getWiFiIsSaved(); + // helper to get saved ssid, if persistent get stored, else get current if connected + String getWiFiPass(bool persistent = false); + // helper to get saved password, if persistent get stored, else get current if connected + String getWiFiSSID(bool persistent = false); + // debug output the softap config void debugSoftAPConfig(); // debug output platform info and versioning @@ -422,7 +427,8 @@ class WiFiManager uint8_t WiFi_softap_num_stations(); bool WiFi_hasAutoConnect(); void WiFi_autoReconnect(); - String WiFi_SSID(); + String WiFi_SSID(bool persistent = false) const; + String WiFi_psk(bool persistent = false) const; bool WiFi_scanNetworks(); bool WiFi_scanNetworks(bool force,bool async); bool WiFi_scanNetworks(unsigned int cachetime,bool async);