From e1e8f522f83bdf6e1cbc5461634af540ecf2ceca Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 30 Jan 2019 17:25:53 +0100 Subject: [PATCH 01/13] Add ESP32 support, changed layout of inline HTML, small fixes --- PersWiFiManager.cpp | 118 ++++++++++++++++++++- PersWiFiManager.h | 15 +++ examples/basic_rest_api/basic_rest_api.ino | 21 +++- library.properties | 2 +- 4 files changed, 152 insertions(+), 4 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 29fb5f0..79717c6 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -6,10 +6,102 @@ #include "PersWiFiManager.h" #ifdef WIFI_HTM_PROGMEM -const char wifi_htm[] PROGMEM = R"=====(ESP WiFi







Back |Home
)====="; +const char wifi_htm[] PROGMEM = R"=====(" \ +"" \ +"" \ + "" \ + "" \ + "ESP WiFi" \ + "" \ + "" \ + "" \ + "" \ + "
" \ + "" \ + "

" \ + "
" \ + "" \ + "
" \ + "" \ + "

" \ + "" \ + "
" \ + "

" \ + "" \ + "
" \ + "Back |Home" \ + "
" \ + "" \ +"" \ +")====="; #endif +#if defined(ESP8266) PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d) { +#elif defined(ESP32) +PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d) { +#endif _server = &s; _dnsServer = &d; _apPass = ""; @@ -66,6 +158,12 @@ void PersWiFiManager::setConnectNonBlock(bool b) { _connectNonBlock = b; } //setConnectNonBlock +#if defined(ESP32) +#define ENC_TYPE_NONE 7 +#endif + +IPAddress apIP(192, 168, 1, 1); + void PersWiFiManager::setupWiFiHandlers() { IPAddress apIP(192, 168, 1, 1); _dnsServer->setErrorReplyCode(DNSReplyCode::NoError); @@ -90,8 +188,13 @@ void PersWiFiManager::setupWiFiHandlers() { s.reserve(2050); for (int i = 0; i < n && s.length() < 2000; i++) { //check s.length to limit memory usage if (ix[i] != -1) { +#if defined(ESP8266) s += String(i ? "\n" : "") + ((constrain(WiFi.RSSI(ix[i]), -100, -50) + 100) * 2) + "," + ((WiFi.encryptionType(ix[i]) == ENC_TYPE_NONE) ? 0 : 1) + "," + WiFi.SSID(ix[i]); +#elif defined(ESP32) + s += String(i ? "\n" : "") + ((constrain(WiFi.RSSI(ix[i]), -100, -50) + 100) * 2) + "," + + ((WiFi.encryptionType(ix[i]) == WIFI_AUTH_OPEN) ? 0 : 1) + "," + WiFi.SSID(ix[i]); +#endif } } @@ -114,13 +217,19 @@ void PersWiFiManager::setupWiFiHandlers() { delay(100); //ESP.restart(); // Adding Safer Restart method +#if defined(ESP8266) ESP.wdtDisable(); ESP.reset(); +#elif defined(ESP32) + ESP.restart(); +#endif delay(2000); }); #ifdef WIFI_HTM_PROGMEM _server->on("/wifi.htm", [&]() { + _server->sendHeader("Cache-Control", " no-cache, no-store, must-revalidate"); + _server->sendHeader("Expires", " 0"); _server->send(200, "text/html", wifi_htm); }); #endif @@ -128,12 +237,19 @@ void PersWiFiManager::setupWiFiHandlers() { }//setupWiFiHandlers bool PersWiFiManager::begin(const String& ssid, const String& pass) { +#if defined(ESP32) + WiFi.mode(WIFI_AP); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? +#endif setupWiFiHandlers(); return attemptConnection(ssid, pass); //switched order of these two for return } //begin String PersWiFiManager::getApSsid() { +#if defined(ESP8266) return _apSsid.length() ? _apSsid : "ESP8266"; +#elif defined(ESP32) + return _apSsid.length() ? _apSsid : "ESP32"; +#endif } //getApSsid void PersWiFiManager::setApCredentials(const String& apSsid, const String& apPass) { diff --git a/PersWiFiManager.h b/PersWiFiManager.h index 80fcf03..fcc115a 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -1,8 +1,15 @@ #ifndef PERSWIFIMANAGER_H #define PERSWIFIMANAGER_H +#if defined(ESP8266) #include #include +#elif defined(ESP32) +#include +#include +#else +#error "Unknown board class" +#endif #include #define WIFI_CONNECT_TIMEOUT 30 @@ -13,7 +20,11 @@ class PersWiFiManager { typedef std::function WiFiChangeHandlerFunction; +#if defined(ESP8266) PersWiFiManager(ESP8266WebServer& s, DNSServer& d); +#elif defined(ESP32) + PersWiFiManager(WebServer& s, DNSServer& d); +#endif bool attemptConnection(const String& ssid = "", const String& pass = ""); @@ -36,7 +47,11 @@ class PersWiFiManager { void onAp(WiFiChangeHandlerFunction fn); private: +#if defined(ESP8266) ESP8266WebServer * _server; +#elif defined(ESP32) + WebServer * _server; +#endif DNSServer * _dnsServer; String _apSsid, _apPass; diff --git a/examples/basic_rest_api/basic_rest_api.ino b/examples/basic_rest_api/basic_rest_api.ino index b2f22dc..5fcf148 100644 --- a/examples/basic_rest_api/basic_rest_api.ino +++ b/examples/basic_rest_api/basic_rest_api.ino @@ -11,19 +11,36 @@ //includes #include #include +#if defined(ESP8266) #include #include #include -#include #include +#elif defined(ESP32) +#include +#include +#include +#include +#else +#error "Unsupported board class" +#endif +#include +#if defined(ESP8266) #define DEVICE_NAME "ESP8266 DEVICE" +#elif defined(ESP32) +#define DEVICE_NAME "ESP32 DEVICE" +#endif //const char *metaRefreshStr = "redirecting..."; const char *metaRefreshStr = "redirecting..."; //server objects +#if defined(ESP8266) ESP8266WebServer server(80); +#elif defined(ESP32) +WebServer server(80); +#endif DNSServer dnsServer; PersWiFiManager persWM(server, dnsServer); @@ -138,8 +155,8 @@ void setup() { SSDP.setHTTPPort(80); SSDP.setName(DEVICE_NAME); SSDP.setURL("/"); - SSDP.begin(); SSDP.setDeviceType("upnp:rootdevice"); + SSDP.begin(); server.begin(); DEBUG_PRINT("setup complete."); diff --git a/library.properties b/library.properties index 520b6d1..296d4bf 100644 --- a/library.properties +++ b/library.properties @@ -6,4 +6,4 @@ sentence=Persistent WiFi Manager paragraph=A non-blocking, persistant wifi manager for ESP8266 that allows network changes at any time category=Communication url=http://ryandowning.net/PersWiFiManager/ -architectures=esp8266 +architectures=esp8266,esp32 From 7979076a5562eb62790b85bd793ab157e373991e Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 30 Jan 2019 17:54:32 +0100 Subject: [PATCH 02/13] Removed temporary #define --- PersWiFiManager.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 79717c6..3442289 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -158,10 +158,6 @@ void PersWiFiManager::setConnectNonBlock(bool b) { _connectNonBlock = b; } //setConnectNonBlock -#if defined(ESP32) -#define ENC_TYPE_NONE 7 -#endif - IPAddress apIP(192, 168, 1, 1); void PersWiFiManager::setupWiFiHandlers() { From d4c76cec1f5f479619e5b7a665ea2afe7236c30b Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 08:15:06 +0100 Subject: [PATCH 03/13] Removed extra IPAddress declaration --- PersWiFiManager.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 3442289..bf27d14 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -147,7 +147,8 @@ void PersWiFiManager::handleWiFi() { void PersWiFiManager::startApMode(){ //start AP mode - IPAddress apIP(192, 168, 1, 1); +// IPAddress apIP(192, 168, 1, 1); + IPAddress apIP(192, 168, 4, 1); WiFi.mode(WIFI_AP); WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); _apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str()); @@ -158,8 +159,6 @@ void PersWiFiManager::setConnectNonBlock(bool b) { _connectNonBlock = b; } //setConnectNonBlock -IPAddress apIP(192, 168, 1, 1); - void PersWiFiManager::setupWiFiHandlers() { IPAddress apIP(192, 168, 1, 1); _dnsServer->setErrorReplyCode(DNSReplyCode::NoError); From af1190569ebbb4ce03a3fd19fd2e0b8f1f6df9ab Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 08:17:30 +0100 Subject: [PATCH 04/13] Changed Access Point IP address to 192.168.4.1 --- PersWiFiManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index bf27d14..abcf5ee 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -147,7 +147,6 @@ void PersWiFiManager::handleWiFi() { void PersWiFiManager::startApMode(){ //start AP mode -// IPAddress apIP(192, 168, 1, 1); IPAddress apIP(192, 168, 4, 1); WiFi.mode(WIFI_AP); WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); From 81dd4c0480ea3538a42b297dc6314c541122fac8 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 17:46:38 +0100 Subject: [PATCH 05/13] Start AP directly without waiting for timeout if no WiFi credentials are found --- PersWiFiManager.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index abcf5ee..261e885 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -114,6 +114,10 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); } else { + if(!WiFi.SSID().length()) { // No saved credentials, so skip trying to connect + _connectStartTime = millis(); + return false; + } WiFi.begin(); } @@ -137,8 +141,8 @@ void PersWiFiManager::handleWiFi() { return; } - //if failed or not connected and time is up - if ((WiFi.status() == WL_CONNECT_FAILED) || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { + //if failed or no saved SSID or not connected and time is up + if ((WiFi.status() == WL_CONNECT_FAILED) || !WiFi.SSID().length() || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { startApMode(); _connectStartTime = 0; //reset connect start time } @@ -232,7 +236,8 @@ void PersWiFiManager::setupWiFiHandlers() { bool PersWiFiManager::begin(const String& ssid, const String& pass) { #if defined(ESP32) - WiFi.mode(WIFI_AP); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? +// WiFi.mode(WIFI_AP); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? + WiFi.mode(WIFI_STA); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? #endif setupWiFiHandlers(); return attemptConnection(ssid, pass); //switched order of these two for return From 4c5022086d7bcbd1ad84c0e972ddc6a0ba4effac Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 18:18:41 +0100 Subject: [PATCH 06/13] FIX Start AP directly without waiting for timeout if no WiFi credentials are found --- PersWiFiManager.cpp | 8 ++++++-- PersWiFiManager.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 261e885..7b2fc0c 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -105,6 +105,7 @@ PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d) { _server = &s; _dnsServer = &d; _apPass = ""; + _freshConnectionAttempt = false; } //PersWiFiManager bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) { @@ -114,11 +115,12 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); } else { + WiFi.begin(); if(!WiFi.SSID().length()) { // No saved credentials, so skip trying to connect _connectStartTime = millis(); + _freshConnectionAttempt = true; return false; } - WiFi.begin(); } //if in nonblock mode, skip this loop @@ -142,9 +144,11 @@ void PersWiFiManager::handleWiFi() { } //if failed or no saved SSID or not connected and time is up - if ((WiFi.status() == WL_CONNECT_FAILED) || !WiFi.SSID().length() || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { + if ((WiFi.status() == WL_CONNECT_FAILED) || _freshConnectionAttempt || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { +// if ((WiFi.status() == WL_CONNECT_FAILED) || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { startApMode(); _connectStartTime = 0; //reset connect start time + _freshConnectionAttempt = false; } } //handleWiFi diff --git a/PersWiFiManager.h b/PersWiFiManager.h index fcc115a..100274e 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -57,6 +57,7 @@ class PersWiFiManager { bool _connectNonBlock; unsigned long _connectStartTime; + bool _freshConnectionAttempt; WiFiChangeHandlerFunction _connectHandler; WiFiChangeHandlerFunction _apHandler; From 02965b19707810dd7dbda35a642fb0e6dc292ff8 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 19:10:09 +0100 Subject: [PATCH 07/13] FIX2 Start AP directly without waiting for timeout if no WiFi credentials are found --- PersWiFiManager.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 7b2fc0c..4a049ae 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -5,6 +5,10 @@ #include "PersWiFiManager.h" +#if defined(ESP32) +#include +#endif + #ifdef WIFI_HTM_PROGMEM const char wifi_htm[] PROGMEM = R"=====(" \ "" \ @@ -115,11 +119,20 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); } else { - WiFi.begin(); - if(!WiFi.SSID().length()) { // No saved credentials, so skip trying to connect +#if defined(ESP8266) + if(!WiFi.SSID().length() && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect +#elif defined(ESP32) + wifi_config_t conf; + esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct comf + const char *SSID = reinterpret_cast(conf.sta.ssid); + const char *password = reinterpret_cast(conf.sta.password); + if((!SSID || strlen(SSID) <= 1) && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect +#endif _connectStartTime = millis(); _freshConnectionAttempt = true; return false; + } else { + WiFi.begin(); } } @@ -143,9 +156,8 @@ void PersWiFiManager::handleWiFi() { return; } - //if failed or no saved SSID or not connected and time is up + //if failed or no saved SSID or no WiFi credentials were found or not connected and time is up if ((WiFi.status() == WL_CONNECT_FAILED) || _freshConnectionAttempt || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { -// if ((WiFi.status() == WL_CONNECT_FAILED) || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { startApMode(); _connectStartTime = 0; //reset connect start time _freshConnectionAttempt = false; From 065d1061970a9060173e86ebf8815f49b0105d73 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 21:09:09 +0100 Subject: [PATCH 08/13] Change DNS server IP to 192.168.4.1 too --- PersWiFiManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 4a049ae..9268ce0 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -179,7 +179,7 @@ void PersWiFiManager::setConnectNonBlock(bool b) { } //setConnectNonBlock void PersWiFiManager::setupWiFiHandlers() { - IPAddress apIP(192, 168, 1, 1); + IPAddress apIP(192, 168, 4, 1); _dnsServer->setErrorReplyCode(DNSReplyCode::NoError); _dnsServer->start((byte)53, "*", apIP); //used for captive portal in AP mode From 9a492020c00844eb14fa832617d5e433d97081bf Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 31 Jan 2019 21:40:58 +0100 Subject: [PATCH 09/13] Fix bug in ESP8266 check on first time connection --- PersWiFiManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 9268ce0..284b2b6 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -120,7 +120,7 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) else WiFi.begin(ssid.c_str()); } else { #if defined(ESP8266) - if(!WiFi.SSID().length() && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect + if((WiFi.SSID().length() <= 1) && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect #elif defined(ESP32) wifi_config_t conf; esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct comf From fed3f1dbb37276ddf3a6a4b60f34d9363d4c203e Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 17:29:37 +0100 Subject: [PATCH 10/13] Add PersWiFiManager::resetSettings(), that clears the WiFi credentials, e.g. for testing --- PersWiFiManager.cpp | 16 +++++++++++++--- PersWiFiManager.h | 2 ++ examples/basic_rest_api/basic_rest_api.ino | 3 +++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 284b2b6..08403d4 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -120,13 +120,13 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) else WiFi.begin(ssid.c_str()); } else { #if defined(ESP8266) - if((WiFi.SSID().length() <= 1) && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect + if((WiFi.SSID().length() == 0) && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect #elif defined(ESP32) wifi_config_t conf; esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct comf const char *SSID = reinterpret_cast(conf.sta.ssid); const char *password = reinterpret_cast(conf.sta.password); - if((!SSID || strlen(SSID) <= 1) && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect + if((strlen(SSID) == 0) && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect #endif _connectStartTime = millis(); _freshConnectionAttempt = true; @@ -252,13 +252,23 @@ void PersWiFiManager::setupWiFiHandlers() { bool PersWiFiManager::begin(const String& ssid, const String& pass) { #if defined(ESP32) -// WiFi.mode(WIFI_AP); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? WiFi.mode(WIFI_STA); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? #endif setupWiFiHandlers(); return attemptConnection(ssid, pass); //switched order of these two for return } //begin +void PersWiFiManager::resetSettings() { +#if defined(ESP8266) + WiFi.disconnect(); +#elif defined(ESP32) + wifi_mode_t m = WiFi.getMode(); + if(!(m & WIFI_MODE_STA)) WiFi.mode(WIFI_STA); + WiFi.disconnect(false, true); + if(!(m & WIFI_MODE_STA)) WiFi.mode(m); +#endif +} // resetSettings + String PersWiFiManager::getApSsid() { #if defined(ESP8266) return _apSsid.length() ? _apSsid : "ESP8266"; diff --git a/PersWiFiManager.h b/PersWiFiManager.h index 100274e..5f2fbcc 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -32,6 +32,8 @@ class PersWiFiManager { bool begin(const String& ssid = "", const String& pass = ""); + void resetSettings(); + String getApSsid(); void setApCredentials(const String& apSsid, const String& apPass = ""); diff --git a/examples/basic_rest_api/basic_rest_api.ino b/examples/basic_rest_api/basic_rest_api.ino index 5fcf148..5b931f0 100644 --- a/examples/basic_rest_api/basic_rest_api.ino +++ b/examples/basic_rest_api/basic_rest_api.ino @@ -113,6 +113,9 @@ void setup() { //allows serving of files from SPIFFS SPIFFS.begin(); persWM.begin(); + //reset saved settings, clears WiFi credentials e.g. for testing + //persWM.resetSettings(); + //serve files from SPIFFS server.onNotFound([]() { From 5376c3f94f917e929bcc84e90e379946ddb57ee5 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 19:58:23 +0100 Subject: [PATCH 11/13] Add ::resetSettings() when connecting to new SSID --- PersWiFiManager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 08403d4..34d889d 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -116,6 +116,7 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) //attempt to connect to wifi WiFi.mode(WIFI_STA); if (ssid.length()) { + resetSettings(); // To avoid issues (experience from WiFiManager) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); } else { From 2189c77b2a8d57677393ae170934f5b72c85904d Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 21:10:42 +0100 Subject: [PATCH 12/13] Correct literal HTM string --- PersWiFiManager.cpp | 178 ++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 34d889d..91c2388 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -10,95 +10,95 @@ #endif #ifdef WIFI_HTM_PROGMEM -const char wifi_htm[] PROGMEM = R"=====(" \ -"" \ -"" \ - "" \ - "" \ - "ESP WiFi" \ - "" \ - "" \ - "" \ - "" \ - "
" \ - "" \ - "

" \ - "
" \ - "" \ - "
" \ - "" \ - "

" \ - "" \ - "
" \ - "

" \ - "" \ - "
" \ - "Back |Home" \ - "
" \ - "" \ -"" \ -")====="; +const char wifi_htm[] PROGMEM = R"=====( + + + + + ESP WiFi + + + + +
+ +

+
+ +
+ +

+ +
+

+ +
+ Back |Home +
+ + +)====="; #endif #if defined(ESP8266) From cfa1f0580b1f1265f065a4dc312e0127c496d2db Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 21:42:13 +0100 Subject: [PATCH 13/13] Add ::getSsid() to get the SSID stored in non volatile memory --- PersWiFiManager.cpp | 21 ++++++++++++--------- PersWiFiManager.h | 2 ++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 91c2388..99690ff 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -120,15 +120,7 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); } else { -#if defined(ESP8266) - if((WiFi.SSID().length() == 0) && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect -#elif defined(ESP32) - wifi_config_t conf; - esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct comf - const char *SSID = reinterpret_cast(conf.sta.ssid); - const char *password = reinterpret_cast(conf.sta.password); - if((strlen(SSID) == 0) && WiFi.status() != WL_CONNECTED) { // No saved credentials, so skip trying to connect -#endif + if((getSsid() == "") && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect _connectStartTime = millis(); _freshConnectionAttempt = true; return false; @@ -278,6 +270,17 @@ String PersWiFiManager::getApSsid() { #endif } //getApSsid +String PersWiFiManager::getSsid() { +#if defined(ESP8266) + return WiFi.SSID(); +#elif defined(ESP32) + wifi_config_t conf; + esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct comf + const char *SSID = reinterpret_cast(conf.sta.ssid); + return String(SSID); +#endif +} //getSsid + void PersWiFiManager::setApCredentials(const String& apSsid, const String& apPass) { if (apSsid.length()) _apSsid = apSsid; if (apPass.length() >= 8) _apPass = apPass; diff --git a/PersWiFiManager.h b/PersWiFiManager.h index 5f2fbcc..d801b86 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -36,6 +36,8 @@ class PersWiFiManager { String getApSsid(); + String getSsid(); + void setApCredentials(const String& apSsid, const String& apPass = ""); void setConnectNonBlock(bool b);