123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include "wifimgr.h"
- fs::SPIFFSFS& FlashFS = SPIFFS;
- #define CONFIG_FILE "/config.json"
- const char hostname[] = "esp32";
- std::vector<String> 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": "<hr style=\"height:1px;border-width:0;color:gray;background-color:#52a6ed\">",
- "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": "<hr style=\"height:1px;border-width:0;color:gray;background-color:#52a6ed\">",
- "posterior": "par"
- },
- {
- "name": "save",
- "type": "ACSubmit",
- "value": "Save",
- "uri": "/save"
- },
- {
- "name": "adjust_width",
- "type": "ACElement",
- "value": "<script type=\"text/javascript\">window.onload=function(){var t=document.querySelectorAll(\"input[type='text']\");for(i=0;i<t.length;i++){var e=t[i].getAttribute(\"placeholder\");e&&t[i].setAttribute(\"size\",e.length*.8)}};</script>"
- }
- ]
- }
- )";
- 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 = "<head> <meta http-equiv=\"refresh\" content=\"0; URL=/config\" /></head>";
- content += AUTOCONNECT_LINK(COG_24);
- server.send(200, "text/html", content);
- });
- server.on("/reset", []() {
- String content = "<head> <meta http-equiv=\"refresh\" content=\"15; URL=/config\" /></head>";
- 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<AutoConnectInput>();
- aux["validated"].value = input.isValid() ? String() : String("Hostname data pattern missmatched.");
- input = configAux["OSC_TARGET_IP"].as<AutoConnectInput>();
- 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();
- }
|