From 382486000ce7e1b4b69b50d7cb67dd99a4453dd7 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 1 May 2023 11:18:15 +0200 Subject: [PATCH] Replaced html page generation by cleaner version. --- firmware/config.h | 1 - firmware/data/config.html | 62 ++++ firmware/data/index.html | 275 ++++++++++++++++ firmware/data/light_control.html | 29 ++ firmware/firmware.ino | 537 ++++++------------------------- 5 files changed, 463 insertions(+), 441 deletions(-) create mode 100644 firmware/data/config.html create mode 100644 firmware/data/index.html create mode 100644 firmware/data/light_control.html diff --git a/firmware/config.h b/firmware/config.h index 3fcbb02..a667301 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -21,4 +21,3 @@ #define MY_NTP_SERVER "de.pool.ntp.org" -//#define DISABLE_WEB_CONTROL diff --git a/firmware/data/config.html b/firmware/data/config.html new file mode 100644 index 0000000..ac695cd --- /dev/null +++ b/firmware/data/config.html @@ -0,0 +1,62 @@ + +
+

Config

+
+ + +
+
+ + +
+
+

Wifi

+
+ + +
+
+ + +
+
+

Network

+
+ + ON + OFF +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
diff --git a/firmware/data/index.html b/firmware/data/index.html new file mode 100644 index 0000000..ad26aa0 --- /dev/null +++ b/firmware/data/index.html @@ -0,0 +1,275 @@ + + + + + + + Light Setup + + + + + + +
+

{{LIGHT_NAME}}

+
+
+ + alert +   + reset +   + reset timing control data +   + update + + +
+
+
+ + ON + OFF +
+ +
+
+ + +
+
+ +
+ + + + + +
+{{LIGHTS_CONTROL}} + + +
+
+ {{CONFIG_PAGE}} + +
+
+ + + diff --git a/firmware/data/light_control.html b/firmware/data/light_control.html new file mode 100644 index 0000000..87bd448 --- /dev/null +++ b/firmware/data/light_control.html @@ -0,0 +1,29 @@ +

Light {{LIGHT_NUMBER}}

+
+ + ON + OFF +
+
+ + +   + 9 + % +
+ + +   + + % + +
diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 626ac45..eff2f60 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -5,6 +5,7 @@ #include #include #include +#include "FS.h" #include "config.h" @@ -323,7 +324,6 @@ void handleNotFound() void init_webserver() { -#ifndef DISABLE_WEB_CONTROL server.on("/state", HTTP_PUT, []() { // HTTP PUT request used to set a new light state DynamicJsonDocument root(1024); @@ -427,13 +427,9 @@ void init_webserver() server.send(200, "application/json", output); }); -#endif // DISABLE_WEB_CONTROL - server.on("/", []() { -#ifndef DISABLE_WEB_CONTROL - if (server.hasArg("transition")) { default_transitiontime = server.arg("transition").toFloat(); @@ -468,7 +464,6 @@ void init_webserver() Serial.println(EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS)); } } -#endif // DISABLE_WEB_CONTROL // timing controller switch handling if (server.hasArg("tc")) @@ -510,8 +505,6 @@ void init_webserver() } } -#ifndef DISABLE_WEB_CONTROL - // scene switch handling if (server.hasArg("scene")) { scene = server.arg("scene").toInt(); @@ -602,8 +595,6 @@ void init_webserver() } // process all lights -#endif // DISABLE_WEB_CONTROL - if (server.hasArg("resettc")) { // reqrite the tc config and reboot tc_write_default(); ESP.reset(); @@ -614,465 +605,131 @@ void init_webserver() } // Generate HTML page - String http_content = ""; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += ""; - //http_content += ""; // Reload the page every 15 seconds automatically - http_content += "Light Setup"; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += ""; - http_content += "
"; - http_content += "

" + (String)light_name + "

"; + String http_content = "123"; + // HTML-Datei öffnen und Inhalt in eine Zeichenfolge speichern + File file = SPIFFS.open("/index.html", "r"); + http_content = file.readString(); + file.close(); - http_content += "
"; + http_content.replace("{{LIGHT_NAME}}", (String)light_name); - http_content += "
"; - http_content += "alert   reset   reset timing control data   update"; - http_content += ""; - http_content += "
"; - - // timing control button - http_content += "
"; - http_content += "
"; - http_content += ""; int tc_val = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); - http_content += "ON"; - http_content += "OFF"; - http_content += "
"; - http_content += ""; - http_content += "
"; + if (tc_val == TIMING_CONTROL_ENABLED) + { + http_content.replace("{{TC_LINK_PRIMARY_ON]]", "pure-button-primary"); + } else { + http_content.replace("{{TC_LINK_PRIMARY_ON]]", ""); + } + if (tc_val == TIMING_CONTROL_DISABLED) + { + http_content.replace("{{TC_LINK_PRIMARY_OFF]]", "pure-button-primary"); + } else { + http_content.replace("{{TC_LINK_PRIMARY_OFF]]", ""); + } - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; - http_content += "
"; + http_content.replace("{{TRANSITION_TIME}}", (String)default_transitiontime); - http_content += ""; + // Generate lights part of the HTML page + String light_content = ""; + // HTML-Datei öffnen und Inhalt in eine Zeichenfolge speichern + File file2 = SPIFFS.open("/light_control.html", "r"); + light_content = file2.readString(); + file2.close(); -#ifndef DISABLE_WEB_CONTROL - http_content += "
"; // Light control for (uint8 light_num = 0; light_num < LIGHTS_COUNT; light_num++) { - // on/off buttons - http_content += "

Light " + (String)(light_num + 1) + "

"; - http_content += "
"; - http_content += ""; - http_content += "ON"; - http_content += "OFF"; - http_content += "
"; - - // slider for brightness - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += " " + (String)(int)(bri[light_num] * 100.0 / 255.0) + "%"; - http_content += "
"; - http_content += ""; - http_content += " %"; - http_content += ""; - - http_content += "
"; - + // on/off buttons and slider + light_content.replace("{{LIGHT_NUMBER}}", (String)(light_num + 1)); + light_content.replace("{{LIGHT_NUMBER_DEC}}", (String)light_num); } - http_content += ""; - http_content += "
"; + // add the lights code to the html output string + http_content.replace("{{LIGHTS_CONTROL}}", light_content); - // startup state and scene for all of the lights - http_content += "
"; - http_content += "
"; - http_content += "

"; - http_content += "

Config

"; - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; + if (ls_val == LAST_STATE_STARTUP_LIGHT_LAST_STATE) + { + http_content.replace("{{STARTUP_SELECTED_LS_0}}", "selected=\"selected\""); + } else { + http_content.replace("{{STARTUP_SELECTED_LS_0}}", ""); + } + + if (ls_val == LAST_STATE_STARTUP_LIGHT_ON_STATE) + { + http_content.replace("{{STARTUP_SELECTED_ON_1}}", "selected=\"selected\""); + } else { + http_content.replace("{{STARTUP_SELECTED_ON_1}}", ""); + } + + if (ls_val == LAST_STATE_STARTUP_LIGHT_OFF_STATE) + { + http_content.replace("{{STARTUP_SELECTED_OFF_2}}", "selected=\"selected\""); + } else { + http_content.replace("{{STARTUP_SELECTED_OFF_2}}", ""); + } // scene - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; + if (sc_val == SCENE_RELEAX) + { + http_content.replace("SCENE_SELECTED_RELAX_0", "selected=\"selected\""); + } else { + http_content.replace("SCENE_SELECTED_RELAX_0", ""); + } + if (sc_val == SCENE_BRIGHT) + { + http_content.replace("SCENE_SELECTED_BRIGHT_1", "selected=\"selected\""); + } else { + http_content.replace("SCENE_SELECTED_BRIGHT_1", ""); + } + if (sc_val == SCENE_NIGHTLY) + { + http_content.replace("SCENE_SELECTED_NIGHT_2", "selected=\"selected\""); + } else { + http_content.replace("SCENE_SELECTED_NIGHT_2", ""); + } + + // Generate lights part of the HTML page + String config_content = ""; + // HTML-Datei öffnen und Inhalt in eine Zeichenfolge speichern + File file3 = SPIFFS.open("/config.html", "r"); + config_content = file3.readString(); + file3.close(); // Wifi settings - http_content += "
"; - http_content += "

Wifi

"; - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; + config_content.replace("{{WIFI_SSIF}}", WiFi.SSID()); // Network settings uint8_t dip = EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS); - http_content += "
"; - http_content += "

Network

"; - http_content += "
"; - http_content += ""; - http_content += "ON"; - http_content += "OFF"; - http_content += "
"; - - // ip config - if (dip == 0) { - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; - http_content += "
"; - http_content += ""; - http_content += ""; - http_content += "
"; + if (dip) + { + config_content.replace("{{DIP_LINK_ON_PRIMARY}}", "pure-button-primary"); + config_content.replace("{{DIP_LINK_OFF_PRIMARY}}", ""); + } else { + config_content.replace("{{DIP_LINK_OFF_PRIMARY}}", "pure-button-primary"); + config_content.replace("{{DIP_LINK_ON_PRIMARY}}", ""); } - // The save button - http_content += "
"; - http_content += ""; - http_content += "
"; + // ip config + if (dip == 0) + { + config_content.replace("{{WIFI_IP}}", WiFi.localIP().toString()); + config_content.replace("{{WIFI_GW}}", WiFi.gatewayIP().toString()); + config_content.replace("{{WIFI_NM}}", WiFi.subnetMask().toString()); + config_content.replace("{{WIFI_DNS}}", WiFi.dnsIP().toString()); + } - http_content += ""; - -#endif // DISABLE_WEB_CONTROL - - http_content += ""; - http_content += "
"; - http_content += "
"; - http_content += ""; - http_content += ""; + http_content.replace("{{PWM_MIN}}", (String)PWM_MIN); + http_content.replace("{{PWM_MAX}}", (String)PWM_MAX); server.send(200, "text/html", http_content); }); - server.on("/reset", []() { // trigger manual reset + server.on("/reset", []() + { // trigger manual reset server.send(200, "text/html", "reset"); delay(1000); ESP.restart();