restart from webui implemented

This commit is contained in:
Marcus 2024-10-21 23:18:42 +02:00
parent de26abaf05
commit f093ac843b
7 changed files with 40 additions and 17 deletions

View file

@ -113,9 +113,9 @@ void setup() {
// blink with the onboard LED on D4/GPIO2 (PinWIPE) // blink with the onboard LED on D4/GPIO2 (PinWIPE)
for(byte i = 0; i <= 6 ; i++) { for(byte i = 0; i <= 6 ; i++) {
if(i % 2) { if(i % 2) {
digitalWrite(PinWIPE, LOW); digitalWrite(PinWIPE, 1 - PinWIPE_default);
} else { } else {
digitalWrite(PinWIPE, HIGH); digitalWrite(PinWIPE, PinWIPE_default);
} }
delay(333); delay(333);
} }
@ -128,7 +128,7 @@ void setup() {
// when PinWIPE is set to LOW, format LittleFS // when PinWIPE is set to LOW, format LittleFS
if(digitalRead(PinWIPE) != PinWIPE_default) { if(digitalRead(PinWIPE) != PinWIPE_default) {
LFS_format(); LFS_format();
Panic(); Restart();
} }
@ -142,6 +142,13 @@ void setup() {
bool alrdySaved = false; bool alrdySaved = false;
void loop() { void loop() {
unsigned long currentMillis = millis();
if(currentMillis - schedulerPrevMillis >= configSystem.schedulerInterval) {
}
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;
@ -155,4 +162,9 @@ void loop() {
} else { } else {
alrdySaved = false; alrdySaved = false;
} }
// if global var doRestart is true, perform a restart
if(doRestart == true) {
Restart();
}
} }

View file

@ -45,6 +45,9 @@
// do we need a restart? (e.g. after wifi settings change) // do we need a restart? (e.g. after wifi settings change)
bool needRestart; bool needRestart;
bool doRestart;
// previous value of millis within the scheduler loop
unsigned long schedulerPrevMillis = 0;
struct Config_WiFi { struct Config_WiFi {
char ssid[32]; char ssid[32];
@ -66,6 +69,7 @@ struct Config_System {
char httpUser[32]; char httpUser[32];
char httpPass[32]; char httpPass[32];
bool httpLogSerial; bool httpLogSerial;
unsigned short schedulerInterval = 1000;
}; };

View file

@ -29,20 +29,21 @@
// blink fast with the built in LED in an infinite loop // blink fast with the built in LED in an infinite loop
void Panic() { void Restart() {
Serial.println("!! PANIC !!"); Serial.println(":: [Restart] got triggered, restarting in 2 seconds");
byte i = 0; byte i = 0;
while(true) { while(i <= 16) {
if(i % 2) { if(i % 2) {
digitalWrite(PinWIPE, LOW); digitalWrite(PinWIPE, 1 - PinWIPE_default);
} else { } else {
digitalWrite(PinWIPE, HIGH); digitalWrite(PinWIPE, PinWIPE_default);
} }
i++; i++;
delay(125); delay(125);
} }
ESP.restart();
} }
@ -54,5 +55,3 @@ char* IP2Char(IPAddress ipaddr){
sprintf(buffer, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3] ); sprintf(buffer, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3] );
return buffer; return buffer;
} }

View file

@ -42,8 +42,8 @@ void LFS_Init() {
#ifdef ESP32 #ifdef ESP32
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
#endif #endif
Serial.println("!! [LittleFS] initializing FAILED !!"); Serial.println("!! [LittleFS] FAILED initializing. You have to format LittleFS manually. Will now restart.");
Panic(); Restart();
} }
} }
@ -55,9 +55,9 @@ void LFS_format() {
LittleFS.begin(); LittleFS.begin();
#endif #endif
if(LittleFS.format()) { if(LittleFS.format()) {
Serial.println(":: [LittleFS] formatting done!"); Serial.println(":: [LittleFS] done formatting");
} else { } else {
Serial.println("!! [LittleFS] formatting FAILED !!"); Serial.println("!! [LittleFS] FAILED formatting");
} }
} }
@ -203,6 +203,7 @@ bool LoadConfig() {
strlcpy(configSystem.httpUser, objSystem["httpUser"], sizeof(configSystem.httpUser)); strlcpy(configSystem.httpUser, objSystem["httpUser"], sizeof(configSystem.httpUser));
strlcpy(configSystem.httpPass, objSystem["httpPass"], sizeof(configSystem.httpPass)); strlcpy(configSystem.httpPass, objSystem["httpPass"], sizeof(configSystem.httpPass));
configSystem.httpLogSerial = objSystem["httpLogSerial"]; configSystem.httpLogSerial = objSystem["httpLogSerial"];
configSystem.schedulerInterval = objSystem["schedulerInterval"];
// * Grow * // * Grow *
JsonObject objGrow = doc["grow"][0]; JsonObject objGrow = doc["grow"][0];
@ -257,6 +258,7 @@ bool SaveConfig(bool writeToSerial = false) {
objSystem["httpUser"] = configSystem.httpUser; objSystem["httpUser"] = configSystem.httpUser;
objSystem["httpPass"] = configSystem.httpPass; objSystem["httpPass"] = configSystem.httpPass;
objSystem["httpLogSerial"] = configSystem.httpLogSerial; objSystem["httpLogSerial"] = configSystem.httpLogSerial;
objSystem["schedulerInterval"] = configSystem.schedulerInterval;
// * Grow * // * Grow *
JsonObject objGrow = doc["grow"].add<JsonObject>(); JsonObject objGrow = doc["grow"].add<JsonObject>();

View file

@ -29,7 +29,7 @@
void Wifi_Connect() { void Wifi_Connect() {
Serial.printf(":: [WiFi] connecting to ssid: %s\n", configWifi.ssid); Serial.printf(":: [WiFi] connecting to SSID: %s\n", configWifi.ssid);
WiFi.begin(configWifi.ssid, configWifi.password); WiFi.begin(configWifi.ssid, configWifi.password);
if(configWifi.dhcp == false) { if(configWifi.dhcp == false) {
Serial.println(":: [WiFi] using static ip configuration:"); Serial.println(":: [WiFi] using static ip configuration:");
@ -76,7 +76,6 @@ void Wifi_Init() {
Serial.println(":: [WiFi] configWifi.ssid is unset"); Serial.println(":: [WiFi] configWifi.ssid is unset");
Wifi_AP(); Wifi_AP();
} else { } else {
Serial.printf(":: [WiFi] connecting to SSID: %s\n", configWifi.ssid);
Wifi_Connect(); Wifi_Connect();
} }
} }

View file

@ -72,6 +72,13 @@ 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) {
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)) {
Serial.println(":: [Webserver:system:restart] POST[confirmed]: is set, triggering restart");
// force a small delay to ensure client has received http payload
doRestart = true;
}
} else { } else {
request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart); request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart);
} }

View file

@ -61,7 +61,7 @@ Soilmoisture sensor: <select id='MoistureSensor_Type' name='MoistureSensor_Type'
<option value='1'>Analog capacitive</option> <option value='1'>Analog capacitive</option>
<option value='2' selected >I2C Chirp</option> <option value='2' selected >I2C Chirp</option>
</select><br> </select><br>
Soilmoisture low: <input class='inputShort' type='number' name='SoilmoistureLow' min='0' value='22' required> %<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> Temperature sensor: <select id='TemperatureSensor_Type' name='TemperatureSensor_Type' required>
<option value='1' selected >I2C BME280</option> <option value='1' selected >I2C BME280</option>
<option value='2'>I2C Chirp</option> <option value='2'>I2C Chirp</option>