diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index ee84e05..9f58e55 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -152,6 +152,9 @@ void loop() { if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) { Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true"); configSystem.httpLogSerial = true; + // save config to littlefs as json + SaveConfig(); + // only print json to serial SaveConfig(true); alrdySaved = true; } else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) { diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 5cd0d8f..9682113 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -226,23 +226,7 @@ bool LoadConfig() { return true; } -void SaveConfig(bool writeToSerial = false) { - #ifdef ESP8266 - File file = LittleFS.open(CANGROW_CFG, "w"); - #endif - - #ifdef ESP32 - fs::FS &fs = LittleFS; - File file = fs.open(CANGROW_CFG, FILE_WRITE); - #endif - - if (!file) { - Serial.printf("!! [LittleFS:SaveConfig] FAILED to open configfile for writing: %s\n", CANGROW_CFG); - return; - } else { - Serial.printf(":: [LittleFS:SaveConfig] opened for writing %s\n", CANGROW_CFG); - } - +void SaveConfig(bool writeToSerial = false) { /* * Building config.json here */ @@ -291,22 +275,40 @@ void SaveConfig(bool writeToSerial = false) { /* * END Building config.json here */ - - // Serialize JSON to file - if (serializeJson(doc, file) == 0) { - Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG); + + // if writeToSerial is true, output json to serial, but do not write to LittleFS + if(writeToSerial == false) { + #ifdef ESP8266 + File file = LittleFS.open(CANGROW_CFG, "w"); + #endif + + #ifdef ESP32 + fs::FS &fs = LittleFS; + File file = fs.open(CANGROW_CFG, FILE_WRITE); + #endif + + if (!file) { + Serial.printf("!! [LittleFS:SaveConfig] FAILED to open configfile for writing: %s\n", CANGROW_CFG); + return; + } else { + Serial.printf(":: [LittleFS:SaveConfig] opened for writing %s\n", CANGROW_CFG); + } + // Serialize JSON to file + if (serializeJson(doc, file) == 0) { + Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG); + } else { + Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG); + } + file.close(); } else { - Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG); - } - file.close(); - - if(writeToSerial == true) { Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG); serializeJson(doc, Serial); Serial.println(""); Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG); } + + } ///* diff --git a/Arduino/CanGrow/include/CanGrow_Webserver.h b/Arduino/CanGrow/include/CanGrow_Webserver.h index c4011fe..36ae44f 100644 --- a/Arduino/CanGrow/include/CanGrow_Webserver.h +++ b/Arduino/CanGrow/include/CanGrow_Webserver.h @@ -33,8 +33,8 @@ * include webpages header files */ #include "Webserver/header.h" - #include "Webserver/footer.h" +#include "Webserver/AddHeaderFooter.h" #include "Webserver/Page_root.h" @@ -43,27 +43,26 @@ /* * include static files files */ -#include "Webserver/File_cangrow_JS.h" +#include "Webserver/File_cangrow_CSS.h" AsyncWebServer webserver(80); // log incoming requests LoggingMiddleware requestLogger; + // https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/examples/SimpleServer/SimpleServer.ino void WebserverNotFound(AsyncWebServerRequest* request) { request->send(404, "text/plain", "Not found"); } - - /* * setup all the webhandlers */ void SetupWebserver() { Serial.println(":: [Webserver] initializing"); webserver.on("/", HTTP_GET, WebPage_root); - webserver.on("/cangrow.js", HTTP_GET, WebFile_cangrow_JS); + webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS); requestLogger.setOutput(Serial); // this activates the middleware diff --git a/Arduino/CanGrow/include/Webserver/footer_HTML.h b/Arduino/CanGrow/include/Webserver/AddHeaderFooter.h similarity index 58% rename from Arduino/CanGrow/include/Webserver/footer_HTML.h rename to Arduino/CanGrow/include/Webserver/AddHeaderFooter.h index cdb0189..ac41610 100644 --- a/Arduino/CanGrow/include/Webserver/footer_HTML.h +++ b/Arduino/CanGrow/include/Webserver/AddHeaderFooter.h @@ -1,6 +1,7 @@ /* * - * include/Webserver/footer_HTML.h - footer page HTML header file + * include/Webserver/AddHeaderFooter.h - header file with functions to add + * HTML header or footer to a String() * * * MIT License @@ -26,3 +27,36 @@ * THE SOFTWARE. * */ + +bool TestHeaderFooter(const String& var) { + #ifdef DEBUG + Serial.print(":: [Webserver:Page:root:proc:hf] var: "); + Serial.println(var); + #endif + + if(var == "HEADER") { + return true; + } else if(var == "FOOTER") { + return true; + } else if(var == "CGVER") { + return true; + } else if(var == "CGBUILD") { + return true; + } else { + return false; + } +} + +String AddHeaderFooter(const String& var) { + if(var == "HEADER") { + return String(Header_HTML); + } else if(var == "FOOTER") { + return String(Footer_HTML); + } else if(var == "CGVER") { + return String(CANGROW_VER); + } else if(var == "CGBUILD") { + return String(CANGROW_BUILD); + } else { + return String(); + } +} diff --git a/Arduino/CanGrow/include/Webserver/File_cangrow_JS.h b/Arduino/CanGrow/include/Webserver/File_cangrow_CSS.h similarity index 94% rename from Arduino/CanGrow/include/Webserver/File_cangrow_JS.h rename to Arduino/CanGrow/include/Webserver/File_cangrow_CSS.h index 6308260..667fe08 100644 --- a/Arduino/CanGrow/include/Webserver/File_cangrow_JS.h +++ b/Arduino/CanGrow/include/Webserver/File_cangrow_CSS.h @@ -1,6 +1,6 @@ /* * - * include/Webserver/File_cangrow_JS.h - /cangrow.js header file + * include/Webserver/File_cangrow_CSS.h - /cangrow.css header file * * * MIT License @@ -28,7 +28,7 @@ */ -const char* File_cangrow_JS PROGMEM = R"(body { +const char* File_cangrow_CSS PROGMEM = R"(body { color: #cae0d0; background-color: #1d211e; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; @@ -190,6 +190,6 @@ input[type=text], input[type=date], input[type=number], input[type=password], se } )"; -void WebFile_cangrow_JS(AsyncWebServerRequest *request) { - request->send_P(200, "text/javascript", File_cangrow_JS); +void WebFile_cangrow_CSS(AsyncWebServerRequest *request) { + request->send_P(200, "text/stylesheet", File_cangrow_CSS); } diff --git a/Arduino/CanGrow/include/Webserver/Page_root.h b/Arduino/CanGrow/include/Webserver/Page_root.h index f432f3e..514c544 100644 --- a/Arduino/CanGrow/include/Webserver/Page_root.h +++ b/Arduino/CanGrow/include/Webserver/Page_root.h @@ -29,21 +29,23 @@ #include "Page_root_HTML.h" + + // https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/ String Proc_WebPage_root(const String& var) { - Serial.println(":: [Webserver:Page:root:proc] start"); - Serial.print(":: [Webserver:Page:root:proc] var: "); - Serial.println(var); + if(TestHeaderFooter(var)) { + return AddHeaderFooter(var); + } if(var == "LOL") { - return String("omg das geht ja"); - } else if(var == "ROFL") { - return String("rooooooofl geilooooomatic! omg %)"); + return String("Nice"); + } else if(var == "LOL") { + return String("Jojoojo :)"); } else { return String(); } } -void WebPage_root(AsyncWebServerRequest *request) { +void WebPage_root(AsyncWebServerRequest *request) { request->send_P(200, "text/html", Page_root_HTML, Proc_WebPage_root); } diff --git a/Arduino/CanGrow/include/Webserver/Page_root_HTML.h b/Arduino/CanGrow/include/Webserver/Page_root_HTML.h index 75807c2..5df5f4e 100644 --- a/Arduino/CanGrow/include/Webserver/Page_root_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_root_HTML.h @@ -28,14 +28,10 @@ */ -const char* Page_root_HTML PROGMEM = R"( - - -

CanGrow

- Hi
- %LOL%
- %ROFL%
- Bye
- - -)"; +const char* Page_root_HTML PROGMEM = R"(%HEADER% +

CanGrow

+Hi
+%LOL%
+%ROFL%
+Bye
+%FOOTER%)"; diff --git a/Arduino/CanGrow/include/Webserver/footer.h b/Arduino/CanGrow/include/Webserver/footer.h index b351085..a8d4fa2 100644 --- a/Arduino/CanGrow/include/Webserver/footer.h +++ b/Arduino/CanGrow/include/Webserver/footer.h @@ -1,6 +1,6 @@ /* * - * include/Webserver/footer.h - footer page header file + * include/Webserver/footer_HTML.h - footer page HTML header file * * * MIT License @@ -27,4 +27,6 @@ * */ - +const char* Footer_HTML PROGMEM = R"(

Bottom %CGVER% + +)"; diff --git a/Arduino/CanGrow/include/Webserver/header.h b/Arduino/CanGrow/include/Webserver/header.h index 6f68faa..4e7b037 100644 --- a/Arduino/CanGrow/include/Webserver/header.h +++ b/Arduino/CanGrow/include/Webserver/header.h @@ -1,6 +1,6 @@ /* * - * include/Webserver/header.h - header page header file + * include/Webserver/header_HTML.h - header page HTML header file * * * MIT License @@ -27,4 +27,8 @@ * */ - +const char* Header_HTML PROGMEM = R"( + + +%CGBUILD% +
)"; diff --git a/Arduino/CanGrow/include/Webserver/header_HTML.h b/Arduino/CanGrow/include/Webserver/header_HTML.h deleted file mode 100644 index 41044ae..0000000 --- a/Arduino/CanGrow/include/Webserver/header_HTML.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * include/Webserver/header_HTML.h - header page HTML header file - * - * - * MIT License - * - * Copyright (c) 2024 DeltaLima - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */