organizing webserver template stuff, playing with templates
This commit is contained in:
parent
2252fe0142
commit
187ade247f
10 changed files with 100 additions and 86 deletions
|
@ -152,6 +152,9 @@ void loop() {
|
||||||
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
||||||
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true");
|
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true");
|
||||||
configSystem.httpLogSerial = true;
|
configSystem.httpLogSerial = true;
|
||||||
|
// save config to littlefs as json
|
||||||
|
SaveConfig();
|
||||||
|
// only print json to serial
|
||||||
SaveConfig(true);
|
SaveConfig(true);
|
||||||
alrdySaved = true;
|
alrdySaved = true;
|
||||||
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
||||||
|
|
|
@ -227,22 +227,6 @@ bool LoadConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SaveConfig(bool writeToSerial = false) {
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Building config.json here
|
* Building config.json here
|
||||||
*/
|
*/
|
||||||
|
@ -292,6 +276,23 @@ void SaveConfig(bool writeToSerial = false) {
|
||||||
* END Building config.json here
|
* END Building config.json here
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// 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
|
// Serialize JSON to file
|
||||||
if (serializeJson(doc, file) == 0) {
|
if (serializeJson(doc, file) == 0) {
|
||||||
Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG);
|
Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG);
|
||||||
|
@ -299,14 +300,15 @@ void SaveConfig(bool writeToSerial = false) {
|
||||||
Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG);
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
} else {
|
||||||
if(writeToSerial == true) {
|
|
||||||
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
|
||||||
serializeJson(doc, Serial);
|
serializeJson(doc, Serial);
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///*
|
///*
|
||||||
|
|
|
@ -33,8 +33,8 @@
|
||||||
* include webpages header files
|
* include webpages header files
|
||||||
*/
|
*/
|
||||||
#include "Webserver/header.h"
|
#include "Webserver/header.h"
|
||||||
|
|
||||||
#include "Webserver/footer.h"
|
#include "Webserver/footer.h"
|
||||||
|
#include "Webserver/AddHeaderFooter.h"
|
||||||
|
|
||||||
#include "Webserver/Page_root.h"
|
#include "Webserver/Page_root.h"
|
||||||
|
|
||||||
|
@ -43,27 +43,26 @@
|
||||||
/*
|
/*
|
||||||
* include static files files
|
* include static files files
|
||||||
*/
|
*/
|
||||||
#include "Webserver/File_cangrow_JS.h"
|
#include "Webserver/File_cangrow_CSS.h"
|
||||||
|
|
||||||
|
|
||||||
AsyncWebServer webserver(80);
|
AsyncWebServer webserver(80);
|
||||||
// log incoming requests
|
// log incoming requests
|
||||||
LoggingMiddleware requestLogger;
|
LoggingMiddleware requestLogger;
|
||||||
|
|
||||||
|
|
||||||
// https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/examples/SimpleServer/SimpleServer.ino
|
// https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/examples/SimpleServer/SimpleServer.ino
|
||||||
void WebserverNotFound(AsyncWebServerRequest* request) {
|
void WebserverNotFound(AsyncWebServerRequest* request) {
|
||||||
request->send(404, "text/plain", "Not found");
|
request->send(404, "text/plain", "Not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* setup all the webhandlers
|
* setup all the webhandlers
|
||||||
*/
|
*/
|
||||||
void SetupWebserver() {
|
void SetupWebserver() {
|
||||||
Serial.println(":: [Webserver] initializing");
|
Serial.println(":: [Webserver] initializing");
|
||||||
webserver.on("/", HTTP_GET, WebPage_root);
|
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);
|
requestLogger.setOutput(Serial);
|
||||||
// this activates the middleware
|
// this activates the middleware
|
||||||
|
|
|
@ -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
|
* MIT License
|
||||||
|
@ -26,3 +27,36 @@
|
||||||
* THE SOFTWARE.
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
* MIT License
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
const char* File_cangrow_JS PROGMEM = R"(body {
|
const char* File_cangrow_CSS PROGMEM = R"(body {
|
||||||
color: #cae0d0;
|
color: #cae0d0;
|
||||||
background-color: #1d211e;
|
background-color: #1d211e;
|
||||||
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
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) {
|
void WebFile_cangrow_CSS(AsyncWebServerRequest *request) {
|
||||||
request->send_P(200, "text/javascript", File_cangrow_JS);
|
request->send_P(200, "text/stylesheet", File_cangrow_CSS);
|
||||||
}
|
}
|
|
@ -29,16 +29,18 @@
|
||||||
|
|
||||||
#include "Page_root_HTML.h"
|
#include "Page_root_HTML.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/
|
// https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/
|
||||||
String Proc_WebPage_root(const String& var) {
|
String Proc_WebPage_root(const String& var) {
|
||||||
Serial.println(":: [Webserver:Page:root:proc] start");
|
if(TestHeaderFooter(var)) {
|
||||||
Serial.print(":: [Webserver:Page:root:proc] var: ");
|
return AddHeaderFooter(var);
|
||||||
Serial.println(var);
|
}
|
||||||
|
|
||||||
if(var == "LOL") {
|
if(var == "LOL") {
|
||||||
return String("omg das geht ja");
|
return String("Nice");
|
||||||
} else if(var == "ROFL") {
|
} else if(var == "LOL") {
|
||||||
return String("rooooooofl geilooooomatic! omg %)");
|
return String("Jojoojo :)");
|
||||||
} else {
|
} else {
|
||||||
return String();
|
return String();
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,14 +28,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
const char* Page_root_HTML PROGMEM = R"(
|
const char* Page_root_HTML PROGMEM = R"(%HEADER%
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<h1>CanGrow</h1>
|
<h1>CanGrow</h1>
|
||||||
Hi<br>
|
Hi<br>
|
||||||
%LOL%<br>
|
%LOL%<br>
|
||||||
%ROFL% <br>
|
%ROFL% <br>
|
||||||
Bye<br>
|
Bye<br>
|
||||||
</body>
|
%FOOTER%)";
|
||||||
</html>
|
|
||||||
)";
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* include/Webserver/footer.h - footer page header file
|
* include/Webserver/footer_HTML.h - footer page HTML header file
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* MIT License
|
* MIT License
|
||||||
|
@ -27,4 +27,6 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const char* Footer_HTML PROGMEM = R"(<br><br><span>Bottom %CGVER%</span>
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* include/Webserver/header.h - header page header file
|
* include/Webserver/header_HTML.h - header page HTML header file
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* MIT License
|
* MIT License
|
||||||
|
@ -27,4 +27,8 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const char* Header_HTML PROGMEM = R"(<html>
|
||||||
|
<meta></meta>
|
||||||
|
<body>
|
||||||
|
%CGBUILD%
|
||||||
|
<br>)";
|
||||||
|
|
|
@ -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.
|
|
||||||
*
|
|
||||||
*/
|
|
Loading…
Reference in a new issue