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
1 changed files with 62 additions and 17 deletions

View File

@ -80,7 +80,7 @@ char WebUiPassword[32] = "cangrow";
// configured - if false, let the user configure system settings first
bool configured = false;
// NTP Offset
int ntpOffset;
byte ntpOffset;
// MoistureSensor_Type - contains which moisture sensor to use
// 1: analog capacitive sensor
// 2: I2C chirp sensor from catnip electronics
@ -314,7 +314,7 @@ const char HTMLsuccess[] PROGMEM = R"EOF(
const char HTMLneedRestart[] PROGMEM = R"EOF(
<div class='warnmsg'>Restart is required to apply new WiFi settings!
<form action="/restart">
<form action="/system/restart">
<input type="submit" value="Restart now" />
</form>
</div>
@ -998,8 +998,8 @@ void loop() {
// first we call webserver handle client
webserver.handleClient();
// do every second
if(currentRuntime - outputPrevTime >= 1000) {
// do every second when everything is configured
if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){
// debug output
Serial.print("secondsSunrise: ");
Serial.println(secondsSunrise);
@ -1058,7 +1058,9 @@ void WebHandler() {
webserver.on("/help", HTTP_GET, WEBhelp);
// 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
//webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); });
@ -1071,12 +1073,13 @@ void WebHandler() {
// switching MOSFETs
webserver.on("/switch", HTTP_POST, POSTswitchMOSFET);
}
void WebRestart() {
void SysRestart() {
String body = FPSTR(HTMLheader);
// TODO only debug and development solution, remove this later
if(true) {
if( (webserver.hasArg("confirmed")) || (NeedRestart == true) ) {
body += "<h1>Restarting</h1>";
body += "<div class='infomsg'>After restart CanGrow will be connected to WiFi SSID<br><b>";
body += WIFIssid;
@ -1087,9 +1090,32 @@ void WebRestart() {
delay(1000);
ESP.restart();
} 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();
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 += "<br>";
body += "Day of Grow: ";
body += DayOfGrow;
body += "<br><br>";
body += int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24))) ;
body += "<br>";
body += "Soil Moisture: ";
body += getSoilmoisture(MoistureSensor_Type);
body += " %<br>";
@ -1343,10 +1369,34 @@ void WEBwifiSettings() {
if(webserver.hasArg("success")) {
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 += "<form method='post' action='/wifiSettings/save'>\n";
body += "SSID: <select id='WIFIssid' name='WIFIssid' required>\n";
body += "<option disabled value='' selected hidden>-Select your network-</option>";
// build option list for selecting wifi
Serial.println("Available Wifis: ");
for(int i = 0 ; i < ssidsAvail; i++) {
@ -1356,11 +1406,6 @@ void WEBwifiSettings() {
body += wifiName + "</option>\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 += "IP: <input type='text' name='WIFIip'><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+= "' required><br>\n";