/** * IotWebConf01Minimal.ino -- IotWebConf is an ESP8266/ESP32 * non blocking WiFi/AP web configuration library for Arduino. * https://github.com/prampec/IotWebConf * * Copyright (C) 2018 Balazs Kelemen * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ /** * Example: Custom HTML * Description: * This example demostrates how to override the default look and feel of the * config portal. * * The first customalization is to add a logo to the config page. * * The second customalization is a special javascript, that detects all * password fields, and adds a small button after each of them with a * lock on it. By pressing the lock the password became visible/hidden. */ #include // -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. const char thingName[] = "testThing"; // -- Initial password to connect to the Thing, when it creates an own Access Point. const char wifiInitialApPassword[] = "smrtTHNG8266"; DNSServer dnsServer; WebServer server(80); IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); // -- Javascript block will be added to the header. const char CUSTOMHTML_SCRIPT_INNER[] PROGMEM = "\n\ document.addEventListener('DOMContentLoaded', function(event) {\n\ let elements = document.querySelectorAll('input[type=\"password\"]');\n\ for (let p of elements) {\n\ let btn = document.createElement('INPUT'); btn.type = 'button'; btn.value = '🔓'; btn.style.width = 'auto'; p.style.width = '83%'; p.parentNode.insertBefore(btn,p.nextSibling);\n\ btn.onclick = function() { if (p.type === 'password') { p.type = 'text'; btn.value = '🔒'; } else { p.type = 'password'; btn.value = '🔓'; } }\n\ };\n\ });\n"; // -- HTML element will be added inside the body element. const char CUSTOMHTML_BODY_INNER[] PROGMEM = "
\n"; // -- This is an OOP technique to override behaviour of the existing // IotWebConfHtmlFormatProvider. Here two method are overriden from // the original class. See IotWebConf.h for all potentially overridable // methods of IotWebConfHtmlFormatProvider . class CustomHtmlFormatProvider : public IotWebConfHtmlFormatProvider { protected: String getScriptInner() override { return IotWebConfHtmlFormatProvider::getScriptInner() + String(FPSTR(CUSTOMHTML_SCRIPT_INNER)); } String getBodyInner() override { return String(FPSTR(CUSTOMHTML_BODY_INNER)) + IotWebConfHtmlFormatProvider::getBodyInner(); } }; // -- An instance must be created from the class defined above. CustomHtmlFormatProvider customHtmlFormatProvider; void setup() { Serial.begin(115200); Serial.println(); Serial.println("Starting up..."); // -- Applying the new HTML format to IotWebConf. iotWebConf.setHtmlFormatProvider(&customHtmlFormatProvider); iotWebConf.init(); // -- Set up required URL handlers on the web server. server.on("/", handleRoot); server.on("/config", []{ iotWebConf.handleConfig(); }); server.onNotFound([](){ iotWebConf.handleNotFound(); }); Serial.println("Ready."); } void loop() { // -- doLoop should be called as frequently as possible. iotWebConf.doLoop(); } /** * Handle web requests to "/" path. */ void handleRoot() { // -- Let IotWebConf test and handle captive portal requests. if (iotWebConf.handleCaptivePortal()) { // -- Captive portal request were already served. return; } String s = ""; s += "IotWebConf 10 Custom HTML"; s += FPSTR(CUSTOMHTML_BODY_INNER); s += "Go to configure page to change settings."; s += "\n"; server.send(200, "text/html; charset=UTF-8", s); }