From fc4a9d03bbf41250addd6d2868a80dbaa38ba0a2 Mon Sep 17 00:00:00 2001 From: cgmckeever Date: Tue, 7 Jun 2022 17:38:10 -0500 Subject: [PATCH] basic config manager --- esp-alexa-relay/relay-a/v3/main/main.ino | 4 +- esp-rf/cc1101/multistation/v1/data/index.html | 26 ++++ esp-rf/cc1101/multistation/v1/data/main.js | 89 +++++++++++++ esp-rf/cc1101/multistation/v1/data/relay.html | 22 +++ esp-rf/cc1101/multistation/v1/data/reset.html | 13 ++ .../cc1101/multistation/v1/data/settings.html | 34 +++++ esp-rf/cc1101/multistation/v1/data/styles.css | 90 +++++++++++++ esp-rf/cc1101/multistation/v1/v1.ino | 125 ++++++++++++++++++ 8 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 esp-rf/cc1101/multistation/v1/data/index.html create mode 100644 esp-rf/cc1101/multistation/v1/data/main.js create mode 100644 esp-rf/cc1101/multistation/v1/data/relay.html create mode 100644 esp-rf/cc1101/multistation/v1/data/reset.html create mode 100644 esp-rf/cc1101/multistation/v1/data/settings.html create mode 100644 esp-rf/cc1101/multistation/v1/data/styles.css diff --git a/esp-alexa-relay/relay-a/v3/main/main.ino b/esp-alexa-relay/relay-a/v3/main/main.ino index f3bc5c6..739d08a 100644 --- a/esp-alexa-relay/relay-a/v3/main/main.ino +++ b/esp-alexa-relay/relay-a/v3/main/main.ino @@ -31,7 +31,7 @@ const byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; template void debug(T &msg, bool newline = false) { DEBUG_MODE = true; - Serial.begin(112500); + Serial.begin(115200); DebugPrint(msg); if (newline) DebugPrintln(F("")); Serial.flush(); @@ -42,7 +42,7 @@ void debug(T &msg, bool newline = false) { // void configSetup() { DEBUG_MODE = true; - Serial.begin(112500); + Serial.begin(115200); // randomSeed(*(volatile uint32_t *)0x3FF20E44); // String sApName = "ESPRELAY-" + String(random(111, 999)); diff --git a/esp-rf/cc1101/multistation/v1/data/index.html b/esp-rf/cc1101/multistation/v1/data/index.html new file mode 100644 index 0000000..3e62444 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/index.html @@ -0,0 +1,26 @@ + + + + + + ConfigManager + + +
+

Wifi Details

+
+
+ + +
+
+ + +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/esp-rf/cc1101/multistation/v1/data/main.js b/esp-rf/cc1101/multistation/v1/data/main.js new file mode 100644 index 0000000..8065578 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/main.js @@ -0,0 +1,89 @@ +$( document ).ready(function() { + + // get the config store + $.ajax({ + url: '/settings', + success: function(data) { + $.each(data, function(key, value, data) { + var selector = '[name="' + key + '"]'; + var target = $(selector); + if (target) { + console.log(selector); + if ( target.is("input") ) { + target.val(value); + } else { + if (key == "isTriggered") { + if (value) { + value = "On"; + var button = $('button[value="on"]') + } else { + value = "Off" + var button = $('button[value="off"]') + } + button.css("color", "grey"); + } + target.html(value); + } + } + }); + } + }); + + var validationSettings = { + rules: { + deviceName: { + required: true + }, + inchingDelay: { + required: true + }, + ledPin: { + required: true + } + } + }; + + $.fn.serializeObject = function() { + var o = {}; + var a = this.serializeArray(); + $.each(a, function() { + var input = $("[name='" + this.name + "']"); + var value = this.value; + var dataType = input[0].getAttribute("data-type"); + if (dataType == "number") value = parseFloat(value); + + if (o[this.name]) { + if (!o[this.name].push) { + o[this.name] = [o[this.name]]; + } + o[this.name].push(value || ''); + } else { + o[this.name] = value || ''; + } + }); + return o; + }; + + var settingForm = '[name="settingsForm"]'; + $(settingForm).validate(validationSettings); + $(settingForm).on('submit', function(e) { + document.getElementById("status").innerHTML = "" + if($(this).valid()) { + e.preventDefault(); + var formData = $(this).serializeObject(); + + $.ajax({ + url: '/settings', + type: 'PUT', + data: JSON.stringify(formData), + contentType: 'application/json', + success: function(data) { + console.log(formData); + document.getElementById("status").innerHTML = "Settings Updated" + } + }); + + return false; + } + }); +}); \ No newline at end of file diff --git a/esp-rf/cc1101/multistation/v1/data/relay.html b/esp-rf/cc1101/multistation/v1/data/relay.html new file mode 100644 index 0000000..e4080e4 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/relay.html @@ -0,0 +1,22 @@ + + + + + + + ESP8266 Relay + + +
+

ESP Relay

+



+ +
+ + +
+



+ Relay State: --- +
+ + \ No newline at end of file diff --git a/esp-rf/cc1101/multistation/v1/data/reset.html b/esp-rf/cc1101/multistation/v1/data/reset.html new file mode 100644 index 0000000..2e63893 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/reset.html @@ -0,0 +1,13 @@ + + + + + + Reset + + +
+

Reset

+
+ + \ No newline at end of file diff --git a/esp-rf/cc1101/multistation/v1/data/settings.html b/esp-rf/cc1101/multistation/v1/data/settings.html new file mode 100644 index 0000000..6834947 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/settings.html @@ -0,0 +1,34 @@ + + + + + + + + + ConfigManager + + +
+

Settings

+
+
+ + +
+
+ + +
+
+ + +
+
+

+ +
+
+
+ + diff --git a/esp-rf/cc1101/multistation/v1/data/styles.css b/esp-rf/cc1101/multistation/v1/data/styles.css new file mode 100644 index 0000000..a7de453 --- /dev/null +++ b/esp-rf/cc1101/multistation/v1/data/styles.css @@ -0,0 +1,90 @@ +body { + color: #434343; + font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; + font-size: 14px; + line-height: 1.42857142857143; + padding: 20px; +} +.container { + margin: 0 auto; + max-width: 400px; +} +form .error { + color: #8A1F11 !important; + border-color: #8A1F11; +} +label.error { + text-transform: none +} +p.error { + margin-bottom: 10px +} +p.inline-errors, p.error { + background: none + border-color: none + border: none + clear: both + font-size: 12px +} +form .field-group { + box-sizing: border-box; + clear: both; + padding: 4px 0; + position: relative; + margin: 1px 0; + width: 100%; +} +form .field-group > label { + color: #757575; + display: block; + margin: 0 0 5px 0; + padding: 5px 0 0; + position: relative; + word-wrap: break-word; +} +input[type=text] { + background: #fff; + border: 1px solid #d0d0d0; + border-radius: 2px; + box-sizing: border-box; + color: #434343; + font-family: inherit; + font-size: inherit; + height: 2.14285714em; + line-height: 1.4285714285714; + padding: 4px 5px; + margin: 0; + width: 100%; +} +input[type=text]:focus { + border-color: #4C669F; + outline: 0; +} +.button-container { + box-sizing: border-box; + clear: both; + margin: 1px 0 0; + padding: 4px 0; + position: relative; + width: 100%; +} +button[type=submit] { + box-sizing: border-box; + background: #f5f5f5; + border: 1px solid #bdbdbd; + border-radius: 2px; + color: #434343; + cursor: pointer; + display: inline-block; + font-family: inherit; + font-size: 14px; + font-variant: normal; + font-weight: 400; + height: 2.14285714em; + line-height: 1.42857143; + margin: 0; + padding: 4px 10px; + text-decoration: none; + vertical-align: baseline; + white-space: nowrap; +} \ No newline at end of file diff --git a/esp-rf/cc1101/multistation/v1/v1.ino b/esp-rf/cc1101/multistation/v1/v1.ino index e69de29..a84361a 100644 --- a/esp-rf/cc1101/multistation/v1/v1.ino +++ b/esp-rf/cc1101/multistation/v1/v1.ino @@ -0,0 +1,125 @@ +#include +ConfigManager configManager; + +const char *settingsHTML = (char *)"/settings.html"; +const char *resetHTML = (char *)"/reset.html"; +const char *stylesCSS = (char *)"/styles.css"; +const char *mainJS = (char *)"/main.js"; + +const char *controlHTML = (char *)"/control.html"; + +const int DEVICENAMELEN = 32; + +struct Config { + char deviceName[DEVICENAMELEN]; +} config; + +struct Metadata { +} meta; + +template +void debug(T &msg, bool newline = false) { + DEBUG_MODE = true; + Serial.begin(115200); + DebugPrint(msg); + if (newline) DebugPrintln(F("")); + Serial.flush(); + Serial.end(); +} + +// ConfigManager +// +void configSetup() { + DEBUG_MODE = true; + Serial.begin(115200); + + String sApName = "ESP"; + + configManager.setAPName(sApName.c_str()); + configManager.setAPFilename("/index.html"); + configManager.setWebPort(8080); + + // Config + configManager.addParameter("deviceName", config.deviceName, DEVICENAMELEN); + + // Callbacks + configManager.setAPCallback(APCallback); + configManager.setAPICallback(APICallback); + configManager.begin(config); +} + +void setConfigDefaults() { + bool requireSave = false; + + char firstChar = config.deviceName[0]; + if (firstChar == '\0' || config.deviceName == NULL || firstChar == '\xFF') { + strncpy(config.deviceName, "rascal", DEVICENAMELEN); + requireSave = true; + } + + if (requireSave) configManager.save(); +} + +void printConfig() { + debug("Configuration: "); + debug(config.deviceName, true); +} + +void serveAssets(WebServer *server) { + server->on("/styles.css", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(stylesCSS, mimeCSS); + }); + + server->on("/main.js", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(mainJS, mimeJS); + }); +} + +void APCallback(WebServer *server) { + serveAssets(server); + setConfigDefaults(); + printConfig(); +} + +void APICallback(WebServer *server) { + serveAssets(server); + + server->on("/disconnect", HTTPMethod::HTTP_GET, [server](){ + configManager.clearWifiSettings(false); + }); + + server->on("/reset", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(resetHTML, mimeHTML); + configManager.clearSettings(false); + }); + + server->on("/wipe", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(resetHTML, mimeHTML); + configManager.clearWifiSettings(false); + configManager.clearSettings(false); + ESP.restart(); + }); + + server->on("/config", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(settingsHTML, mimeHTML); + }); + + server->on("/control", HTTPMethod::HTTP_GET, [server](){ + configManager.streamFile(controlHTML, mimeHTML); + }); + + setConfigDefaults(); + printConfig(); +} + +// Main +// +void setup() { + configSetup(); +} + +void loop() { + configManager.loop(); +} + +