firmware wip

This commit is contained in:
Marcus 2024-04-16 01:51:24 +02:00
parent 8c5742f45f
commit 0becdf380a

View file

@ -70,7 +70,7 @@ char WebUiPassword[32] = "cangrow";
// GrowName - contains the name of the grow/plant. Up to 32 byte // GrowName - contains the name of the grow/plant. Up to 32 byte
char GrowName[32]; char GrowName[32];
// GrowStart - contains unix timestamp from date where grow starts (00:00) // GrowStart - contains unix timestamp from date where grow starts (00:00)
// unsigned long is 4 byte // unsigned long is 8 byte
unsigned long GrowStart; unsigned long GrowStart;
// DayOfGrow contains on which day the grow is // DayOfGrow contains on which day the grow is
byte DayOfGrow; byte DayOfGrow;
@ -153,10 +153,10 @@ const char HTMLheader[] PROGMEM = R"EOF(
<body> <body>
<ul class="nav"> <ul class="nav">
<li><a href="/">CanGrow</a></li> <li><a href="/">CanGrow</a></li>
<li><a href="/wifiConfig">WiFi Config</a></li> <li><a id="growSettings" href="/growSettings">Grow settings</a></li>
<li><a href="#">Menu item 2</a></li> <li><a id="systemSettings" href="/systemSettings">System settings</a></li>
<li><a href="#">Menu item 3</a></li> <li><a href="/wifiSettings">WiFi settings</a></li>
<li><a href="#">Menu item 4</a></li> <li><a href="#">Help</a></li>
</ul> </ul>
<div class="center"> <div class="center">
@ -181,6 +181,10 @@ body {
} }
h1, h2, h3, h4, h5 {
text-align: center;
}
a:link, a:visited { a:link, a:visited {
color: #04AA6D; color: #04AA6D;
} }
@ -222,6 +226,12 @@ a:active {
list-style: none; list-style: none;
} }
.nav li:first-of-type {
background: #026b45;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.nav li a { .nav li a {
color: #ddd; color: #ddd;
display: block; display: block;
@ -234,6 +244,7 @@ a:active {
.nav li a:hover { .nav li a:hover {
background: #04AA6D; background: #04AA6D;
color: #fff; color: #fff;
border-radius: 3px;
} }
.nav li a:active { .nav li a:active {
@ -242,6 +253,15 @@ a:active {
)EOF";
const char HTMLjsNoWifi[] PROGMEM = R"EOF(
<script>document.getElementById('growSettings').style.display = 'none'; document.getElementById('systemSettings').style.display = 'none';</script>
)EOF";
const char HTMLhelp[] PROGMEM = R"EOF(
<h1>CanGrow help</h1>
Here you will get some helpful help.
)EOF"; )EOF";
/* /*
@ -547,27 +567,19 @@ void wipeEEPROM() {
bool loadEEPROM() { bool loadEEPROM() {
Serial.println(":: loading EEPROM ::"); Serial.println(":: loading EEPROM ::");
/*
* configured
* // read var WIFIssid from address 0, 32 byte long
* read var configured from address 511 - I put this to the end to // read this first, because we decide on the ssid length (>0?) if
* prevent confusion with the 1 byte offset in the address when it // we run in unconfigured AP mode, nor not
* would be at the beginning - more a cosmetic thing EEPROM.get(0, WIFIssid);
*
* All boolean variables are at the end of the EEPROM
*/
EEPROM.get(511, configured);
Serial.print("configured: ");
Serial.println(configured);
// when configured is > 1 (it should == 1) then read EEPROM furher data // when length is > 0 then read furter EEPROM config data
if(configured > 0) { if(strlen(WIFIssid)) {
/* /*
* WIFI settings * WIFI settings
*/ */
// read var WIFIssid from address 0, 32 byte long
EEPROM.get(0, WIFIssid);
// read var WIFIpassword from address 32, 64 byte long // read var WIFIpassword from address 32, 64 byte long
EEPROM.get(32, WIFIpassword); EEPROM.get(32, WIFIpassword);
// read var WIFIip from address 96, 16 byte long // read var WIFIip from address 96, 16 byte long
@ -585,7 +597,16 @@ bool loadEEPROM() {
/* /*
* System settings * System settings
*/ */
/*
* configured
*
* read var configured from address 511 - I put this to the end to
* prevent confusion with the 1 byte offset in the address when it
* would be at the beginning - more a cosmetic thing
*
* All boolean variables are at the end of the EEPROM
*/
EEPROM.get(511, configured);
EEPROM.get(160, WebUiUsername); EEPROM.get(160, WebUiUsername);
EEPROM.get(176, WebUiPassword); EEPROM.get(176, WebUiPassword);
@ -598,15 +619,18 @@ bool loadEEPROM() {
// print values to Serial output // print values to Serial output
Serial.println(":: EEPROM loaded ::");
Serial.print("WIFIssid: "); Serial.print("WIFIssid: ");
Serial.println(WIFIssid); Serial.println(WIFIssid);
Serial.print("Use DHCP: "); Serial.print("Use DHCP: ");
Serial.println(WIFIuseDHCP); Serial.println(WIFIuseDHCP);
Serial.print("configured: ");
Serial.println(configured);
} else {
Serial.println("EEPROM value WIFIssid is empty");
} }
Serial.println(":: EEPROM loaded ::"); Serial.println(":: EEPROM loaded ::");
return(configured); return(strlen(WIFIssid));
} }
void wifiConnect() { void wifiConnect() {
@ -744,20 +768,16 @@ void setup() {
// connect to wifi // connect to wifi
wifiConnect(); wifiConnect();
// use webhandler for configured state
WebHandler_configured();
// configured is 0, setup Access Point // configured is 0, setup Access Point
} else { } else {
// start an wifi accesspoint // start an wifi accesspoint
wifiAp(); wifiAp();
// use webhandler for unconfigured state
WebHandler_unconfigured();
} }
// general webHandler for wifiConfig, 404, ... // set web handler
WebHandler_general(); WebHandler();
// start webserver // start webserver
webserver.begin(); webserver.begin();
@ -781,13 +801,11 @@ void loop() {
* Web Handler * Web Handler
*/ */
void WebHandler() {
/*
void WebHandler_general() {
/*
* Webserver handlers * Webserver handlers
* here are the generic webserver handlers like 404 not found * here are the generic webserver handlers like 404 not found
* wifiConfig, ... * wifiSettings, ...
* *
* if you are looking for the single webpages handler, have a look to * if you are looking for the single webpages handler, have a look to
* *
@ -795,10 +813,12 @@ void WebHandler_general() {
*/ */
// generic handler // generic handler
// WiFi Stuff // WiFi Stuff
webserver.on("/wifiConfig", HTTP_GET, WEBwifiConfig); webserver.on("/wifiSettings", HTTP_GET, WEBwifiSettings);
webserver.on("/wifiConfig/save", HTTP_POST, POSTwifiConfig); webserver.on("/wifiSettings/save", HTTP_POST, POSTwifiSettings);
webserver.on("/style.css", HTTP_GET, WEBstyleCSS); webserver.on("/style.css", HTTP_GET, WEBstyleCSS);
// 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!"); });
// 404 handling // 404 handling
@ -806,25 +826,25 @@ void WebHandler_general() {
// failed whole page every call. we can save up this 0,5kb traffic :o) // failed whole page every call. we can save up this 0,5kb traffic :o)
webserver.on("/favicon.ico", [](){ webserver.send(404, "text/html", "404 - not found"); }); webserver.on("/favicon.ico", [](){ webserver.send(404, "text/html", "404 - not found"); });
webserver.onNotFound(WEB404); webserver.onNotFound(WEB404);
if(strlen(WIFIssid) < 1) {
webserver.on("/", HTTP_GET, WEBwifiSettings);
}
if(configured == false) {
webserver.on("/", HTTP_GET, WEBsystemSettings);
webserver.on("/growSettings", HTTP_GET, WEBsystemSettings);
}
webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings);
if(GrowStart < 1) {
webserver.on("/", HTTP_GET, WEBgrowSettings);
}
webserver.on("/growSettings", HTTP_GET, WEBgrowSettings);
} }
void WebHandler_unconfigured() {
/*
* WebHandler_unconfigured()
*
* Here all URL routings are defined, in unconfigured state
*/
webserver.on("/", HTTP_GET, WEBrootUnconfigured);
}
void WebHandler_configured() {
/*
* WebHandler_unconfigured()
*
* Here all URL routings are defined, in configured state
*/
webserver.on("/", HTTP_GET, WEBroot);
}
void WebAuth() { void WebAuth() {
/* /*
@ -883,20 +903,48 @@ void WEBlogout() {
webserver.send(401, "text/html", body); webserver.send(401, "text/html", body);
} }
void WEBhelp() {
String body = FPSTR(HTMLheader);
body += FPSTR(HTMLhelp);
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
/*
* Root pages
*/
void WEBroot() {
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
body += "<p>";
body += timeClient.getFormattedTime();
body += "</p>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
/* /*
* Config pages * Config pages
*/ */
void WEBwifiConfig() { void WEBwifiSettings() {
byte ssidsAvail = WiFi.scanNetworks(); byte ssidsAvail = WiFi.scanNetworks();
String body = FPSTR(HTMLheader); String body = FPSTR(HTMLheader);
if(strlen(WIFIssid) == 0) {
body += "<h1>CanGrow</h1>";
body += "<p>CanGrow is actually unconfigured. You need to Setup your WiFi below first.<br>";
body += "<br>After you configured the WiFi connection successfully, you can start your grow &#129382;";
body += "<br>";
body += "</p>";
}
body += "<h1>WiFi config</h1>\n"; body += "<h1>WiFi config</h1>\n";
if(webserver.hasArg("success")) { if(webserver.hasArg("success")) {
body += "<div class='infomsg'>Successfully saved!</div>"; body += "<div class='infomsg'>Successfully saved!<br>Please restart the device.</div>";
} }
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='/wifiConfig/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
@ -918,33 +966,35 @@ void WEBwifiConfig() {
body += "DNS: <input type='text' name='WIFIdns'><br>\n"; body += "DNS: <input type='text' name='WIFIdns'><br>\n";
body += "<input type='submit' value='Save'>\n"; body += "<input type='submit' value='Save'>\n";
body += "</form>\n"; body += "</form>\n";
body += FPSTR(HTMLjsNoWifi);
body += FPSTR(HTMLfooter); body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body); webserver.send(200, "text/html", body);
} }
void WEBroot() { void WEBsystemSettings() {
String body = FPSTR(HTMLheader); String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>"; body += "<h1>System settings</h1>";
body += "<p>"; body += "<p>here you can set which features and sensors you use<br>";
body += timeClient.getFormattedTime();
body += "</p>"; body += "</p>";
body += FPSTR(HTMLfooter); body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body); webserver.send(200, "text/html", body);
} }
void WEBrootUnconfigured() { /*
* Grow pages
*/
void WEBgrowSettings() {
String body = FPSTR(HTMLheader); String body = FPSTR(HTMLheader);
body += "<h1>CanGrow</h1>"; body += "<h1>Grow Settings</h1>";
body += "<p>CanGrow is actually unconfigured. You need to <a href='./wifiConfig'>Configure WiFi</a>.<br>"; body += "<p>Here you can set everything grow related, like light hours, how much water, LED brightnes<br>";
body += "<br>After you configured the WiFi connection successfully, you can start your grow &#129382;";
body += "<br>";
body += "</p>"; body += "</p>";
body += FPSTR(HTMLfooter); body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body); webserver.send(200, "text/html", body);
} }
/* /*
* *
@ -952,7 +1002,7 @@ void WEBrootUnconfigured() {
* *
*/ */
void POSTwifiConfig() { void POSTwifiSettings() {
String WIFIssid_new = webserver.arg("WIFIssid"); String WIFIssid_new = webserver.arg("WIFIssid");
String WIFIpassword_new = webserver.arg("WIFIpassword"); String WIFIpassword_new = webserver.arg("WIFIpassword");
String WIFIip_new = webserver.arg("WIFIip"); String WIFIip_new = webserver.arg("WIFIip");
@ -976,7 +1026,7 @@ void POSTwifiConfig() {
WIFIuseDHCP = true; WIFIuseDHCP = true;
} }
configured = true;
EEPROM.put(0, WIFIssid); EEPROM.put(0, WIFIssid);
EEPROM.put(32, WIFIpassword); EEPROM.put(32, WIFIpassword);
@ -985,11 +1035,10 @@ void POSTwifiConfig() {
EEPROM.put(128, WIFIgateway); EEPROM.put(128, WIFIgateway);
EEPROM.put(144, WIFIdns); EEPROM.put(144, WIFIdns);
EEPROM.put(510, WIFIuseDHCP); EEPROM.put(510, WIFIuseDHCP);
EEPROM.put(511, configured);
EEPROM.commit(); EEPROM.commit();
Serial.println(":: POSTwifiConfig ::"); Serial.println(":: POSTwifiSettings ::");
Serial.print("WIFIssid: "); Serial.print("WIFIssid: ");
Serial.println(WIFIssid_new); Serial.println(WIFIssid_new);
@ -1007,15 +1056,10 @@ void POSTwifiConfig() {
Serial.println(WIFIdns_new); Serial.println(WIFIdns_new);
Serial.print("WIFIuseDHCP: "); Serial.print("WIFIuseDHCP: ");
Serial.println(WIFIuseDHCP); Serial.println(WIFIuseDHCP);
Serial.print("configured: ");
Serial.println(configured);
String body = FPSTR(HTMLheader);
body += "<h1>WiFi Config</h1>"; webserver.sendHeader("Location", String("/wifiSettings?success"), true);
body += "<p>WiFi settings successfully saved. Please restart the device.</p>"; webserver.send(302, "text/plain", "wifiSettings/save: success!");
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
} }