This commit is contained in:
tablatronix
2021-03-09 11:35:22 -06:00
5 changed files with 94 additions and 16 deletions
+32 -12
View File
@@ -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);
+12 -2
View File
@@ -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<const char*>& 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> 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
@@ -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(){
@@ -0,0 +1,45 @@
#include <WiFiManager.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
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();
}
+4 -2
View File
@@ -32,7 +32,7 @@ const char HTTP_SCRIPT[] PROGMEM = "<script>function c(l){"
const char HTTP_HEAD_END[] PROGMEM = "</head><body class='{c}'><div class='wrap'>"; // {c} = _bodyclass
// example of embedded logo, base64 encoded inline, No styling here
// const char HTTP_ROOT_MAIN[] PROGMEM = "<img title=' alt=' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAADQElEQVRoQ+2YjW0VQQyE7Q6gAkgFkAogFUAqgFQAVACpAKiAUAFQAaECQgWECggVGH1PPrRvn3dv9/YkFOksoUhhfzwz9ngvKrc89JbnLxuA/63gpsCmwCADWwkNEji8fVNgotDM7osI/x777x5l9F6JyB8R4eeVql4P0y8yNsjM7KGIPBORp558T04A+CwiH1UVUItiUQmZ2XMReSEiAFgjAPBeVS96D+sCYGaUx4cFbLfmhSpnqnrZuqEJgJnd8cQplVLciAgX//Cf0ToIeOB9wpmloLQAwpnVmAXgdf6pwjpJIz+XNoeZQQZlODV9vhc1Tuf6owrAk/8qIhFbJH7eI3eEzsvydQEICqBEkZwiALfF70HyHPpqScPV5HFjeFu476SkRA0AzOfy4hYwstj2ZkDgaphE7m6XqnoS7Q0BOPs/sw0kDROzjdXcCMFCNwzIy0EcRcOvBACfh4k0wgOmBX4xjfmk4DKTS31hgNWIKBCI8gdzogTgjYjQWFMw+o9LzJoZ63GUmjWm2wGDc7EvDDOj/1IVMIyD9SUAL0WEhpriRlXv5je5S+U1i2N88zdPuoVkeB+ls4SyxCoP3kVm9jsjpEsBLoOBNC5U9SwpGdakFkviuFP1keblATkTENTYcxkzgxTKOI3jyDxqLkQT87pMA++H3XvJBYtsNbBN6vuXq5S737WqHkW1VgMQNXJ0RshMqbbT33sJ5kpHWymzcJjNTeJIymJZtSQd9NHQHS1vodoFoTMkfbJzpRnLzB2vi6BZAJxWaCr+62BC+jzAxVJb3dmmiLzLwZhZNPE5e880Suo2AZgB8e8idxherqUPnT3brBDTlPxO3Z66rVwIwySXugdNd+5ejhqp/+NmgIwGX3Py3QBmlEi54KlwmjkOytQ+iJrLJj23S4GkOeecg8G091no737qvRRdzE+HLALQoMTBbJgBsCj5RSWUlUVJiZ4SOljb05eLFWgoJ5oY6yTyJp62D39jDANoKKcSocPJD5dQYzlFAFZJflUArgTPZKZwLXAnHmerfJquUkKZEgyzqOb5TuDt1P3nwxobqwPocZA11m4A1mBx5IxNgRH21ti7KbAGiyNn3HoF/gJ0w05A8xclpwAAAABJRU5ErkJggg==' /><h1>{v}</h1><h3>WiFiManager</h3>";
const char HTTP_ROOT_MAIN[] PROGMEM = "<h1>{v}</h1><h3>WiFiManager</h3>";
const char HTTP_ROOT_MAIN[] PROGMEM = "<h1>{t}</h1><h3>{v}</h3>";
const char * const HTTP_PORTAL_MENU[] PROGMEM = {
"<form action='/wifi' method='get'><button>Configure WiFi</button></form><br/>\n", // MENU_WIFI
"<form action='/0wifi' method='get'><button>Configure WiFi (No Scan)</button></form><br/>\n", // MENU_WIFINOSCAN
@@ -201,7 +201,9 @@ const char HTTP_INFO_stamac[] PROGMEM = "<dt>Station MAC</dt><dd>{1}</dd>";
const char HTTP_INFO_conx[] PROGMEM = "<dt>Connected</dt><dd>{1}</dd>";
const char HTTP_INFO_autoconx[] PROGMEM = "<dt>Autoconnect</dt><dd>{1}</dd>";
// Strings
const char S_brand[] PROGMEM = "WiFiManager";
const char S_debugPrefix[] PROGMEM = "*wm:";
const char S_y[] PROGMEM = "Yes";
const char S_n[] PROGMEM = "No";
const char S_enable[] PROGMEM = "Enabled";