From 755aca16430b6f6f99d54ab01ca39b75e102b699 Mon Sep 17 00:00:00 2001 From: tablatronix <807787+tablatronix@users.noreply.github.com> Date: Fri, 4 Feb 2022 22:04:10 -0600 Subject: [PATCH] #1348 just change _hostname to string ? --- WiFiManager.cpp | 46 +++++++++++++------ WiFiManager.h | 3 +- .../OnDemandConfigPortal.ino | 3 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 1288c8f..ff02a60 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -298,7 +298,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { WiFi_autoReconnect(); // set hostname before stating - if((String)_hostname != ""){ + if(_hostname != ""){ setupHostname(true); } @@ -325,7 +325,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { #endif _lastconxresult = WL_CONNECTED; - if((String)_hostname != ""){ + if(_hostname != ""){ #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),getWiFiHostname()); #endif @@ -358,7 +358,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { } bool WiFiManager::setupHostname(bool restart){ - if((String)_hostname == "") { + if(_hostname == "") { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("No Hostname to set")); #endif @@ -366,34 +366,39 @@ bool WiFiManager::setupHostname(bool restart){ } else { #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_DEV,F("setupHostname: "),_hostname); + DEBUG_WM(DEBUG_VERBOSE,F("Setting Hostnames: "),_hostname); #endif } bool res = true; #ifdef ESP8266 - #ifdef WM_DEBUG_LEVEL + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Setting WiFi hostname")); #endif - res = WiFi.hostname(_hostname); + res = WiFi.hostname(_hostname.c_str()); // #ifdef ESP8266MDNS_H #ifdef WM_MDNS - #ifdef WM_DEBUG_LEVEL + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Setting MDNS hostname, tcp 80")); #endif - if(MDNS.begin(_hostname)){ + if(MDNS.begin(_hostname.c_str())){ MDNS.addService("http", "tcp", 80); } #endif #elif defined(ESP32) // @note hostname must be set after STA_START delay(200); // do not remove, give time for STA_START - res = WiFi.setHostname(_hostname); + + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Setting WiFi hostname")); + #endif + + res = WiFi.setHostname(_hostname.c_str()); // #ifdef ESP32MDNS_H #ifdef WM_MDNS - #ifdef WM_DEBUG_LEVEL - DEBUG_WM(DEBUG_VERBOSE,F("Setting MDNS hostname, tcp 80")); - #endif - if(MDNS.begin(_hostname)){ + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Setting MDNS hostname, tcp 80")); + #endif + if(MDNS.begin(_hostname.c_str())){ MDNS.addService("http", "tcp", 80); } #endif @@ -491,8 +496,8 @@ bool WiFiManager::startAP(){ // set ap hostname #ifdef ESP32 - if(ret && (String)_hostname != ""){ - bool res = WiFi.softAPsetHostname(_hostname); + if(ret && _hostname != ""){ + bool res = WiFi.softAPsetHostname(_hostname.c_str()); #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("setting softAP Hostname:"),_hostname); if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: AP set failed!")); @@ -2848,6 +2853,12 @@ void WiFiManager::setEnableConfigPortal(boolean enable) * @return bool false if hostname is not valid */ bool WiFiManager::setHostname(const char * hostname){ + //@todo max length 32 + _hostname = String(hostname); + return true; +} + +bool WiFiManager::setHostname(String hostname){ //@todo max length 32 _hostname = hostname; return true; @@ -3020,6 +3031,11 @@ bool WiFiManager::getWiFiIsSaved(){ return WiFi_hasAutoConnect(); } +/** + * getDefaultAPName + * @since $dev + * @return string + */ String WiFiManager::getDefaultAPName(){ String hostString = String(WIFI_getChipId(),HEX); hostString.toUpperCase(); diff --git a/WiFiManager.h b/WiFiManager.h index 8022005..41c453b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -318,6 +318,7 @@ class WiFiManager // set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266 bool setHostname(const char * hostname); + bool setHostname(String hostname); // show erase wifi onfig button on info page, true void setShowInfoErase(boolean enabled); @@ -492,7 +493,7 @@ class WiFiManager boolean _showInfoUpdate = true; // info page update button boolean _showBack = false; // show back button boolean _enableConfigPortal = true; // use config portal if autoconnect failed - const char * _hostname = ""; // hostname for esp8266 for dhcp, and or MDNS + String _hostname = ""; // hostname for esp8266 for dhcp, and or MDNS const char* _customHeadElement = ""; // store custom head element html from user String _bodyClass = ""; // class to add to body diff --git a/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino index b34413f..f1fbf1a 100644 --- a/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino +++ b/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -151,7 +151,8 @@ void setup() { wm.setCountry("US"); // set Hostname - wm.setHostname("WIFIMANAGERTESTING"); + + wm.setHostname(("WM_"+wm.getDefaultAPName()).c_str()); // set custom channel // wm.setWiFiAPChannel(13);