diff --git a/firmware/config_html.h b/firmware/config_html.h deleted file mode 100644 index f56997b..0000000 --- a/firmware/config_html.h +++ /dev/null @@ -1,66 +0,0 @@ - -String getConfigHTML() -{ - String config_html = "
" -"

Config

" -"
" -"" -"" -"
" -"
" -"" -"" -"
" -"
" -"

Wifi

" -"
" -"" -"" -"
" -"
" -"" -"" -"
" -"
" -"

Network

" -"
" -"" -"ON" -"OFF" -"
" -"
" -"" -"" -"
" -"
" -"" -"" -"
" -"
" -"" -"" -"
" -"
" -"" -"" -"
" -"
" -"" -"
" -"
"; - return config_html; -} diff --git a/firmware/data/config_template.html b/firmware/data/config_template.html new file mode 100644 index 0000000..bf94325 --- /dev/null +++ b/firmware/data/config_template.html @@ -0,0 +1,62 @@ +
+

Config

+
+ + +
+
+ + +
+
+

Wifi

+
+ + +
+
+ + +
+
+

Network

+
+ +ON +OFF +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ diff --git a/firmware/data/index_template_bottom.html b/firmware/data/index_template_bottom.html new file mode 100644 index 0000000..af0a134 --- /dev/null +++ b/firmware/data/index_template_bottom.html @@ -0,0 +1,175 @@ + + + + + + diff --git a/firmware/data/index_template_middle.html b/firmware/data/index_template_middle.html new file mode 100644 index 0000000..088c0a5 --- /dev/null +++ b/firmware/data/index_template_middle.html @@ -0,0 +1,7 @@ + + + +
+ + + diff --git a/firmware/data/index_template_top.html b/firmware/data/index_template_top.html new file mode 100644 index 0000000..2e8110d --- /dev/null +++ b/firmware/data/index_template_top.html @@ -0,0 +1,91 @@ + + + + + + +Light setup - {{LIGHT_NAME}} + + + + + + +
+

{{LIGHT_NAME}}

+
+
+ +alert +  +reset +  +reset timing control data +  +update + + +
+
+
+ +ON +OFF +
+ +
+
+ + +
+
+ +
+ + +
diff --git a/firmware/data/light_control_template.html b/firmware/data/light_control_template.html new file mode 100644 index 0000000..8bc313f --- /dev/null +++ b/firmware/data/light_control_template.html @@ -0,0 +1,29 @@ +

Light {{LIGHT_NUMBER}}

+
+ +ON +OFF +
+
+ + +  +9 +% +
+ + +  + +% + +
diff --git a/firmware/config_html.html b/firmware/data/org/config_template.html similarity index 98% rename from firmware/config_html.html rename to firmware/data/org/config_template.html index f96ff61..802760a 100644 --- a/firmware/config_html.html +++ b/firmware/data/org/config_template.html @@ -1,3 +1,4 @@ +

Config

@@ -59,4 +60,4 @@
- +
diff --git a/firmware/index_html.html b/firmware/data/org/index_template_bottom.html similarity index 68% rename from firmware/index_html.html rename to firmware/data/org/index_template_bottom.html index 45afa17..78a6769 100644 --- a/firmware/index_html.html +++ b/firmware/data/org/index_template_bottom.html @@ -1,102 +1,18 @@ - - - - - - - Light setup - {{LIGHT_NAME}} - - - - - - -
-

{{LIGHT_NAME}}

-
-
- - alert -   - reset -   - reset timing control data -   - update - - -
-
-
- - ON - OFF -
- -
-
- - -
-
- -
- - - - - -
-{{LIGHTS_CONTROL}} - -
-
- {{CONFIG_PAGE}} + + + + + + + + + +
+

{{LIGHT_NAME}}

+
+
+ + alert +   + reset +   + reset timing control data +   + update + + +
+
+ +
+ Main + Config +
+ +
+
+ + ON + OFF +
+ +
+
+ + +
+
+ +
+
+ + +
diff --git a/firmware/light_control_html.html b/firmware/data/org/light_control_template.html similarity index 100% rename from firmware/light_control_html.html rename to firmware/data/org/light_control_template.html diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 629f137..3c546af 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,3 +1,4 @@ +//********************************// #include #include @@ -6,13 +7,10 @@ #include #include #include +#include #include "config.h" -#include "index_html.h" -#include "light_control_html.h" -#include "config_html.h" - //********* Config block *********// // blue, warmwhite, purple, white&red&green // blau, schwarz, rot, weiß @@ -190,6 +188,59 @@ uint16_t calcPWM(float tbri) void read_eeprom_config() { + uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); + if (tmp == TIMING_CONTROL_DISABLED) + { + tc_enabled = TIMING_CONTROL_DISABLED; + + } else if (tmp == TIMING_CONTROL_ENABLED) + { + tc_enabled = TIMING_CONTROL_ENABLED; + + } else { + // Write default value + EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED); + EEPROM.commit(); + tc_enabled = TIMING_CONTROL_DISABLED; + Serial.println("Written default timing control config to EEPROM (disabled)"); + } + Serial.println("Timing Control status: " + (String)tc_enabled); + + if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) > 2) + { + // set the default value on uninitialized EEPROM + EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0); + EEPROM.commit(); + Serial.println("Written default 'last state' config to EEPROM"); + } + Serial.println("Last state startup setting: " + (String)EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS)); + + if (EEPROM.read(EEPROM_SCENE_ADDRESS) > 2) + { + // set the default value on uninitialized EEPROM + EEPROM.write(EEPROM_SCENE_ADDRESS, 0); + EEPROM.commit(); + Serial.println("Written default scene config to EEPROM"); + } + Serial.println("Scene setting: " + (String)EEPROM.read(EEPROM_SCENE_ADDRESS)); + +#ifdef USE_STATIC_IP + if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) > 1) + { + EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 0); + EEPROM.commit(); + Serial.println("Written default dynamic IP setting (disabled) to EEPROM"); + } +#else + if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) > 1) + { + EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 1); + EEPROM.commit(); + Serial.println("Written default dynamic IP setting (enabled) to EEPROM"); + } +#endif + Serial.println("Dynamic IP setting: " + (String)EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS)); + for (uint8_t light = 0; light < LIGHTS_COUNT; light++) { apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light); @@ -204,45 +255,6 @@ void read_eeprom_config() Serial.println("light[" + (String)light + "] = " + (String)light_state[light]); } - uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); - if (tmp == TIMING_CONTROL_DISABLED) - { - tc_enabled = TIMING_CONTROL_DISABLED; - - } else if (tmp == TIMING_CONTROL_ENABLED) - { - tc_enabled = TIMING_CONTROL_ENABLED; - - } else { - - EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED); - EEPROM.commit(); - tc_enabled = TIMING_CONTROL_DISABLED; - } - - Serial.println("tc_enabled = " + (String)tc_enabled); - - if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255) - { - // set the default value on uninitialized EEPROM - EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0); - EEPROM.commit(); - } - -#ifdef USE_STATIC_IP - if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) - { - EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 0); - EEPROM.commit(); - } -#else - if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) - { - EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 1); - EEPROM.commit(); - } -#endif - } //********************************// @@ -251,8 +263,22 @@ void setup() { EEPROM.begin(256); + SPIFFS.begin(); + Serial.begin(SERIAL_BAUD_RATE); + Serial.flush(); + delay(1000); + + Dir dir = SPIFFS.openDir("/"); + Serial.println("\n\nSPIFFS directory content:"); + while (dir.next()) + { + String fileName = dir.fileName(); + size_t fileSize = dir.fileSize(); + Serial.printf("Datei Name: %s, Größe: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); + } + if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0) { WiFi.config(strip_ip, gateway_ip, subnet_mask, dns); @@ -293,8 +319,10 @@ void setup() init_webserver(); + Serial.println("Init timinc control"); tc_init(); + Serial.println("Starting webserver"); server.begin(); } // end of setup @@ -562,6 +590,7 @@ void init_webserver() if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 0) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON); + EEPROM.commit(); } Serial.print("Light "); Serial.print(light); @@ -574,6 +603,7 @@ void init_webserver() if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 1) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF); + EEPROM.commit(); } Serial.print("Light "); Serial.print(light); @@ -581,8 +611,6 @@ void init_webserver() Serial.println(light_state[light]); } - EEPROM.commit(); - if (tc_enabled == TIMING_CONTROL_DISABLED) { process_lightdata(light, default_transitiontime); @@ -608,7 +636,8 @@ void init_webserver() } // process all lights - if (server.hasArg("resettc")) { // reqrite the tc config and reboot + if (server.hasArg("resettc")) + { // reqrite the tc config and reboot tc_write_default(); ESP.reset(); } @@ -620,17 +649,22 @@ void init_webserver() // ***** Generate HTML page ***** // - server.sendHeader("Content-Type", "text/html"); - server.sendHeader("Connection", "close"); - server.sendHeader("Access-Control-Allow-Origin", "*"); - server.send(200); - server.sendContent(replacePlaceholder(replaceLightsControl(getIndexHTMLTop()))); - server.sendContent(replacePlaceholder(getConfigHTML())); - server.sendContent(replacePlaceholder(getIndexHTMLBottom())); + String tmp1 = genHMTLTop(); + + String tmp2 = genLightControlHTML(); + + String tmp3 = getIndexHTMLMiddle(); + + String tmp4 = genConfigHTML(); + + String tmp5 = genHMTLBottom(); + + server.send(200, "text/html", tmp1 + tmp2 + tmp3 + tmp4 + tmp5); + }); server.on("/reset", []() - { // trigger manual reset + { // trigger manual reset server.send(200, "text/html", "reset"); delay(1000); ESP.restart(); @@ -639,9 +673,48 @@ void init_webserver() server.onNotFound(handleNotFound); } -String replacePlaceholder(String in) +//********************************// + +String genHMTLTop() +{ + return replacePlaceholder(getIndexHTMLTop()); +} + +String genHMTLBottom() +{ + return replacePlaceholder(getIndexHTMLBottom()); +} + +String genConfigHTML() +{ + // +++++ Generate config part of the page +++++ + return replacePlaceholder(getConfigHTML()); +} + +String genLightControlHTML() +{ + String http_content = ""; + // +++++ Generate lights part of the HTML page +++++ + // Light control + for (uint8 light_num = 0; light_num < LIGHTS_COUNT; light_num++) + { + // Generate lights part of the HTML page + String tmp_light_content = getLightControlHTML(); + + // on/off buttons and slider + tmp_light_content.replace("{{LIGHT_NUMBER}}", (String)(light_num + 1)); + tmp_light_content.replace("{{LIGHT_NUMBER_DEC}}", (String)light_num); + + // add the lights code to the html output string + http_content += tmp_light_content; + } + return http_content; +} + +//********************************// + +String replacePlaceholder(String http_content) { - String http_content = in; http_content.replace("{{LIGHT_NAME}}", (String)light_name); @@ -706,31 +779,27 @@ String replacePlaceholder(String in) http_content.replace("{{SCENE_SELECTED_NIGHT_2}}", ""); } - // Generate lights part of the HTML page - String config_content = getConfigHTML(); - // Wifi settings - config_content.replace("{{WIFI_SSID}}", WiFi.SSID()); + http_content.replace("{{WIFI_SSID}}", WiFi.SSID()); // Network settings uint8_t dip = EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS); if (dip) { - config_content.replace("{{DIP_LINK_ON_PRIMARY}}", "pure-button-primary"); - config_content.replace("{{DIP_LINK_OFF_PRIMARY}}", ""); + http_content.replace("{{DIP_LINK_ON_PRIMARY}}", "pure-button-primary"); + http_content.replace("{{DIP_LINK_OFF_PRIMARY}}", ""); } else { - config_content.replace("{{DIP_LINK_OFF_PRIMARY}}", "pure-button-primary"); - config_content.replace("{{DIP_LINK_ON_PRIMARY}}", ""); + http_content.replace("{{DIP_LINK_OFF_PRIMARY}}", "pure-button-primary"); + http_content.replace("{{DIP_LINK_ON_PRIMARY}}", ""); } - // ip config - config_content.replace("{{WIFI_CFG_IP}}", WiFi.localIP().toString()); - config_content.replace("{{WIFI_CFG_GW}}", WiFi.gatewayIP().toString()); - config_content.replace("{{WIFI_CFG_NM}}", WiFi.subnetMask().toString()); - config_content.replace("{{WIFI_CFG_DNS}}", WiFi.dnsIP().toString()); - - http_content.replace("{{CONFIG_PAGE}}", config_content); + // network config + http_content.replace("{{WIFI_CFG_IP}}", WiFi.localIP().toString()); + http_content.replace("{{WIFI_CFG_GW}}", WiFi.gatewayIP().toString()); + http_content.replace("{{WIFI_CFG_NM}}", WiFi.subnetMask().toString()); + http_content.replace("{{WIFI_CFG_DNS}}", WiFi.dnsIP().toString()); + // add the current ip address to the page http_content.replace("{{IP_ADDRESS}}", WiFi.localIP().toString()); // set the pwm values @@ -740,25 +809,73 @@ String replacePlaceholder(String in) return http_content; } -String replaceLightsControl(String in) +//********************************// + +String loadSPIFFSFile(String fname) { - String light_content = ""; - // Light control - for (uint8 light_num = 0; light_num < LIGHTS_COUNT; light_num++) + File file = SPIFFS.open(fname, "r"); + if (!file) { - // Generate lights part of the HTML page - String tmp_light_content = getLightControlHTML(); - - // on/off buttons and slider - tmp_light_content.replace("{{LIGHT_NUMBER}}", (String)(light_num + 1)); - tmp_light_content.replace("{{LIGHT_NUMBER_DEC}}", (String)light_num); - - // add the lights code to the html output string - light_content += tmp_light_content; + Serial.println("Failed to open file " + fname); + return ""; } - - // add the created lights control code to the html output - in.replace("{{LIGHTS_CONTROL}}", light_content); - - return in; + String contents = file.readString(); + file.close(); + return contents; } + +//********************************// + +String getIndexHTMLTop() +{ + // load file + return loadSPIFFSFile("/index_template_top.html"); +} + +String getIndexHTMLMiddle() +{ + // load file + return loadSPIFFSFile("/index_template_middle.html"); +} + +String getIndexHTMLBottom() +{ + // load file + return loadSPIFFSFile("/index_template_bottom.html"); +} + +//********************************// + +String getConfigHTML() +{ + // load file + return loadSPIFFSFile("/config_template.html"); +} + +//********************************// + +String getLightControlHTML() +{ + // load file + return loadSPIFFSFile("/light_control_template.html"); +} + +//********************************// + +String formatBytes(size_t bytes) +{ + if (bytes < 1024) + { + return String(bytes) + " B"; + } else if (bytes < (1024 * 1024)) + { + return String(bytes / 1024.0) + " KB"; + } else if (bytes < (1024 * 1024 * 1024)) + { + return String(bytes / 1024.0 / 1024.0) + " MB"; + } else { + return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; + } +} + +//********************************// diff --git a/firmware/images/Over.jpg b/firmware/images/Over.jpg deleted file mode 100755 index 8f6ddab..0000000 Binary files a/firmware/images/Over.jpg and /dev/null differ diff --git a/firmware/images/Under.jpg b/firmware/images/Under.jpg deleted file mode 100755 index 87c4456..0000000 Binary files a/firmware/images/Under.jpg and /dev/null differ diff --git a/firmware/index_html.h b/firmware/index_html.h deleted file mode 100644 index 1f0042b..0000000 --- a/firmware/index_html.h +++ /dev/null @@ -1,283 +0,0 @@ - - -String getIndexHTMLTop() -{ - String index_html = "" -"" -"" -"" -"" -"" -"Light setup - {{LIGHT_NAME}}" -"" -"" -"" -"" -"" -"" -"
" -"

{{LIGHT_NAME}}

" -"
" -"
" -"" -"alert" -"  " -"reset" -"  " -"reset timing control data" -"  " -"update" -"" -"" -"
" -"
" -"
" -"" -"ON" -"OFF" -"
" -"" -"
" -"
" -"" -"" -"
" -"
" -"" -"
" -"" -"" -"" -"" -"" -"
" -"{{LIGHTS_CONTROL}}" -"" -"
" -"
"; - return index_html; -} - -String getIndexHTMLBottom() -{ - const char index_html[] = "" -"
" -"
" -"" -""; - return String(index_html); -} diff --git a/firmware/light_control_html.h b/firmware/light_control_html.h deleted file mode 100644 index 63517c4..0000000 --- a/firmware/light_control_html.h +++ /dev/null @@ -1,34 +0,0 @@ - -String getLightControlHTML() -{ - String light_control_html = "

Light {{LIGHT_NUMBER}}

" -"
" -"" -"ON" -"OFF" -"
" -"
" -"" -"" -" " -"9" -"%" -"
" -"" -"" -" " -"" -"%" -"" -"
"; - return light_control_html; -} diff --git a/tools/html2string.sh b/tools/html2string.sh index a339e62..bb59438 100644 --- a/tools/html2string.sh +++ b/tools/html2string.sh @@ -1,5 +1,4 @@ #!/bin/bash -cat $1 | sed -e"s/^ *//ig" | sed -e 's/"/\\"/ig' | sed -e "s/^/\"/ig" | sed -e "s/$/\"/ig" -echo ";" +cat $1 | sed -e"s/^ *//ig"