#include "wifimgr.h" fs::SPIFFSFS& FlashFS = SPIFFS; #define CONFIG_FILE "/config.json" const char hostname[] = "esp32"; std::vector const& config_elements = { "HOSTNAME", "OSC_TARGET_IP", "OSC_TARGET_PORT", "OSC_INPUT_PORT"} ; static const char PAGE_CONFIG[] PROGMEM = R"( { "uri": "/config", "title": "Sensor Configuration", "menu": true, "element": [ { "name": "tablecss", "type": "ACStyle", "value": "table{font-family:arial,sans-serif;border-collapse:collapse;width:100%;color:black;}td,th{border:1px solid #dddddd;text-align:center;padding:8px;}tr:nth-child(even){background-color:#dddddd;}" }, { "name": "hr", "type": "ACElement", "value": "
", "posterior": "par" }, { "name": "HOSTNAME", "type": "ACInput", "label": "Hostname", "placeholder": "This area accepts hostname patterns", "pattern": "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$" }, { "name": "OSC_SECTION_TEXT", "type": "ACText", "value": "OSC CONFIGURATION", "style": "font-family:Arial;font-size:18px;font-weight:400;color:#191970" }, { "name": "OSC_TARGET_IP", "type": "ACInput", "label": "OSC target IP", "placeholder": "This area accepts IP patterns" }, { "name": "OSC_TARGET_PORT", "type": "ACInput", "label": "OSC target port", "value": "", "apply": "number", "pattern": "\\d*" }, { "name": "OSC_INPUT_PORT", "type": "ACInput", "label": "OSC input port", "value": "", "apply": "number", "pattern": "\\d*" }, { "name": "hr2", "type": "ACElement", "value": "
", "posterior": "par" }, { "name": "save", "type": "ACSubmit", "value": "Save", "uri": "/save" }, { "name": "adjust_width", "type": "ACElement", "value": "" } ] } )"; static const char PAGE_SAVE[] PROGMEM = R"( { "uri": "/save", "title": "Elements", "menu": false, "element": [ { "name": "caption", "type": "ACText", "format": "Elements have been saved to %s", "style": "font-family:Arial;font-size:18px;font-weight:400;color:#191970" }, { "name": "validated", "type": "ACText", "style": "color:red" }, { "name": "echo", "type": "ACText", "style": "font-family:monospace;font-size:small;white-space:pre;" }, { "name": "ok", "type": "ACSubmit", "value": "Apply", "uri": "/reset" } ] } )"; WebServerClass server; AutoConnect portal(server); AutoConnectConfig config; AutoConnectAux configAux; AutoConnectAux saveAux; ///////////////////////// SETUP ////////////////////////////////// void setup_wifimgr(void) { // Responder of root page handled directly from WebServer class. server.on("/", []() { String content = " "; content += AUTOCONNECT_LINK(COG_24); server.send(200, "text/html", content); }); server.on("/reset", []() { String content = " "; content += "waiting 15s for sensor to reboot"; server.send(200, "text/html", content); delay(100); ESP.restart(); }); // Load a custom web page described in JSON as PAGE_ELEMENT and // register a handler. This handler will be invoked from // AutoConnectSubmit named the Load defined on the same page. configAux.load(FPSTR(PAGE_CONFIG)); configAux.on([] (AutoConnectAux& aux, PageArgument& arg) { if (portal.where() == "/config") { // Use the AutoConnect::where function to identify the referer. // Since this handler only supports AutoConnectSubmit called the // Load, it uses the uri of the custom web page placed to // determine whether the Load was called me or not. FlashFS.begin(); File param = FlashFS.open(CONFIG_FILE, "r"); if (param) { aux.loadElement(param, config_elements ); param.close(); } FlashFS.end(); } return String(); }); saveAux.load(FPSTR(PAGE_SAVE)); saveAux.on([] (AutoConnectAux& aux, PageArgument& arg) { // You can validate input values ​​before saving with // AutoConnectInput::isValid function. // Verification is using performed regular expression set in the // pattern attribute in advance. AutoConnectInput& input = configAux["hostname"].as(); aux["validated"].value = input.isValid() ? String() : String("Hostname data pattern missmatched."); input = configAux["OSC_TARGET_IP"].as(); aux["validated"].value = input.isValid() ? String() : String("IP data pattern missmatched."); // The following line sets only the value, but it is HTMLified as // formatted text using the format attribute. aux["caption"].value = CONFIG_FILE; FlashFS.begin(true); File param = FlashFS.open(CONFIG_FILE, "w"); if (param) { // Save as a loadable set for parameters. configAux.saveElement(param, config_elements); param.close(); // Read the saved elements again to display. param = FlashFS.open(CONFIG_FILE, "r"); aux["echo"].value = param.readString(); param.close(); } else { aux["echo"].value = "Filesystem failed to open."; } FlashFS.end(); return String(); }); //Read config file to start with good config values FlashFS.begin(); File param = FlashFS.open(CONFIG_FILE, "r"); if (param) { configAux.loadElement(param, config_elements ); param.close(); } FlashFS.end(); portal.join({ configAux, saveAux }); config.autoReconnect = true ; config.auth = AC_AUTH_NONE; config.authScope = AC_AUTHSCOPE_AUX; config.ticker = true; config.hostName = hostname; portal.config(config); portal.begin(); }