diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 29fb5f0..99690ff 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -5,24 +5,128 @@ #include "PersWiFiManager.h" +#if defined(ESP32) +#include +#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) PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d) { +#elif defined(ESP32) +PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d) { +#endif _server = &s; _dnsServer = &d; _apPass = ""; + _freshConnectionAttempt = false; } //PersWiFiManager 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 { - WiFi.begin(); + if((getSsid() == "") && (WiFi.status() != WL_CONNECTED)) { // No saved credentials, so skip trying to connect + _connectStartTime = millis(); + _freshConnectionAttempt = true; + return false; + } else { + WiFi.begin(); + } } //if in nonblock mode, skip this loop @@ -45,17 +149,18 @@ 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 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(){ //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)); _apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str()); @@ -67,7 +172,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 @@ -90,8 +195,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 +224,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,14 +244,43 @@ void PersWiFiManager::setupWiFiHandlers() { }//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 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"; +#elif defined(ESP32) + return _apSsid.length() ? _apSsid : "ESP32"; +#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 80fcf03..d801b86 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 = ""); @@ -21,8 +32,12 @@ class PersWiFiManager { bool begin(const String& ssid = "", const String& pass = ""); + void resetSettings(); + String getApSsid(); + String getSsid(); + void setApCredentials(const String& apSsid, const String& apPass = ""); void setConnectNonBlock(bool b); @@ -36,12 +51,17 @@ class PersWiFiManager { void onAp(WiFiChangeHandlerFunction fn); private: +#if defined(ESP8266) ESP8266WebServer * _server; +#elif defined(ESP32) + WebServer * _server; +#endif DNSServer * _dnsServer; String _apSsid, _apPass; bool _connectNonBlock; unsigned long _connectStartTime; + bool _freshConnectionAttempt; WiFiChangeHandlerFunction _connectHandler; WiFiChangeHandlerFunction _apHandler; diff --git a/examples/basic_rest_api/basic_rest_api.ino b/examples/basic_rest_api/basic_rest_api.ino index b2f22dc..5b931f0 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); @@ -96,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([]() { @@ -138,8 +158,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