firmware wip - ntpOffset now byte , add wipe function, let the user restart the device at any point

This commit is contained in:
Marcus 2024-04-18 15:15:59 +02:00
parent 7365fc2b85
commit ae51581a4b

View file

@ -80,7 +80,7 @@ char WebUiPassword[32] = "cangrow";
// configured - if false, let the user configure system settings first // configured - if false, let the user configure system settings first
bool configured = false; bool configured = false;
// NTP Offset // NTP Offset
int ntpOffset; byte ntpOffset;
// MoistureSensor_Type - contains which moisture sensor to use // MoistureSensor_Type - contains which moisture sensor to use
// 1: analog capacitive sensor // 1: analog capacitive sensor
// 2: I2C chirp sensor from catnip electronics // 2: I2C chirp sensor from catnip electronics
@ -314,7 +314,7 @@ const char HTMLsuccess[] PROGMEM = R"EOF(
const char HTMLneedRestart[] PROGMEM = R"EOF( const char HTMLneedRestart[] PROGMEM = R"EOF(
<div class='warnmsg'>Restart is required to apply new WiFi settings! <div class='warnmsg'>Restart is required to apply new WiFi settings!
<form action="/restart"> <form action="/system/restart">
<input type="submit" value="Restart now" /> <input type="submit" value="Restart now" />
</form> </form>
</div> </div>
@ -998,8 +998,8 @@ void loop() {
// first we call webserver handle client // first we call webserver handle client
webserver.handleClient(); webserver.handleClient();
// do every second // do every second when everything is configured
if(currentRuntime - outputPrevTime >= 1000) { if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){
// debug output // debug output
Serial.print("secondsSunrise: "); Serial.print("secondsSunrise: ");
Serial.println(secondsSunrise); Serial.println(secondsSunrise);
@ -1058,7 +1058,9 @@ void WebHandler() {
webserver.on("/help", HTTP_GET, WEBhelp); webserver.on("/help", HTTP_GET, WEBhelp);
// restart when NeedRestart is true // restart when NeedRestart is true
webserver.on("/restart", HTTP_GET, WebRestart); webserver.on("/system/restart", HTTP_GET, SysRestart);
// wipe eeprom triggered from WebGui
webserver.on("/system/wipe", HTTP_GET, SysWipe);
// does not work atm TODO // does not work atm TODO
//webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); }); //webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); });
@ -1071,12 +1073,13 @@ void WebHandler() {
// switching MOSFETs // switching MOSFETs
webserver.on("/switch", HTTP_POST, POSTswitchMOSFET); webserver.on("/switch", HTTP_POST, POSTswitchMOSFET);
} }
void WebRestart() { void SysRestart() {
String body = FPSTR(HTMLheader); String body = FPSTR(HTMLheader);
// TODO only debug and development solution, remove this later // TODO only debug and development solution, remove this later
if(true) { if( (webserver.hasArg("confirmed")) || (NeedRestart == true) ) {
body += "<h1>Restarting</h1>"; body += "<h1>Restarting</h1>";
body += "<div class='infomsg'>After restart CanGrow will be connected to WiFi SSID<br><b>"; body += "<div class='infomsg'>After restart CanGrow will be connected to WiFi SSID<br><b>";
body += WIFIssid; body += WIFIssid;
@ -1087,9 +1090,32 @@ void WebRestart() {
delay(1000); delay(1000);
ESP.restart(); ESP.restart();
} else { } else {
body += "<div class='warnmsg'><h1>Not allowed</h1></div>"; body += "<h1>Restart CanGrow</h1>";
body += "<div class='infomsg'>Do you want to restart CanGrow?";
body += "<br>Please confirm.";
body += "<form action='/system/restart'><input type='hidden' name='confirmed' value='true' /><input type='submit' value='Confirm restart' /></form>";
body += "</div>";
body += returnHTMLfooter(); body += returnHTMLfooter();
webserver.send(405, "text/html", body); webserver.send(200, "text/html", body);
}
}
void SysWipe() {
String body = FPSTR(HTMLheader);
// TODO only debug and development solution, remove this later
if(webserver.hasArg("confirmed")) {
body += "<div class='warnmsg'><h2>!! Wiping CanGrow's EEPROM !!</h2><br>Device will restart in a few seconds.</div>";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
wipeEEPROM();
} else {
body += "<h1>Wipeing EEPROM</h1>";
body += "<div class='warnmsg'>All settings will be removed!!<br>";
body += "<br>Please confirm wiping the EEPROM";
body += "<form action='/system/wipe'><input type='hidden' name='confirmed' value='true' /><input type='submit' value='Confirm wiping' /></form>";
body += "</div>";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
} }
} }
@ -1261,8 +1287,8 @@ void WEBroot() {
body += returnStrDateFromEpoch(GrowStart); body += returnStrDateFromEpoch(GrowStart);
body += "<br>"; body += "<br>";
body += "Day of Grow: "; body += "Day of Grow: ";
body += DayOfGrow; body += int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24))) ;
body += "<br><br>"; body += "<br>";
body += "Soil Moisture: "; body += "Soil Moisture: ";
body += getSoilmoisture(MoistureSensor_Type); body += getSoilmoisture(MoistureSensor_Type);
body += " %<br>"; body += " %<br>";
@ -1343,10 +1369,34 @@ void WEBwifiSettings() {
if(webserver.hasArg("success")) { if(webserver.hasArg("success")) {
body += FPSTR(HTMLsuccess); body += FPSTR(HTMLsuccess);
} }
if(FirstRun == false) {
body += "<u>Current Settings:</u><br>";
body += "WiFi SSID: <b>";
body += WIFIssid;
body += "</b><br>\n";
body += "Use DHCP: <b>";
body += WIFIuseDHCP;
body += "</b><br>\n";
body += "IP address: <b>";
body += WiFi.localIP().toString();
body += "</b><br>\n";
body += "Subnet mask: <b>";
body += WiFi.subnetMask().toString();
body += "</b><br>\n";
body += "Gateway: <b>";
body += WiFi.gatewayIP().toString();
body += "</b><br>\n";
body += "DNS: <b>";
body += WiFi.dnsIP().toString();
body += "</b><br><br>\n";
}
body += "<p>Select your wifi network from the SSID list.<br>To use DHCP leave IP, Subnet, Gateway and DNS fields blank!</p>"; body += "<p>Select your wifi network from the SSID list.<br>To use DHCP leave IP, Subnet, Gateway and DNS fields blank!</p>";
body += "<form method='post' action='/wifiSettings/save'>\n"; body += "<form method='post' action='/wifiSettings/save'>\n";
body += "SSID: <select id='WIFIssid' name='WIFIssid' required>\n"; body += "SSID: <select id='WIFIssid' name='WIFIssid' required>\n";
body += "<option disabled value='' selected hidden>-Select your network-</option>"; body += "<option disabled value='' selected hidden>-Select your network-</option>";
// build option list for selecting wifi // build option list for selecting wifi
Serial.println("Available Wifis: "); Serial.println("Available Wifis: ");
for(int i = 0 ; i < ssidsAvail; i++) { for(int i = 0 ; i < ssidsAvail; i++) {
@ -1356,11 +1406,6 @@ void WEBwifiSettings() {
body += wifiName + "</option>\n"; body += wifiName + "</option>\n";
} }
body += "</select><br>\n"; body += "</select><br>\n";
if(strlen(WIFIssid) > 0) {
body += "Currently connected to: <b>";
body += WIFIssid;
body += "</b><br>\n";
}
body += "Password: <input type='password' name='WIFIpassword'><br>\n"; body += "Password: <input type='password' name='WIFIpassword'><br>\n";
body += "IP: <input type='text' name='WIFIip'><br>\n"; body += "IP: <input type='text' name='WIFIip'><br>\n";
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>\n"; body += "Subnet mask: <input type='text' name='WIFInetmask'><br>\n";
@ -1492,7 +1537,7 @@ void WEBgrowSettings() {
body += "Grow name: <input type='text' name='GrowName' value='"; body += "Grow name: <input type='text' name='GrowName' maxlength='32' value='";
body += GrowName; body += GrowName;
body+= "' required><br>\n"; body+= "' required><br>\n";