diff --git a/WiFiManager.cpp b/WiFiManager.cpp index a9f7e51c..994a7619 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -166,6 +166,14 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { // so we must force it on else, if not connectimeout then waitforconnectionresult gets stuck endless loop WiFi_autoReconnect(); + if(_hostname != ""){ + #ifdef ESP8266 + WiFi.hostname(_hostname); + #elif defined(ESP32) + WiFi.setHostname(_hostname); + #endif + } + // if already connected, or try stored connect if (WiFi.status() == WL_CONNECTED || connectWifi("", "") == WL_CONNECTED) { //connected @@ -208,7 +216,13 @@ bool WiFiManager::startAP(){ if ( _apcallback != NULL) { _apcallback(this); } - + + #ifdef ESP32 + if(ret && _hostname != ""){ + WiFi.softAPsetHostname(_hostname); + } + #endif + return ret; } @@ -279,6 +293,7 @@ void WiFiManager::setupConfigPortal() { server->on((String)F("/i"), std::bind(&WiFiManager::handleInfo, this)); server->on((String)F("/r"), std::bind(&WiFiManager::handleReset, this)); server->on((String)F("/exit"), std::bind(&WiFiManager::handleExit, this)); + // server->on((String)F("/exit"), std::bind(&WiFiManager::stopCaptivePortal, this)); server->on((String)F("/erase"), std::bind(&WiFiManager::handleErase, this)); server->on((String)F("/status"), std::bind(&WiFiManager::handleWiFiStatus, this)); //server->on("/fwlink", std::bind(&WiFiManager::handleRoot, this)); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler. @@ -439,8 +454,8 @@ boolean WiFiManager::stopConfigPortal(){ DEBUG_WM(F("disconnect configportal")); bool ret = WiFi.softAPdisconnect(false); if(!ret)DEBUG_WM(F("disconnect configportal - softAPdisconnect failed")); - // WiFi_Mode(_usermode); // restore users wifi mode - DEBUG_WM("uswermode",_usermode); + // WiFi_Mode(_usermode); // restore users wifi mode, BUG https://github.com/esp8266/Arduino/issues/4372 + // DEBUG_WM("usermode",_usermode); configPortalActive = false; return ret; } @@ -494,6 +509,15 @@ int WiFiManager::connectWifi(String ssid, String pass) { } _lastconxresult = connRes; + + if(connRes == WL_CONNECTED && _hostname != ""){ + #ifdef ESP8266 + WiFi.hostname(_hostname); + #elif defined(ESP32) + WiFi.setHostname(_hostname); + #endif + } + return connRes; } @@ -1295,6 +1319,10 @@ boolean WiFiManager::captivePortal() { return false; } +void WiFiManager::stopCaptivePortal(){ + _enableCaptivePortal= false; +} + void WiFiManager::reportStatus(String &page){ String str; if (WiFi_SSID() != ""){ @@ -1442,6 +1470,11 @@ uint8_t WiFiManager::getLastConxResult(){ return _lastconxresult; } +bool WiFiManager::setHostname(const char * hostname){ + //@todo max length 32 + _hostname = hostname; +} + // HELPERS template diff --git a/WiFiManager.h b/WiFiManager.h index 7dd3f83e..51e40c83 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -177,6 +177,7 @@ class WiFiManager uint8_t getLastConxResult(); // get a status as string String getWLStatusString(uint8_t status); + bool setHostname(const char * hostname); private: std::unique_ptr dnsServer; @@ -211,6 +212,7 @@ class WiFiManager String _wifissidprefix = FPSTR(S_ssidpre); // auto apname prefix prefix+chipid uint8_t _lastconxresult = WL_IDLE_STATUS; + // option parameters int _minimumQuality = -1; // filter wifiscan ap by this rssi boolean _removeDuplicateAPs = true; // remove dup aps from wifiscan @@ -224,6 +226,7 @@ class WiFiManager boolean _cpClientCheck = false; // keep cp alive if cp have station boolean _webClientCheck = true; // keep cp alive if web have client boolean _scanDispOptions = false; // show percentage in scans not icons + const char * _hostname = ""; const char* _customHeadElement = ""; // store custom head element html from user @@ -252,6 +255,7 @@ class WiFiManager boolean configPortalHasTimeout(); boolean stopConfigPortal(); uint8_t handleConfigPortal(); + void stopCaptivePortal(); // wifi platform abstractions bool WiFi_Mode(WiFiMode_t m);