firmware wip

This commit is contained in:
Marcus 2024-04-16 01:51:24 +02:00
parent 8c5742f45f
commit 0becdf380a
1 changed files with 125 additions and 81 deletions

View File

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