add config.system.output.device , add POST stuff for /system/

This commit is contained in:
Marcus 2024-10-24 04:14:19 +02:00
parent 458077442a
commit 5c019f8df6
7 changed files with 140 additions and 42 deletions

View file

@ -157,6 +157,7 @@ void loop() {
Serial.println(":: [LOOP] save output 0"); Serial.println(":: [LOOP] save output 0");
byte i = 0; byte i = 0;
config.system.output.type[i] = 1; config.system.output.type[i] = 1;
config.system.output.device[i] = 1;
strlcpy(config.system.output.name[i], "bla", sizeof("bla")); strlcpy(config.system.output.name[i], "bla", sizeof("bla"));
config.system.output.gpio[i] = 3; config.system.output.gpio[i] = 3;
config.system.output.gpio_invert[i] = false; config.system.output.gpio_invert[i] = false;
@ -172,6 +173,7 @@ void loop() {
Serial.println(":: [LOOP] save output 1"); Serial.println(":: [LOOP] save output 1");
i = 1; i = 1;
config.system.output.type[i] = 2; config.system.output.type[i] = 2;
config.system.output.device[i] = 3;
strlcpy(config.system.output.name[i], "lol", sizeof("lol")); strlcpy(config.system.output.name[i], "lol", sizeof("lol"));
config.system.output.gpio[i] = 5; config.system.output.gpio[i] = 5;
config.system.output.gpio_invert[i] = true; config.system.output.gpio_invert[i] = true;
@ -181,7 +183,7 @@ void loop() {
for(byte j=0; j < 4 ; j++) { for(byte j=0; j < 4 ; j++) {
config.system.output.ip[i][j] = j + 23; config.system.output.ip[i][j] = j + 23;
} }
strlcpy(config.system.output.ip_path[i], "/asd?foo=lol", sizeof("/asd?foo=lol")); strlcpy(config.system.output.ip_path[i], "/toggle_switch?on=true", sizeof("/toggle_switch?on=true"));
config.system.output.enabled[i] = true; config.system.output.enabled[i] = true;
// save config to littlefs as json // save config to littlefs as json

View file

@ -109,6 +109,13 @@ struct Config_System_Output {
* 1 - GPIO * 1 - GPIO
* 2 - I2C * 2 - I2C
* 3 - URL * 3 - URL
* - device: what this output is connected to
* 1 - Light
* 2 - Fan
* 3 - Pump
* 4 - Humudifier
* 5 - Dehumidifier
* 6 - Heating
* - gpio: which gpio is used * - gpio: which gpio is used
* - gpio_invert: invert gpio output * - gpio_invert: invert gpio output
* - gpio_pwm: enable pwm for output * - gpio_pwm: enable pwm for output
@ -119,6 +126,7 @@ struct Config_System_Output {
* *
*/ */
byte type[Max_Outputs]; byte type[Max_Outputs];
byte device[Max_Outputs];
char name[Max_Outputs][32]; char name[Max_Outputs][32];
byte gpio[Max_Outputs]; byte gpio[Max_Outputs];
bool gpio_invert[Max_Outputs]; bool gpio_invert[Max_Outputs];

View file

@ -211,6 +211,7 @@ bool LoadConfig() {
for(byte i=0; i < Max_Outputs; i++) { for(byte i=0; i < Max_Outputs; i++) {
if(objSystemOutput["type"][i] > 0) { if(objSystemOutput["type"][i] > 0) {
config.system.output.type[i] = objSystemOutput["type"][i]; config.system.output.type[i] = objSystemOutput["type"][i];
config.system.output.device[i] = objSystemOutput["device"][i];
strlcpy(config.system.output.name[i], objSystemOutput["name"][i], sizeof(config.system.output.name[i])); strlcpy(config.system.output.name[i], objSystemOutput["name"][i], sizeof(config.system.output.name[i]));
config.system.output.gpio[i] = objSystemOutput["gpio"][i]; config.system.output.gpio[i] = objSystemOutput["gpio"][i];
config.system.output.gpio_invert[i] = objSystemOutput["gpio_invert"][i]; config.system.output.gpio_invert[i] = objSystemOutput["gpio_invert"][i];
@ -288,6 +289,7 @@ bool SaveConfig(bool writeToSerial = false) {
for(byte i=0; i < Max_Outputs; i++) { for(byte i=0; i < Max_Outputs; i++) {
if(config.system.output.type[i] > 0) { if(config.system.output.type[i] > 0) {
objSystemOutput["type"][i] = config.system.output.type[i]; objSystemOutput["type"][i] = config.system.output.type[i];
objSystemOutput["device"][i] = config.system.output.device[i];
objSystemOutput["name"][i] = config.system.output.name[i]; objSystemOutput["name"][i] = config.system.output.name[i];
objSystemOutput["gpio"][i] = config.system.output.gpio[i]; objSystemOutput["gpio"][i] = config.system.output.gpio[i];
objSystemOutput["gpio_invert"][i] = config.system.output.gpio_invert[i]; objSystemOutput["gpio_invert"][i] = config.system.output.gpio_invert[i];

View file

@ -99,6 +99,9 @@ void Webserver_Init() {
webserver.on("/system/wipe", HTTP_GET, WebPage_system_wipe); webserver.on("/system/wipe", HTTP_GET, WebPage_system_wipe);
webserver.on("/system/wipe", HTTP_POST, WebPage_system_wipe); webserver.on("/system/wipe", HTTP_POST, WebPage_system_wipe);
webserver.on("/system/output", HTTP_GET, WebPage_system_output);
webserver.on("/system/output", HTTP_POST, WebPage_system_output);
requestLogger.setOutput(Serial); requestLogger.setOutput(Serial);
// this activates the middleware // this activates the middleware
if(config.system.httpLogSerial == true) { if(config.system.httpLogSerial == true) {

View file

@ -39,7 +39,7 @@ const char Common_HTML_SAVE_MSG_ERR[] PROGMEM = R"EOF(
const char Common_HTML_NEED_RESTART[] PROGMEM = R"EOF( const char Common_HTML_NEED_RESTART[] PROGMEM = R"EOF(
<div class='warnmsg'>&#10071; Restart is required to apply new settings! <div class='warnmsg'>&#10071; Restart is required to apply new settings!
<form action='/system/restart'> <form action='/system/restart' method='post'><input type='hidden' name='confirmed' value='true' />
<input type='submit' value='Restart now' /> <input type='submit' value='Restart now' />
</form> </form>
</div> </div>

View file

@ -36,15 +36,66 @@
String Proc_WebPage_system(const String& var) { String Proc_WebPage_system(const String& var) {
if(TestHeaderFooter(var)) { if(TestHeaderFooter(var)) {
return AddHeaderFooter(var); return AddHeaderFooter(var);
} else if(var == "LOL") { } else if(var == "SUBNAV") {
return String("Nice"); return String(Page_system_HTML_SUBNAV);
} else { } else {
return String(); return String();
} }
} }
String Proc_WebPage_system_POST(const String& var) {
if(var == "SUBNAV") {
return String(Page_system_HTML_SUBNAV);
} else if(var == "SAVE_MSG") {
return String(Common_HTML_SAVE_MSG);
} else {
return Proc_WebPage_system(var);
}
}
String Proc_WebPage_system_POST_ERR(const String& var) {
if(var == "SUBNAV") {
return String(Page_system_HTML_SUBNAV);
} else if(var == "SAVE_MSG") {
return String(Common_HTML_SAVE_MSG_ERR);
} else {
return Proc_WebPage_system(var);
}
}
void WebPage_system(AsyncWebServerRequest *request) { void WebPage_system(AsyncWebServerRequest *request) {
if(request->method() == HTTP_POST) {
request->send_P(200, "text/html", Page_system_HTML, Proc_WebPage_system_POST);
Serial.println(":: [Webserver:system:output] [POST] hello");
if(request->hasParam("config.system.ntpOffset", true)) {
const AsyncWebParameter* p_ntpOffset = request->getParam("config.system.ntpOffset", true);
Serial.printf(":: [Webserver:system] POST[%s]: %s\n", p_ntpOffset->name().c_str(), p_ntpOffset->value().c_str());
config.system.ntpOffset = p_ntpOffset->value().toInt();
}
if(request->hasParam("config.system.httpLogSerial", true)) {
const AsyncWebParameter* p_httpLogSerial = request->getParam("config.system.httpLogSerial", true);
Serial.printf(":: [Webserver:system] POST[%s]: %s\n", p_httpLogSerial->name().c_str(), p_httpLogSerial->value().c_str());
config.system.httpLogSerial = p_httpLogSerial->value().toInt();
}
if(SaveConfig()) {
// we need a restart to apply the new settings
needRestart = true;
Serial.println(":: [Webserver:system] config saved");
request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_system_POST);
} else {
Serial.println("!! [Webserver:system] ERROR saving config ");
request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_system_POST_ERR);
}
} else {
request->send_P(200, "text/html", Page_system_HTML, Proc_WebPage_system); request->send_P(200, "text/html", Page_system_HTML, Proc_WebPage_system);
}
} }
@ -71,6 +122,9 @@ String Proc_WebPage_system_restart_POST(const String& var) {
void WebPage_system_restart(AsyncWebServerRequest *request) { void WebPage_system_restart(AsyncWebServerRequest *request) {
if(request->method() == HTTP_POST) { if(request->method() == HTTP_POST) {
if(request->hasParam("confirmed", true)) {
doRestart = false;
}
request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart_POST); request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart_POST);
if(request->hasParam("confirmed", true)) { if(request->hasParam("confirmed", true)) {
@ -184,3 +238,37 @@ void WebPage_system_wipe(AsyncWebServerRequest *request) {
} }
/*
* Subpage output
*/
String Proc_WebPage_system_output(const String& var) {
if(TestHeaderFooter(var)) {
return AddHeaderFooter(var);
} else if(var == "SUBNAV") {
return String(Page_system_HTML_SUBNAV);
} else{
return String();
}
}
String Proc_WebPage_system_output_POST(const String& var) {
if(var == "SAVE_MSG") {
return String(Common_HTML_SAVE_MSG);
} else {
return Proc_WebPage_system_output(var);
}
}
void WebPage_system_output(AsyncWebServerRequest *request) {
if(request->method() == HTTP_POST) {
request->send_P(200, "text/html", Page_system_output_HTML, Proc_WebPage_system_output_POST);
Serial.println(":: [Webserver:system:output] [POST] hello");
} else {
request->send_P(200, "text/html", Page_system_output_HTML, Proc_WebPage_system_output);
}
}

View file

@ -31,48 +31,35 @@
const char* Page_system_HTML PROGMEM = R"(%HEADER% const char* Page_system_HTML PROGMEM = R"(%HEADER%
<h2>&#9881; System settings</h2> <h2>&#9881; System settings</h2>
<ul class='subnav'> %SAVE_MSG%
<li><a href='/system/update'>&#x1F504; Firmware update</a></li> %SUBNAV%
<li><a href='/system/restart' >&#x1F501; CanGrow restart</a></li> <p>here you can set which features and sensors you use<br></p><form method='post' action='/system/'>
<li><a href='/system/wipe' >&#x1F4A3; Factory reset</a></li>
</ul> <u>NTP offset</u>:<br>
<p>here you can set which features and sensors you use<br></p><form method='post' action='/systemSettings/save'> <input class='inputShort' type='number' name='ntpOffset' min='-12' max='14' value='%CONFIG_SYSTEM_NTPOFFSET%' required> Hours<br>
Fan mode: <select id='UseFan' name='UseFan' required>
<option value='1'>Off</option> <u>Maintenance duration</u>:<br> <input class='inputShort' type='number' name='maintenanceDuration' min='0' max='900' value='%CONFIG_SYSTEM_MAINTDUR%' required> Seconds<br>
<option value='1' selected >Use</option>
<option value='0'>Do not use</option> <u>ESP32-Cam IP (optional)</u>:<br>
<input type='text' name='esp32camIp' maxlength='16' value='%CONFIG_SYSTEM_ESP32CAMIP%' ><br>
<u>HTTP log to serial</u>:<br>
<select name='config.system.httpLogSerial' required>
<option disabled value='' selected hidden>---</option>
<option value='1'>On</option>
<option value='0'>Off</option>
</select><br> </select><br>
Pump mode: <select id='UsePump' name='UsePump' required>
<option value='0' selected >Off</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select><br><p class='helpbox'><b>1:</b> Water every few days.<br> <b>2:</b> Water if the soil moisture falls below <i>Soilmoisture low</i> value<br> <b>3:</b> Water every few days if the soil moisture falls below <i>Soilmoisture low</i> value.</p>
Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>
<option value='1'>Yes</option>
<option value='0' selected >No</option>
</select><br>
Use relais for FAN: <select id='UseFANrelais' name='UseFANrelais' required>
<option value='1'>Yes</option>
<option value='0' selected >No</option>
</select><br>
Pump ON time: <input class='inputShort' type='number' name='PumpOnTime' min='0' max='255' value='1' required> Seconds<br>
Soilmoisture sensor: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>
<option value='1'>Analog capacitive</option>
<option value='2' selected >I2C Chirp</option>
</select><br>
Soilmoisture low: <input class='inputShort' type='number' name='SoilmoistureLow' min='0' value='22' required> %%<br>
Temperature sensor: <select id='TemperatureSensor_Type' name='TemperatureSensor_Type' required>
<option value='1' selected >I2C BME280</option>
<option value='2'>I2C Chirp</option>
</select><br>
NTP offset: <input class='inputShort' type='number' name='NtpOffset' min='-12' max='14' value='2' required> Hours<br>
Maintenance Duration: <input class='inputShort' type='number' name='MaintenanceDuration' min='0' max='900' value='900' required> Seconds<br>
ESP32-Cam IP (optional): <input type='text' name='Esp32CamIP' maxlength='16' value='' ><br>
<input type='submit' value='&#x1F4BE; Save settings'> <input type='submit' value='&#x1F4BE; Save settings'>
</form> </form>
%FOOTER%)"; %FOOTER%)";
const char* Page_system_HTML_SUBNAV PROGMEM = R"(<ul class='subnav'>
<li><a href='/system/output'>&#9889; Output configuration</a></li>
<li><a href='/system/update'>&#x1F504; Firmware update</a></li>
<li><a href='/system/restart' >&#x1F501; CanGrow restart</a></li>
<li><a href='/system/wipe' >&#x1F4A3; Factory reset</a></li>
</ul>)";
/* /*
* Subpage update * Subpage update
@ -144,3 +131,11 @@ Please confirm: <input type='checkbox' id='confirmed' name='confirmed' required
const char* Page_system_wipe_HTML_WIPE_MSG_POST PROGMEM = R"(Restarting...)"; const char* Page_system_wipe_HTML_WIPE_MSG_POST PROGMEM = R"(Restarting...)";
/*
* Subpage wipe
*/
const char* Page_system_output_HTML PROGMEM = R"(%HEADER%
<h1>&#9889; Output configuration</h1>
%FOOTER%)";