update wifi manager

This commit is contained in:
technyon
2023-01-26 18:38:10 +01:00
parent 8ad0aa517d
commit a62017883a
19 changed files with 1113 additions and 741 deletions

View File

@@ -23,7 +23,7 @@ WiFiManager wm;
// TEST OPTION FLAGS
bool TEST_CP = true; // always start the configportal, even if ap found
bool TEST_CP = false; // always start the configportal, even if ap found
int TESP_CP_TIMEOUT = 90; // test cp timeout
bool TEST_NET = true; // do a network test after connect, (gets ntp time)
@@ -34,6 +34,23 @@ bool WMISBLOCKING = true; // use blocking or non blocking mode, non global pa
// char ssid[] = "*************"; // your network SSID (name)
// char pass[] = "********"; // your network password
//callbacks
// called after AP mode and config portal has started
// setAPCallback( std::function<void(WiFiManager*)> func );
// called after webserver has started
// setWebServerCallback( std::function<void()> func );
// called when settings reset have been triggered
// setConfigResetCallback( std::function<void()> func );
// called when wifi settings have been changed and connection was successful ( or setBreakAfterConfig(true) )
// setSaveConfigCallback( std::function<void()> func );
// called when saving either params-in-wifi or params page
// setSaveParamsCallback( std::function<void()> func );
// called when saving params-in-wifi or params before anything else happens (eg wifi)
// setPreSaveConfigCallback( std::function<void()> func );
// called just before doing OTA update
// setPreOtaUpdateCallback( std::function<void()> func );
void saveWifiCallback(){
Serial.println("[CALLBACK] saveCallback fired");
}
@@ -64,15 +81,19 @@ void handleRoute(){
wm.server->send(200, "text/plain", "hello from user code");
}
void handlePreOtaUpdateCallback(){
Update.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("CUSTOM Progress: %u%%\r", (progress / (total / 100)));
});
}
void setup() {
// WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// WiFi.mode(WIFI_STA); // explicitly set mode, esp can default to STA+AP
// put your setup code here, to run once:
Serial.begin(115200);
// Serial1.begin(115200);
// Serial.setDebugOutput(true);
delay(1000);
// Serial.setDebugOutput(true);
Serial.println("\n Starting");
// WiFi.setSleepMode(WIFI_NONE_SLEEP); // disable sleep, can improve ap stability
@@ -83,20 +104,27 @@ void setup() {
Serial.println("[ERROR] TEST");
Serial.println("[INFORMATION] TEST");
// WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // wifi_scan_method_t scanMethod
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // wifi_sort_method_t sortMethod - WIFI_CONNECT_AP_BY_SIGNAL,WIFI_CONNECT_AP_BY_SECURITY
// WiFi.setMinSecurity(WIFI_AUTH_WPA2_PSK);
wm.setDebugOutput(true);
wm.debugPlatformInfo();
//reset settings - for testing
// wm.resetSettings();
// wm.erase();
// wm.erase();
// setup some parameters
WiFiManagerParameter custom_html("<p style=\"color:pink;font-weight:Bold;\">This Is Custom HTML</p>"); // only custom html
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "", 6);
WiFiManagerParameter custom_token("api_token", "api token", "", 16);
WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "", 0); // id is invalid, cannot contain spaces
WiFiManagerParameter custom_ipaddress("input_ip", "input IP", "", 15,"pattern='\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}'"); // custom input attrs (ip mask)
WiFiManagerParameter custom_input_type("input_pwd", "input pass", "", 15,"type='password'"); // custom input attrs (ip mask)
const char _customHtml_checkbox[] = "type=\"checkbox\"";
WiFiManagerParameter custom_checkbox("my_checkbox", "My Checkbox", "T", 2, _customHtml_checkbox,WFM_LABEL_AFTER);
@@ -128,6 +156,7 @@ void setup() {
wm.setWebServerCallback(bindServerCallback);
wm.setSaveConfigCallback(saveWifiCallback);
wm.setSaveParamsCallback(saveParamCallback);
wm.setPreOtaUpdateCallback(handlePreOtaUpdateCallback);
// add all your parameters here
wm.addParameter(&custom_html);
@@ -137,6 +166,7 @@ void setup() {
wm.addParameter(&custom_tokenb);
wm.addParameter(&custom_ipaddress);
wm.addParameter(&custom_checkbox);
wm.addParameter(&custom_input_type);
wm.addParameter(&custom_html_inputs);
@@ -144,11 +174,21 @@ void setup() {
custom_html.setValue("test",4);
custom_token.setValue("test",4);
// set custom html head content , inside <head>
const char* headhtml = "<meta name='color-scheme' content='dark light'><style></style><script></script>";
wm.setCustomHeadElement(headhtml);
// const char* icon = "
// <link rel='icon' type='image/png' sizes='16x16' href='data:image/png;base64,
// iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAMFBMVEU0OkArMjhobHEoPUPFEBIu
// O0L+AAC2FBZ2JyuNICOfGx7xAwTjCAlCNTvVDA1aLzQ3COjMAAAAVUlEQVQI12NgwAaCDSA0888G
// CItjn0szWGBJTVoGSCjWs8TleQCQYV95evdxkFT8Kpe0PLDi5WfKd4LUsN5zS1sKFolt8bwAZrCa
// GqNYJAgFDEpQAAAzmxafI4vZWwAAAABJRU5ErkJggg==' />";
// set custom html menu content , inside <head>
// set custom html head content , inside <head>
// examples of favicon, or meta tags etc
// const char* headhtml = "<link rel='icon' type='image/png' href='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==' />";
// const char* headhtml = "<meta name='color-scheme' content='dark light'><style></style><script></script>";
// wm.setCustomHeadElement(headhtml);
// set custom html menu content , inside menu item "custom", see setMenu()
const char* menuhtml = "<form action='/custom' method='get'><button>Custom</button></form><br/>\n";
wm.setCustomMenuHTML(menuhtml);
@@ -186,7 +226,8 @@ void setup() {
// set Hostname
wm.setHostname(("WM_"+wm.getDefaultAPName()).c_str());
wm.setHostname(("WM_"+wm.getDefaultAPName()).c_str());
// wm.setHostname("WM_RANDO_1234");
// set custom channel
// wm.setWiFiAPChannel(13);
@@ -271,11 +312,13 @@ void setup() {
}
void wifiInfo(){
// can contain gargbage on esp32 if wifi is not ready yet
Serial.println("[WIFI] WIFI INFO DEBUG");
// WiFi.printDiag(Serial);
Serial.println("[WIFI] SAVED: " + (String)(wm.getWiFiIsSaved() ? "YES" : "NO"));
Serial.println("[WIFI] SSID: " + (String)wm.getWiFiSSID());
Serial.println("[WIFI] PASS: " + (String)wm.getWiFiPass());
Serial.println("[WIFI] HOSTNAME: " + (String)WiFi.getHostname());
}
void loop() {
@@ -348,7 +391,7 @@ void getTime() {
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo); // @NOTE doesnt work in esp2.3.0
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}