diff --git a/WiFiManager.cpp b/WiFiManager.cpp index d09d1bd..08d8bca 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -326,11 +326,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { if((String)_hostname != ""){ #ifdef WM_DEBUG_LEVEL - #ifdef ESP8266 - DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),WiFi.hostname()); - #elif defined(ESP32) - DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),WiFi.getHostname()); - #endif + DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),getWiFiHostname()); #endif } return true; // connected success @@ -1196,9 +1192,10 @@ void WiFiManager::handleRoot() { #endif if (captivePortal()) return; // If captive portal redirect instead of displaying the page handleRequest(); - String page = getHTTPHead(FPSTR(S_options)); // @token options @todo replace options with title - String str = FPSTR(HTTP_ROOT_MAIN); - str.replace(FPSTR(T_v),configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading + String page = getHTTPHead(_title); // @token options @todo replace options with title + String str = FPSTR(HTTP_ROOT_MAIN); // @todo custom title + str.replace(FPSTR(T_t),_title); + str.replace(FPSTR(T_v),configPortalActive ? _apName : (getWiFiHostname() + " - " + WiFi.localIP().toString())); // use ip if ap is not active for heading @todo use hostname? page += str; page += FPSTR(HTTP_PORTAL_OPTIONS); page += getMenuOut(); @@ -1307,7 +1304,7 @@ String WiFiManager::getMenuOut(){ String page; for(auto menuId :_menuIds ){ - if(((String)menuId == "param") && (_paramsCount == 0)) continue; // no params set, omit params from menu, @todo this may be undesired by someone + if((String)_menutokens[menuId] == "param" && _paramsCount == 0) continue; // no params set, omit params from menu, @todo this may be undesired by someone, use only menu to force? page += HTTP_PORTAL_MENU[menuId]; } @@ -2468,6 +2465,11 @@ void WiFiManager::setDebugOutput(boolean debug) { if(_debug && _debugLevel == DEBUG_DEV) debugPlatformInfo(); } +void WiFiManager::setDebugOutput(boolean debug, String prefix) { + _debugPrefix = prefix; + setDebugOutput(debug); +} + /** * [setAPStaticIPConfig description] * @access public @@ -2800,6 +2802,23 @@ bool WiFiManager::getWebPortalActive(){ return webPortalActive; } + +String WiFiManager::getWiFiHostname(){ + #ifdef ESP32 + return (String)WiFi.getHostname(); + #else + return (String)WiFi.hostname(); + #endif +} + +/** + * [setTitle description] + * @param String title, set app title + */ +void WiFiManager::setTitle(String title){ + _title = title; +} + /** * set menu items and order * if param is present in menu , params will be removed from wifi page automatically @@ -3015,7 +3034,7 @@ void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text,Genericb textb) { _debugPort.printf("[MEM] free: %5d | max: %5d | frag: %3d%% \n", free, max, frag); #endif } - _debugPort.print("*WM: "); + _debugPort.print(_debugPrefix); if(_debugLevel >= debugLvlShow) _debugPort.print("["+(String)level+"] "); _debugPort.print(text); if(textb){ @@ -3484,9 +3503,10 @@ void WiFiManager::handleUpdate() { DEBUG_WM(DEBUG_VERBOSE,F("<- Handle update")); #endif if (captivePortal()) return; // If captive portal redirect instead of displaying the page - String page = getHTTPHead(FPSTR(S_options)); // @token options + String page = getHTTPHead(_title); // @token options String str = FPSTR(HTTP_ROOT_MAIN); - str.replace(FPSTR(T_v), configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading + str.replace(FPSTR(T_t), _title); + str.replace(FPSTR(T_v), configPortalActive ? _apName : (getWiFiHostname() + " - " + WiFi.localIP().toString())); // use ip if ap is not active for heading page += str; page += FPSTR(HTTP_UPDATE); diff --git a/WiFiManager.h b/WiFiManager.h index 5003af1..9729fab 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -236,7 +236,8 @@ class WiFiManager // toggle debug output void setDebugOutput(boolean debug); - + void setDebugOutput(boolean debug, String prefix); // log line prefix, default "*wm:" + //set min quality percentage to include in scan, defaults to 8% if not specified void setMinimumSignalQuality(int quality = 8); @@ -316,6 +317,9 @@ class WiFiManager void setMenu(std::vector& menu); void setMenu(const char* menu[], uint8_t size); + // set the webapp title, default WiFiManager + void setTitle(String title); + // add params to its own menu page and remove from wifi, NOT TO BE COMBINED WITH setMenu! void setParamsPage(bool enable); @@ -370,6 +374,10 @@ class WiFiManager // to preload autoconnect for test fixtures or other uses that skip esp sta config bool preloadWiFi(String ssid, String pass); + // get hostname helper + String getWiFiHostname(); + + std::unique_ptr dnsServer; #if defined(ESP32) && defined(WM_WEBSERVERSHIM) @@ -461,6 +469,7 @@ class WiFiManager const char* _customHeadElement = ""; // store custom head element html from user String _bodyClass = ""; // class to add to body + String _title = FPSTR(S_brand); // app title - default WiFiManager // internal options @@ -599,7 +608,8 @@ class WiFiManager } wm_debuglevel_t; boolean _debug = true; - + String _debugPrefix = FPSTR(S_debugPrefix); + wm_debuglevel_t debugLvlShow = DEBUG_VERBOSE; // at which level start showing [n] level tags // build debuglevel support diff --git a/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino index b96b067..cb5b475 100644 --- a/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino +++ b/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -44,6 +44,7 @@ void configModeCallback (WiFiManager *myWiFiManager) { // Serial.println(WiFi.softAPIP()); //if you used auto generated SSID, print it // Serial.println(myWiFiManager->getConfigPortalSSID()); + esp_wifi_set_bandwidth(WIFI_IF_AP, WIFI_BW_HT20); } void saveParamCallback(){ diff --git a/examples/Tests/wificountry/wificountry.ino b/examples/Tests/wificountry/wificountry.ino new file mode 100644 index 0000000..da9d2c9 --- /dev/null +++ b/examples/Tests/wificountry/wificountry.ino @@ -0,0 +1,45 @@ +#include +#include +#include + +WiFiClient espClient; +PubSubClient client(espClient); +char mqtt_server[40]; +void mqttCallback(char* topic, byte* payload, unsigned int length) { + // message received +} + +void mqttReconnect() { + // reconnect code from PubSubClient example +} + +void setup() { + Serial.begin(9600); + WiFiManager wifiManager; + wifiManager.setTimeout(180); + + if(!wifiManager.autoConnect("AutoConnectAP")) { + Serial.println("failed to connect and hit timeout"); + delay(3000); + ESP.restart(); + delay(5000); + } + // id/name, placeholder/prompt, default, length +WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); +wifiManager.addParameter(&custom_mqtt_server); + +const char* mqtt_server = custom_mqtt_server.getValue(); + + Serial.println("connected...yeey :)"); + + client.setServer(mqtt_server, 1883); + client.setCallback(mqttCallback); +} + +void loop() { + if (!client.connected()) { + mqttReconnect(); + } + client.loop(); + yield(); +} \ No newline at end of file diff --git a/strings_en.h b/strings_en.h index 39c725e..995cec6 100644 --- a/strings_en.h +++ b/strings_en.h @@ -32,7 +32,7 @@ const char HTTP_SCRIPT[] PROGMEM = "