From e1e8f522f83bdf6e1cbc5461634af540ecf2ceca Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 30 Jan 2019 17:25:53 +0100 Subject: [PATCH 01/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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/16] 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 3efc826743d7d410a6a68c8a4c7302fd69e25211 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 20:57:59 +0100 Subject: [PATCH 12/16] First steps to report status --- PersWiFiManager.cpp | 229 +++++++++++++++++++++++++------------------- PersWiFiManager.h | 4 + 2 files changed, 134 insertions(+), 99 deletions(-) diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 34d889d..0b05aa3 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -9,96 +9,100 @@ #include #endif +#define WIFI_HTM_PROGMEM + #ifdef WIFI_HTM_PROGMEM -const char wifi_htm[] PROGMEM = R"=====(" \ -"" \ -"" \ - "" \ - "" \ - "ESP WiFi" \ - "" \ - "" \ - "" \ - "" \ - "
" \ - "" \ - "

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

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

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

+
+ +
+ +

+ +
+

+ +
+ {{status}} +
+ Back |Home +
+ + +)====="; #endif #if defined(ESP8266) @@ -120,15 +124,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; @@ -148,6 +144,26 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) } //attemptConnection +void PersWiFiManager::reportStatus(String &page) { + String statusReport; + if (getSsid() != "") { + statusReport += F("Configured to connect to access point "); + statusReport += getSsid(); + if (WiFi.status()==WL_CONNECTED){ + statusReport += F(" and currently connected on IP "); + statusReport += WiFi.localIP().toString(); + statusReport += F(""); + } else { + statusReport += F(" but not currently connected to network."); + } + } else { + statusReport += F("No network currently configured."); + } + page.replace(F("{{status}}"), statusReport); +} // reportStatus + void PersWiFiManager::handleWiFi() { if (!_connectStartTime) return; @@ -245,7 +261,11 @@ void PersWiFiManager::setupWiFiHandlers() { _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); +// _server->send(200, "text/html", wifi_htm); + String page = wifi_htm; + reportStatus(page); +Serial.printf("Page: '%s'\n", page.c_str()); + _server->send(200, "text/html", page); }); #endif @@ -278,6 +298,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 conf + 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..b8af320 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -28,6 +28,8 @@ class PersWiFiManager { bool attemptConnection(const String& ssid = "", const String& pass = ""); + void reportStatus(String &page); + void setupWiFiHandlers(); bool begin(const String& ssid = "", const String& pass = ""); @@ -36,6 +38,8 @@ class PersWiFiManager { String getApSsid(); + String getSsid(); + void setApCredentials(const String& apSsid, const String& apPass = ""); void setConnectNonBlock(bool b); From 2189c77b2a8d57677393ae170934f5b72c85904d Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 1 Feb 2019 21:10:42 +0100 Subject: [PATCH 13/16] 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 14/16] 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); From f3f854a8331ffa9c5568d129ea8fbabba6a05adb Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 3 Feb 2019 18:28:11 +0100 Subject: [PATCH 15/16] EXPERIMENTAL: Access Point Portal with reporting back successful connections --- PersWiFiManager.cpp | 183 +++++++++++---- PersWiFiManager.h | 7 +- data/wifi.htm | 222 +++++++++--------- data/wifi.min.htm | 2 +- data/wifi.min.htm.gz | Bin 1159 -> 1392 bytes .../basic_rest_api_reporter.ino | 191 +++++++++++++++ .../basic_rest_api_reporter/data/favicon.ico | Bin 0 -> 15086 bytes .../basic_rest_api_reporter/data/index.htm | 52 ++++ .../data/wifi.min.htm.gz | Bin 0 -> 1159 bytes 9 files changed, 503 insertions(+), 154 deletions(-) create mode 100644 examples/basic_rest_api_reporter/basic_rest_api_reporter.ino create mode 100644 examples/basic_rest_api_reporter/data/favicon.ico create mode 100644 examples/basic_rest_api_reporter/data/index.htm create mode 100644 examples/basic_rest_api_reporter/data/wifi.min.htm.gz diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index f1739a6..7d0dbdd 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -9,13 +9,13 @@ #include #endif -#define WIFI_HTM_PROGMEM - #ifdef WIFI_HTM_PROGMEM const char wifi_htm[] PROGMEM = R"=====( + + ESP WiFi - - - -
- -

-
- -
- -
-
- -
-
-
- -
- -
- -
- Back | - Home -
- - + + + + + ESP WiFi + + + + +
+ +

+
+ +
+ +

+
+ +

+ +
+

+
+ Back |Home +
+ + diff --git a/data/wifi.min.htm b/data/wifi.min.htm index 49a4a55..54df19d 100644 --- a/data/wifi.min.htm +++ b/data/wifi.min.htm @@ -1 +1 @@ -ESP WiFi









Back | Home
\ No newline at end of file +ESP WiFi








Back |Home
diff --git a/data/wifi.min.htm.gz b/data/wifi.min.htm.gz index c8b0d2fbde7b60a8324c9c2e79b8a98a8363c2c3..b71b706f45c838f2fff977bebe6cef959d46bba5 100644 GIT binary patch literal 1392 zcmV-$1&{h4iwFn<9#>od19xd=X)bMPZZ2qaZ2+xSTW{Mo6n@{YP#H9m5L>>aEjr6| zkviFup*Pei<{`)fEgjixXi_05yPoI&zC+2fout{*fLy4se;!`{_2t5w z*|`|>w|{}9gZ>;W^AaU3NK1)@AGYPT<7CFP0ZR*;HQy36w0RTm3%;OaSZOz}S2>W! z9)6+^GEpg)(^^-uIZ(3Tu*{XVWN*9Vsh!aj7CeDw`O)yWMIb7X^@ z0$z#9I@SD{0^w$rhPha2pUw!YEVv zf+e$H7X`w#0P9%_wOWIO4MWjNAVq?17=2e<27UvJaDb-tBJfkb@WTdoONNVNyNLPC z=r-^Z!HhuzgOcQdiS{wl27flLcSxdu{C;tbh%I@>TY?))8det&;#>2+=+n!g*a|wwJ zG`Q3GS&CIfvSH0KXQOlbJg0CIp~Sys(}$J69j}HzB`M1PFT$1imC4gTMxQ@LCEURi z=DCnaO>|o+N87?7#pAopeN-d~7?Tx3ti0X6XO%}zwpOFj)p@)DVZhtFa>5t1kSSzb z9y%PVLZqHlw)XanubsgswhrSH6Q08I-GDZwUh_L)~basWQ=dbMBoz4GkfF@~ql z_isn`veh}Bj^}mro$kZ<<;;g~U$25HlWn>0Bqb*<`UyGCap|0gb$ZCeJu~~Q?eNAa zZdW+Xxy%c@&ROcNlHTk5N1Wlk(KP&T=yhNom-j{Q*VivM6P2#k?j~z8!Kbp9V78zu z^^DEASoId5QzltFVaeT8E9_J=Q9|k7M4%+D*2@{Uuwm=!>!_2lQ%E*AsjZUhiP9

9dhtq$smy1R-x?e@z!d@g$1L@vNz*t3NkLy*$F`io*QoBvf`%6 zSpq$&mYU_8qor6MrrL>K!+e9?DVx%7Rj&vK`Jj)J*DK}JKY(9AX5;@z@U+{;W*#)w z$DUUxdq`7%Vx{rDdM=ad-XnwKqaWSQWD>bDMw>0*`)E6p)p`AG4l?+K7oe3hJbJuD!BnG)D;>btXACaND;=Cx(M zvwZW)v-na%5t1EGa_T?yv=dj}%HNjD`w0xm9tOW2(;34<9r}aqnB4)f^Yz$?KEN^$ z{I4)kIF71E*E_rhD_Arlg&>q7)fz@z07zeG2Y~?d`@XNme85iQ*3IG7EJhb@$ literal 1159 zcmV;21bF)&iwFp66CPRu3wLQ|X)bMPZZ2qaZ2*NnW<- zQcSYllhI<5Et#W8(j_B!#aW}3aTw)FIwl>#4KG|p3bx`o>$gG=amgL0qHl957#a0Y zZJB8$ItPVRcreKvcY;l(vupH;zvJ1UlQWweuHB@pW$w6=Xc6*gW0xpQ?%MeJG+$=*EMX2GjP)#gUPQ4Wo$1c;*J`Iu^MjhM5 z0fTM2q6Ws6?B8U8{J25@aOvC*OLo)UW~># z0df5LY>3<105G&A>Q4Dy^@HXL<|cq>Tlj2!5uu$z{QL9UvTU)Pe&}~?M@skYNAGLZC z92G&Z(xpG4crqSBosViB0A731Xg?6RTRV92;@QJ^G@qx)FO6)_nN2c{ctQM90YZ9` z!8|V!Ed5G>Rx9fQMB9NP!JzF%SgVrd4$RwzYrSpIy&?h`Ai?AU-U%mT1f+9TDFJ0B z!EAPQ;mMg}KfU+U2fx>^kXvi7g6VFcM}qHh4(bl8{dNg=ZnmB;Ij}qLXg=mS@R>bv zI^2>D1K5bhp-LZAOVgO3_>-QiORfd%Wf1xYdZ3XC8>y%l?ex`i+-@@N)Kqu%QbTA*_$)#E{ Zug(V0f$;3QanSw?@GoP;HFFpU0001+GHL(- diff --git a/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino b/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino new file mode 100644 index 0000000..9435667 --- /dev/null +++ b/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino @@ -0,0 +1,191 @@ +#define DEBUG_SERIAL //uncomment for Serial debugging statements + +#ifdef DEBUG_SERIAL +#define DEBUG_BEGIN Serial.begin(115200) +#define DEBUG_PRINT(x) Serial.println(x) +#else +#define DEBUG_PRINT(x) +#define DEBUG_BEGIN +#endif + +//includes +#include +#include +#if defined(ESP8266) +#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); + +////// Sample program data +int x; +String y; + + +//code from fsbrowser example, consolidated. +bool handleFileRead(String path) { + DEBUG_PRINT("handlefileread" + path); +// if (path.endsWith("/")) path += "index.htm"; + if (path.endsWith("/")) path += "wifi.htm"; + String contentType; + if (path.endsWith(".htm") || path.endsWith(".html")) contentType = "text/html"; + else if (path.endsWith(".css")) contentType = "text/css"; + else if (path.endsWith(".js")) contentType = "application/javascript"; + else if (path.endsWith(".png")) contentType = "image/png"; + else if (path.endsWith(".gif")) contentType = "image/gif"; + else if (path.endsWith(".jpg")) contentType = "image/jpeg"; + else if (path.endsWith(".ico")) contentType = "image/x-icon"; + else if (path.endsWith(".xml")) contentType = "text/xml"; + else if (path.endsWith(".pdf")) contentType = "application/x-pdf"; + else if (path.endsWith(".zip")) contentType = "application/x-zip"; + else if (path.endsWith(".gz")) contentType = "application/x-gzip"; + else if (path.endsWith(".json")) contentType = "application/json"; + else contentType = "text/plain"; + + //split filepath and extension + String prefix = path, ext = ""; + int lastPeriod = path.lastIndexOf('.'); + if (lastPeriod >= 0) { + prefix = path.substring(0, lastPeriod); + ext = path.substring(lastPeriod); + } + + //look for smaller versions of file + //minified file, good (myscript.min.js) + if (SPIFFS.exists(prefix + ".min" + ext)) path = prefix + ".min" + ext; + //gzipped file, better (myscript.js.gz) + if (SPIFFS.exists(prefix + ext + ".gz")) path = prefix + ext + ".gz"; + //min and gzipped file, best (myscript.min.js.gz) + if (SPIFFS.exists(prefix + ".min" + ext + ".gz")) path = prefix + ".min" + ext + ".gz"; + + if (SPIFFS.exists(path)) { + DEBUG_PRINT("sending file " + path); + File file = SPIFFS.open(path, "r"); + if (server.hasArg("download")) + server.sendHeader("Content-Disposition", " attachment;"); + if (server.uri().indexOf("nocache") < 0) + server.sendHeader("Cache-Control", " max-age=172800"); + + //optional alt arg (encoded url), server sends redirect to file on the web + if (WiFi.status() == WL_CONNECTED && server.hasArg("alt")) { + server.sendHeader("Location", server.arg("alt"), true); + server.send ( 302, "text/plain", ""); + } else { + //server sends file + size_t sent = server.streamFile(file, contentType); + } + file.close(); + return true; + } //if SPIFFS.exists + return false; +} //bool handleFileRead + +void setup() { + DEBUG_BEGIN; //for terminal debugging + DEBUG_PRINT(); + + //allows serving of files from SPIFFS + SPIFFS.begin(); + + //reset saved settings + persWM.resetSettings(); + + //make connecting/disconnecting non-blocking + persWM.setConnectNonBlock(true); + + persWM.begin(); + + persWM.onConnect([]() { + DEBUG_PRINT("wifi connected"); + DEBUG_PRINT(WiFi.localIP()); + }); + + persWM.onAp([](){ + DEBUG_PRINT("AP MODE"); + DEBUG_PRINT(persWM.getApSsid()); + DEBUG_PRINT(WiFi.softAPIP()); + }); + + //serve files from SPIFFS + server.onNotFound([]() { + if (!handleFileRead(server.uri())) { + server.sendHeader("Cache-Control", " max-age=172800"); + server.send(302, "text/html", metaRefreshStr); + } + }); //server.onNotFound + + //handles commands from webpage, sends live data in JSON format + server.on("/api", []() { + DEBUG_PRINT("server.on /api"); + if (server.hasArg("x")) { + x = server.arg("x").toInt(); + DEBUG_PRINT(String("x: ")+x); + } //if + if (server.hasArg("y")) { + y = server.arg("y"); + DEBUG_PRINT("y: "+y); + } //if + + //build json object of program data + StaticJsonBuffer<200> jsonBuffer; + JsonObject &json = jsonBuffer.createObject(); + json["x"] = x; + json["y"] = y; + + char jsonchar[200]; + json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece + server.send(200, "application/json", jsonchar); + + }); //server.on api + + + //SSDP makes device visible on windows network + server.on("/description.xml", HTTP_GET, []() { + SSDP.schema(server.client()); + }); + SSDP.setSchemaURL("description.xml"); + SSDP.setHTTPPort(80); + SSDP.setName(DEVICE_NAME); + SSDP.setURL("/"); + SSDP.setDeviceType("upnp:rootdevice"); + SSDP.begin(); + + server.begin(); // Inconsistency: the DNS server is started from the library and the web server directly from setup() + DEBUG_PRINT("setup complete."); +} //void setup + +void loop() { + persWM.handleWiFi(); + dnsServer.processNextRequest(); + server.handleClient(); + + // do stuff with x and y + +} //void loop diff --git a/examples/basic_rest_api_reporter/data/favicon.ico b/examples/basic_rest_api_reporter/data/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..1cd2a8b468a04c5f047813ccd6c8eb1608bfb6e1 GIT binary patch literal 15086 zcmd6tUx-vy9LMjvru`vi$v`M%C$R9L(h96tX^NhLqL)C+42uwo zT4*3?Nc55s8Wh1U8x#`wBw2#ZR%j1_W2R2E?(O@To!@dioI7*xof&t3^7Z`t{l357 zIrrRi&ONhH)ED(ft5!uyi_s^yM$zghii*W%c_4~Ts%}6j?VA@y(OXhjAsv}S8wKhc zvV11#!T;BY^HJe#VZZQ=a9sGS3G&p@h7P*e%%v=m^XG(v!g(Ri2yC#01FkD2*U8ba za8Ae?bCNz>aKhay=@;=P;k1wxN2m30;{#val$%6$M93Q7v}{0DJAC1@8^UsNN@y3u zuhJP6)(LBbqOhn5@H{&gd)};DN$bo3wJj2QE%?A!s1L-}YHi(=V8@tTiP+_)A^1vq4#XG|_P)^G#GLHq zoC(E39X>0#CT&x|#p7w@WQQwhF9>k_fL!uct5o?%^^Yas@IT#1#KO*jMk zI_xX$-gmR_Wl9X8J?>e@Dd$)}e)h)sBE;gyN6ZM{5uyIEzt0!peB#GX%=X>nw@uEM zz@0caANq0HH=kcW;@IWMUN56}h3|zQg-?Zhgid5s#(tdkz3SJm?ZDA3vJI`<|EhjN z2fUoi{W$llj_+!}ekiA(1+rtzeLgGytv33XN7l&5cr3Mh0xMfu%;P{dn*%jS!b#2&IEk8+~o7_iNr)~E}z}cxOI8s|JdVpIo|OYH0cetp{gOE<4kJz#Hqyi0q9ZhSuwlkr+g?Cil-wlwYe;p>2{ zaU4_I146fin2g&}y9eehWObIdJHI^@u)+3%@S1Ry&@CYri_21CwmWRjqE>HdtMl`K z^#2Ih99CW4{Z*!k((3$-QP%I5U9z!$lKw6{A@oAhe7`J_<8y)=6Zt)XfVfAx)YRqM z^gV&|X0L=8{J4mfm|YoG!PoP@-}|L=w5fMQ>D@vvCGGb+F{HUac~*Kg;RS)SW-le{ z{Ry#fH{?G9%Hsm(k7WYqv|bCo@EMAQ7?Xtg!Mq6B9Z)^Iy%v1nD-;8<5o0UieLp3H z>=~18?4?Qm;Z7QNVj{LQ!Wwf?2=(El>UpoaEpWme@<}YjlttD{ImAUB>k{i)+VPNT zqQC(coJszOC68d2^c?WT9?sr4AAdhmSPkL1CA|;scyT{7Dy$RM2t{F06Xb`4wZe!n zDY&+wJU+9|8JPryXPmSyg?wAxwCsSD)28_9#@rOmtgvov5<Zbc9^Ck7w?;}O zx3EtS3!(E;yZDmY;er$HDP*r}3lWG#N=Ng<2nJRG#K-obHR`N?D7xtg9t` z1Q+7CHmJkhU>w&AN=tD(rLhxN<)Y5lfwuyL6kC;4oQn~kH5tHy3-tY^l3MKrTeKd5x}@3aO~$S)e@ z=n>%qK|PIr7I;4adFp6G2iXi^3>55vVkk@Sll69_I#g*HSKnxouGrRZx1Kjt%MkT-)B^}p_NfW z8#dUwaloB44m=}&%nz&XW+AU&3x^v6K0@<>dE)9}*Ddj5z?UnGi9633w@&))a+8P4 zeHQp~pTjPd-M;5}-lAwJaJoM5iLh3=YYTI?SK_`G__Q_EjVtf(0BK_8-E@7~dTw>q zG$fABfbvDBoF;ohO6Je@5-aacLm(aeekgF(;vD~_z@2#-VVyL8wvSo)^)v+1+2?SW z?cS8Otkq^?d#IJ4Nx|`uV{dk(7lbrtH}(v(<2`iW`*!|Ww~jmZ#zXppkVbs}jDhjA z8-IU|G&|C?Ylc7m%mL%s-J%1_)ppU*H5=~R*R`t<>;Y*Nnj1* zUC1K-`^Oq!{z*gY&y$|cQI+o%vPx+EX}A8;KgO8Yn5!=fI_c+(0Ns2|8OJ*&;&@_598ZFsaeS8Rn8H-4CbgM=Q#|8ps0ds+yc;oSHuOS()6>Vvxb)hK`WoBmKiUiSW@`(%xNX-a=g^rn8867Ey}e$~CNHgwR% H2HXDuI~)0Q literal 0 HcmV?d00001 diff --git a/examples/basic_rest_api_reporter/data/index.htm b/examples/basic_rest_api_reporter/data/index.htm new file mode 100644 index 0000000..c6f2be1 --- /dev/null +++ b/examples/basic_rest_api_reporter/data/index.htm @@ -0,0 +1,52 @@ +

Current Data

+x: + +
y: + + +
Last updated + seconds ago. + +
+ +

Update Data

+
+ x: + + +
+
+ y: + + +
+ +WiFi settings + + diff --git a/examples/basic_rest_api_reporter/data/wifi.min.htm.gz b/examples/basic_rest_api_reporter/data/wifi.min.htm.gz new file mode 100644 index 0000000000000000000000000000000000000000..c8b0d2fbde7b60a8324c9c2e79b8a98a8363c2c3 GIT binary patch literal 1159 zcmV;21bF)&iwFp66CPRu3wLQ|X)bMPZZ2qaZ2*NnW<- zQcSYllhI<5Et#W8(j_B!#aW}3aTw)FIwl>#4KG|p3bx`o>$gG=amgL0qHl957#a0Y zZJB8$ItPVRcreKvcY;l(vupH;zvJ1UlQWweuHB@pW$w6=Xc6*gW0xpQ?%MeJG+$=*EMX2GjP)#gUPQ4Wo$1c;*J`Iu^MjhM5 z0fTM2q6Ws6?B8U8{J25@aOvC*OLo)UW~># z0df5LY>3<105G&A>Q4Dy^@HXL<|cq>Tlj2!5uu$z{QL9UvTU)Pe&}~?M@skYNAGLZC z92G&Z(xpG4crqSBosViB0A731Xg?6RTRV92;@QJ^G@qx)FO6)_nN2c{ctQM90YZ9` z!8|V!Ed5G>Rx9fQMB9NP!JzF%SgVrd4$RwzYrSpIy&?h`Ai?AU-U%mT1f+9TDFJ0B z!EAPQ;mMg}KfU+U2fx>^kXvi7g6VFcM}qHh4(bl8{dNg=ZnmB;Ij}qLXg=mS@R>bv zI^2>D1K5bhp-LZAOVgO3_>-QiORfd%Wf1xYdZ3XC8>y%l?ex`i+-@@N)Kqu%QbTA*_$)#E{ Zug(V0f$;3QanSw?@GoP;HFFpU0001+GHL(- literal 0 HcmV?d00001 From cd0c3892ea27d949fe4bea5f6101fda984d053d3 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sat, 9 Feb 2019 13:15:07 +0100 Subject: [PATCH 16/16] Adapted library with reporting back status --- PersWiFiManager.cpp | 56 ++++++--- PersWiFiManager.h | 12 +- data/wifi.min.htm | 2 +- data/wifi.min.htm.gz | Bin 1392 -> 0 bytes examples/basic_rest_api/basic_rest_api.ino | 10 +- examples/basic_rest_api/data/wifi.htm | 120 +++++++++++++++++++ examples/basic_rest_api/data/wifi.min.htm | 1 + examples/basic_rest_api/data/wifi.min.htm.gz | Bin 1159 -> 0 bytes 8 files changed, 177 insertions(+), 24 deletions(-) delete mode 100644 data/wifi.min.htm.gz create mode 100644 examples/basic_rest_api/data/wifi.htm create mode 100644 examples/basic_rest_api/data/wifi.min.htm delete mode 100644 examples/basic_rest_api/data/wifi.min.htm.gz diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 7d0dbdd..abc2d04 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -135,9 +135,9 @@ const char wifi_htm[] PROGMEM = R"=====( #endif #if defined(ESP8266) -PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d) { +PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d): _connectHandler(nullptr), _apHandler(nullptr), _apCloseHandler(nullptr) { #elif defined(ESP32) -PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d) { +PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d): _connectHandler(nullptr), _apHandler(nullptr), _apCloseHandler(nullptr) { #endif _server = &s; _dnsServer = &d; @@ -157,6 +157,7 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct conf const char *SSID = reinterpret_cast(conf.sta.ssid); const char *psk = reinterpret_cast(conf.sta.password); +if(WiFi.status() == WL_CONNECTED && String(SSID) == ssid && String(psk) == pass) Serial.println("ALREADY CON"); if(WiFi.status() == WL_CONNECTED && String(SSID) == ssid && String(psk) == pass) return true; // Already connected to this network #endif @@ -169,11 +170,18 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if((status = WiFi.status()) == WL_CONNECTED) break; delay(100); } - if(status != WL_CONNECTED) WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(status != WL_CONNECTED) { + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(!(m & WIFI_AP) && _apHandler) _apHandler(); + } } else { if(getSsid() == "") { // No saved credentials, so skip trying to connect _connectStartTime = millis(); _freshConnectionAttempt = true; + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Set AP Mode because connecting to router is not attempted + if(!(m & WIFI_AP) && _apHandler) _apHandler(); return false; } else { if(!(WiFi.getMode() & WIFI_STA)) WiFi.mode(wifi_mode_t(WiFi.getMode() | WIFI_STA)); // Add Station Mode to connect to router @@ -183,7 +191,11 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) if((status = WiFi.status()) == WL_CONNECTED) break; delay(100); } - if(status != WL_CONNECTED) WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(status != WL_CONNECTED) { + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(!(m & WIFI_AP) && _apHandler) _apHandler(); + } } } @@ -198,29 +210,27 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) } //attemptConnection void PersWiFiManager::handleWiFi() { + // If AP mode and no client connected, close AP mode if the ESP has connected to the router or if time is up + if((WiFi.getMode() & WIFI_AP) && (WiFi.softAPgetStationNum() == 0)) { + if((WiFi.status() == WL_CONNECTED) || (millis() - _apModeStartMillis > _apModeTimeoutMillis)) closeAp(); + } + if (!_connectStartTime) return; if (WiFi.status() == WL_CONNECTED) { _connectStartTime = 0; if (_connectHandler) _connectHandler(); - // Disable AP mode directly after connection - // wifi_mode_t m = WiFi.getMode(); - // if(m & WIFI_AP) { - // delay(100); - // WiFi.mode(wifi_mode_t(m & ~WIFI_AP)); - // } - return; } - +/* //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)))) { startApMode(); _connectStartTime = 0; //reset connect start time _freshConnectionAttempt = false; } - +*/ } //handleWiFi void PersWiFiManager::startApMode(){ @@ -236,8 +246,9 @@ void PersWiFiManager::closeAp(){ if(m & WIFI_AP) { delay(100); WiFi.mode(wifi_mode_t(m & ~WIFI_AP)); + if (_apCloseHandler) _apCloseHandler(); } -}// clodeAp +}// closeAp void PersWiFiManager::setConnectNonBlock(bool b) { _connectNonBlock = b; @@ -357,16 +368,22 @@ void PersWiFiManager::setupWiFiHandlers() { }); #endif + _server->begin(); }//setupWiFiHandlers -bool PersWiFiManager::begin(const String& ssid, const String& pass) { -//#if defined(ESP32) - WiFi.mode(WIFI_STA); // ESP32 needs this before setupWiFiHandlers(). Might be good for ESP8266 too? -//#endif +bool PersWiFiManager::begin(const String& ssid, const String& pass, time_t apModeTimeoutSeconds) { + _apModeTimeoutMillis = 1000 * apModeTimeoutSeconds; + _apModeStartMillis = millis(); + WiFi.mode(WIFI_STA); setupWiFiHandlers(); return attemptConnection(ssid, pass); //switched order of these two for return } //begin +void PersWiFiManager::stop() { + _server->stop(); + _dnsServer->stop(); +}// close + // Remove the WiFi credentials (e.g. for testing purposes) void PersWiFiManager::resetSettings() { #if defined(ESP8266) @@ -411,4 +428,7 @@ void PersWiFiManager::onAp(WiFiChangeHandlerFunction fn) { _apHandler = fn; } +void PersWiFiManager::onApClose(WiFiChangeHandlerFunction fn) { + _apCloseHandler = fn; +} diff --git a/PersWiFiManager.h b/PersWiFiManager.h index d3a3250..b3b9b83 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -12,7 +12,7 @@ #endif #include -#define WIFI_CONNECT_TIMEOUT 30 +//#define WIFI_CONNECT_TIMEOUT 30 class PersWiFiManager { @@ -30,7 +30,9 @@ class PersWiFiManager { void setupWiFiHandlers(); - bool begin(const String& ssid = "", const String& pass = ""); + bool begin(const String& ssid = "", const String& pass = "", time_t apModeTimeoutSeconds = 300); + + void stop(); void resetSettings(); @@ -52,6 +54,8 @@ class PersWiFiManager { void onAp(WiFiChangeHandlerFunction fn); + void onApClose(WiFiChangeHandlerFunction fn); + private: #if defined(ESP8266) ESP8266WebServer * _server; @@ -67,10 +71,14 @@ class PersWiFiManager { WiFiChangeHandlerFunction _connectHandler; WiFiChangeHandlerFunction _apHandler; + WiFiChangeHandlerFunction _apCloseHandler; String buildReport(); void sendNoCacheHeaders(); + + time_t _apModeTimeoutMillis; + time_t _apModeStartMillis; };//class #endif diff --git a/data/wifi.min.htm b/data/wifi.min.htm index 54df19d..12240bc 100644 --- a/data/wifi.min.htm +++ b/data/wifi.min.htm @@ -1 +1 @@ -ESP WiFi








Back |Home
+ ESP WiFi








Back |Home
diff --git a/data/wifi.min.htm.gz b/data/wifi.min.htm.gz deleted file mode 100644 index b71b706f45c838f2fff977bebe6cef959d46bba5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1392 zcmV-$1&{h4iwFn<9#>od19xd=X)bMPZZ2qaZ2+xSTW{Mo6n@{YP#H9m5L>>aEjr6| zkviFup*Pei<{`)fEgjixXi_05yPoI&zC+2fout{*fLy4se;!`{_2t5w z*|`|>w|{}9gZ>;W^AaU3NK1)@AGYPT<7CFP0ZR*;HQy36w0RTm3%;OaSZOz}S2>W! z9)6+^GEpg)(^^-uIZ(3Tu*{XVWN*9Vsh!aj7CeDw`O)yWMIb7X^@ z0$z#9I@SD{0^w$rhPha2pUw!YEVv zf+e$H7X`w#0P9%_wOWIO4MWjNAVq?17=2e<27UvJaDb-tBJfkb@WTdoONNVNyNLPC z=r-^Z!HhuzgOcQdiS{wl27flLcSxdu{C;tbh%I@>TY?))8det&;#>2+=+n!g*a|wwJ zG`Q3GS&CIfvSH0KXQOlbJg0CIp~Sys(}$J69j}HzB`M1PFT$1imC4gTMxQ@LCEURi z=DCnaO>|o+N87?7#pAopeN-d~7?Tx3ti0X6XO%}zwpOFj)p@)DVZhtFa>5t1kSSzb z9y%PVLZqHlw)XanubsgswhrSH6Q08I-GDZwUh_L)~basWQ=dbMBoz4GkfF@~ql z_isn`veh}Bj^}mro$kZ<<;;g~U$25HlWn>0Bqb*<`UyGCap|0gb$ZCeJu~~Q?eNAa zZdW+Xxy%c@&ROcNlHTk5N1Wlk(KP&T=yhNom-j{Q*VivM6P2#k?j~z8!Kbp9V78zu z^^DEASoId5QzltFVaeT8E9_J=Q9|k7M4%+D*2@{Uuwm=!>!_2lQ%E*AsjZUhiP9

9dhtq$smy1R-x?e@z!d@g$1L@vNz*t3NkLy*$F`io*QoBvf`%6 zSpq$&mYU_8qor6MrrL>K!+e9?DVx%7Rj&vK`Jj)J*DK}JKY(9AX5;@z@U+{;W*#)w z$DUUxdq`7%Vx{rDdM=ad-XnwKqaWSQWD>bDMw>0*`)E6p)p`AG4l?+K7oe3hJbJuD!BnG)D;>btXACaND;=Cx(M zvwZW)v-na%5t1EGa_T?yv=dj}%HNjD`w0xm9tOW2(;34<9r}aqnB4)f^Yz$?KEN^$ z{I4)kIF71E*E_rhD_Arlg&>q7)fz@z07zeG2Y~?d`@XNme85iQ*3IG7EJhb@$ diff --git a/examples/basic_rest_api/basic_rest_api.ino b/examples/basic_rest_api/basic_rest_api.ino index 5b931f0..90d0a6c 100644 --- a/examples/basic_rest_api/basic_rest_api.ino +++ b/examples/basic_rest_api/basic_rest_api.ino @@ -10,7 +10,7 @@ //includes #include -#include +// #include // ArduinoJson is a bit of an overkill for this use case #if defined(ESP8266) #include #include @@ -112,10 +112,11 @@ void setup() { //allows serving of files from SPIFFS SPIFFS.begin(); - persWM.begin(); + //reset saved settings, clears WiFi credentials e.g. for testing //persWM.resetSettings(); + persWM.begin(); //serve files from SPIFFS server.onNotFound([]() { @@ -137,6 +138,7 @@ void setup() { DEBUG_PRINT("y: "+y); } //if +/* //build json object of program data StaticJsonBuffer<200> jsonBuffer; JsonObject &json = jsonBuffer.createObject(); @@ -145,6 +147,9 @@ void setup() { char jsonchar[200]; json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece +*/ + char jsonchar[200]; + sprintf(jsonchar, "{\"x\":%d,\"y\":\"%s\"}", x, y.c_str()); // Easy alternative for ArduinoJson server.send(200, "application/json", jsonchar); }); //server.on api @@ -161,7 +166,6 @@ void setup() { SSDP.setDeviceType("upnp:rootdevice"); SSDP.begin(); - server.begin(); DEBUG_PRINT("setup complete."); } //void setup diff --git a/examples/basic_rest_api/data/wifi.htm b/examples/basic_rest_api/data/wifi.htm new file mode 100644 index 0000000..eedf71d --- /dev/null +++ b/examples/basic_rest_api/data/wifi.htm @@ -0,0 +1,120 @@ + + + + + + + ESP WiFi + + + + +

+ +

+
+ +
+ +

+
+ +

+ +
+

+
+ Back |Home +
+ + + diff --git a/examples/basic_rest_api/data/wifi.min.htm b/examples/basic_rest_api/data/wifi.min.htm new file mode 100644 index 0000000..12240bc --- /dev/null +++ b/examples/basic_rest_api/data/wifi.min.htm @@ -0,0 +1 @@ + ESP WiFi








Back |Home
diff --git a/examples/basic_rest_api/data/wifi.min.htm.gz b/examples/basic_rest_api/data/wifi.min.htm.gz deleted file mode 100644 index c8b0d2fbde7b60a8324c9c2e79b8a98a8363c2c3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1159 zcmV;21bF)&iwFp66CPRu3wLQ|X)bMPZZ2qaZ2*NnW<- zQcSYllhI<5Et#W8(j_B!#aW}3aTw)FIwl>#4KG|p3bx`o>$gG=amgL0qHl957#a0Y zZJB8$ItPVRcreKvcY;l(vupH;zvJ1UlQWweuHB@pW$w6=Xc6*gW0xpQ?%MeJG+$=*EMX2GjP)#gUPQ4Wo$1c;*J`Iu^MjhM5 z0fTM2q6Ws6?B8U8{J25@aOvC*OLo)UW~># z0df5LY>3<105G&A>Q4Dy^@HXL<|cq>Tlj2!5uu$z{QL9UvTU)Pe&}~?M@skYNAGLZC z92G&Z(xpG4crqSBosViB0A731Xg?6RTRV92;@QJ^G@qx)FO6)_nN2c{ctQM90YZ9` z!8|V!Ed5G>Rx9fQMB9NP!JzF%SgVrd4$RwzYrSpIy&?h`Ai?AU-U%mT1f+9TDFJ0B z!EAPQ;mMg}KfU+U2fx>^kXvi7g6VFcM}qHh4(bl8{dNg=ZnmB;Ij}qLXg=mS@R>bv zI^2>D1K5bhp-LZAOVgO3_>-QiORfd%Wf1xYdZ3XC8>y%l?ex`i+-@@N)Kqu%QbTA*_$)#E{ Zug(V0f$;3QanSw?@GoP;HFFpU0001+GHL(-