From f093ac843b4b4727abb592d2beabc9f1878f9b71 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 21 Oct 2024 23:18:42 +0200 Subject: [PATCH] restart from webui implemented --- Arduino/CanGrow/CanGrow.ino | 18 +++++++++++++++--- Arduino/CanGrow/include/CanGrow.h | 4 ++++ Arduino/CanGrow/include/CanGrow_Core.h | 13 ++++++------- Arduino/CanGrow/include/CanGrow_LittleFS.h | 10 ++++++---- Arduino/CanGrow/include/CanGrow_Wifi.h | 3 +-- .../CanGrow/include/Webserver/Page_system.h | 7 +++++++ .../include/Webserver/Page_system_HTML.h | 2 +- 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index b6cbaa3..a8e6b5f 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -113,9 +113,9 @@ void setup() { // blink with the onboard LED on D4/GPIO2 (PinWIPE) for(byte i = 0; i <= 6 ; i++) { if(i % 2) { - digitalWrite(PinWIPE, LOW); + digitalWrite(PinWIPE, 1 - PinWIPE_default); } else { - digitalWrite(PinWIPE, HIGH); + digitalWrite(PinWIPE, PinWIPE_default); } delay(333); } @@ -128,7 +128,7 @@ void setup() { // when PinWIPE is set to LOW, format LittleFS if(digitalRead(PinWIPE) != PinWIPE_default) { LFS_format(); - Panic(); + Restart(); } @@ -142,6 +142,13 @@ void setup() { bool alrdySaved = false; void loop() { + unsigned long currentMillis = millis(); + + if(currentMillis - schedulerPrevMillis >= configSystem.schedulerInterval) { + + } + + 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; @@ -155,4 +162,9 @@ void loop() { } else { alrdySaved = false; } + + // if global var doRestart is true, perform a restart + if(doRestart == true) { + Restart(); + } } diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index b6a2574..4d35988 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -45,6 +45,9 @@ // do we need a restart? (e.g. after wifi settings change) bool needRestart; +bool doRestart; +// previous value of millis within the scheduler loop +unsigned long schedulerPrevMillis = 0; struct Config_WiFi { char ssid[32]; @@ -66,6 +69,7 @@ struct Config_System { char httpUser[32]; char httpPass[32]; bool httpLogSerial; + unsigned short schedulerInterval = 1000; }; diff --git a/Arduino/CanGrow/include/CanGrow_Core.h b/Arduino/CanGrow/include/CanGrow_Core.h index 6793550..10a69cd 100644 --- a/Arduino/CanGrow/include/CanGrow_Core.h +++ b/Arduino/CanGrow/include/CanGrow_Core.h @@ -29,20 +29,21 @@ // blink fast with the built in LED in an infinite loop -void Panic() { - Serial.println("!! PANIC !!"); +void Restart() { + Serial.println(":: [Restart] got triggered, restarting in 2 seconds"); byte i = 0; - while(true) { + while(i <= 16) { if(i % 2) { - digitalWrite(PinWIPE, LOW); + digitalWrite(PinWIPE, 1 - PinWIPE_default); } else { - digitalWrite(PinWIPE, HIGH); + digitalWrite(PinWIPE, PinWIPE_default); } i++; 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] ); return buffer; } - - diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 45e0b02..6902447 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -42,8 +42,8 @@ void LFS_Init() { #ifdef ESP32 if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { #endif - Serial.println("!! [LittleFS] initializing FAILED !!"); - Panic(); + Serial.println("!! [LittleFS] FAILED initializing. You have to format LittleFS manually. Will now restart."); + Restart(); } } @@ -55,9 +55,9 @@ void LFS_format() { LittleFS.begin(); #endif if(LittleFS.format()) { - Serial.println(":: [LittleFS] formatting done!"); + Serial.println(":: [LittleFS] done formatting"); } 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.httpPass, objSystem["httpPass"], sizeof(configSystem.httpPass)); configSystem.httpLogSerial = objSystem["httpLogSerial"]; + configSystem.schedulerInterval = objSystem["schedulerInterval"]; // * Grow * JsonObject objGrow = doc["grow"][0]; @@ -257,6 +258,7 @@ bool SaveConfig(bool writeToSerial = false) { objSystem["httpUser"] = configSystem.httpUser; objSystem["httpPass"] = configSystem.httpPass; objSystem["httpLogSerial"] = configSystem.httpLogSerial; + objSystem["schedulerInterval"] = configSystem.schedulerInterval; // * Grow * JsonObject objGrow = doc["grow"].add(); diff --git a/Arduino/CanGrow/include/CanGrow_Wifi.h b/Arduino/CanGrow/include/CanGrow_Wifi.h index 4533a77..ae6d1c8 100644 --- a/Arduino/CanGrow/include/CanGrow_Wifi.h +++ b/Arduino/CanGrow/include/CanGrow_Wifi.h @@ -29,7 +29,7 @@ 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); if(configWifi.dhcp == false) { Serial.println(":: [WiFi] using static ip configuration:"); @@ -76,7 +76,6 @@ void Wifi_Init() { Serial.println(":: [WiFi] configWifi.ssid is unset"); Wifi_AP(); } else { - Serial.printf(":: [WiFi] connecting to SSID: %s\n", configWifi.ssid); Wifi_Connect(); } } diff --git a/Arduino/CanGrow/include/Webserver/Page_system.h b/Arduino/CanGrow/include/Webserver/Page_system.h index 932e715..a4dee70 100644 --- a/Arduino/CanGrow/include/Webserver/Page_system.h +++ b/Arduino/CanGrow/include/Webserver/Page_system.h @@ -72,6 +72,13 @@ String Proc_WebPage_system_restart_POST(const String& var) { void WebPage_system_restart(AsyncWebServerRequest *request) { if(request->method() == HTTP_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 { request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart); } diff --git a/Arduino/CanGrow/include/Webserver/Page_system_HTML.h b/Arduino/CanGrow/include/Webserver/Page_system_HTML.h index a6d5a2b..8e6b35e 100644 --- a/Arduino/CanGrow/include/Webserver/Page_system_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_system_HTML.h @@ -61,7 +61,7 @@ Soilmoisture sensor:
-Soilmoisture low: %
+Soilmoisture low: %%
Temperature sensor: