firmware WIP

This commit is contained in:
Marcus 2024-04-13 21:41:36 +02:00
parent 3b374e32b4
commit a4eec2ebc8

View file

@ -45,9 +45,11 @@ float valHumidity;
bool configured;
char WIFIssid[32];
char WIFIpassword[64];
//int WIFIssidsAvail;
IPAddress WIFIip(192,168,4,20);
IPAddress WIFInetmask(255,255,255,0);
IPAddress WIFIgateway(192,168,4,254);
IPAddress WIFIdns(0,0,0,0);
// GrowName - contains the name of the grow/plant. Up to 32 byte
char GrowName[32];
@ -91,8 +93,14 @@ byte SoilmoistureLow;
* WiFi
*/
const char *APssid = "CanGrow-unconfigured";
const char *APpass = "CanGrow";
const char* APssid = "CanGrow-unconfigured";
/*
* TODO - does not work atm. idk why.
* const char* APpass = "CanGrow";
const int APchannel = 6;
const bool APhidden = false;
*
*/
/*
@ -424,7 +432,7 @@ void wipeEEPROM() {
bool loadEEPROM() {
Serial.println(":: loading EEPROM ::");
/*
* configured
*
@ -433,6 +441,8 @@ bool loadEEPROM() {
* would be at the beginning - more a cosmetic thing
*/
EEPROM.get(511, configured);
Serial.print("configured: ");
Serial.println(configured);
// when configured is > 1 (it should == 1) then read EEPROM furher data
if(configured > 0) {
@ -452,6 +462,8 @@ bool loadEEPROM() {
EEPROM.get(112, WIFInetmask);
// read var WIFIgateway from address 128, 16 byte long
EEPROM.get(128, WIFIgateway);
// read var WIFIgateway from address 128, 16 byte long
EEPROM.get(144, WIFIdns);
/*
* Grow data
@ -468,14 +480,43 @@ bool loadEEPROM() {
Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword);
}
Serial.print("configured: ");
Serial.println(configured);
Serial.println(":: EEPROM loaded ::");
return(configured);
}
void wifiConnect() {
Serial.println(":: Connecting to WiFi ::");
Serial.print("SSID: ");
Serial.println(WIFIssid);
// Start WiFi connection
WiFi.begin(WIFIssid, WIFIpassword);
WiFi.config(WIFIip, WIFIdns, WIFIgateway);
// wait until WiFi connection is established
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void wifiAp() {
Serial.println(":: Creating Accesspoint ::");
// configure WiFi Access Point
WiFi.softAPConfig(WIFIip, WIFIgateway, WIFInetmask);
// start Access Point
// TODO make AP with password - does not work atm. idk why.
WiFi.softAP(APssid);
Serial.print("SSID: ");
Serial.println(APssid);
Serial.print("CanGrow IP address: ");
Serial.println(WiFi.softAPIP());
}
/*
* Setup
@ -554,42 +595,21 @@ void setup() {
*/
// load stored values from EEPROM
// load stored values from EEPROM and check what var configured is returned
if(loadEEPROM()) {
Serial.print("Configuration found in EEPROM, connect to Wifi ");
Serial.println(WIFIssid);
// Start WiFi connection
WiFi.begin(WIFIssid, WIFIpassword);
// connect to wifi
wifiConnect();
// wait until WiFi connection is established
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
// use webhandlers for configured state
// use webhandler for configured state
WebHandler_configured();
// no data found in EEPROM, setup Access Point
// configured is 0, setup Access Point
} else {
Serial.println("Creating Accesspoint");
Serial.print("SSID: ");
Serial.println(APssid);
Serial.print("Password: ");
Serial.println(APpass);
// configure WiFi Access Point
WiFi.softAPConfig(WIFIip, WIFIgateway, WIFInetmask);
// start Access Point
WiFi.softAP(APssid, APpass);
Serial.print("CanGrow IP address: ");
Serial.println(WiFi.softAPIP());
// use webhandlers for unconfigured state
// start an wifi accesspoint
wifiAp();
// use webhandler for unconfigured state
WebHandler_unconfigured();
}
@ -627,10 +647,20 @@ void loop() {
}
void WebHandler_unconfigured() {
webserver.on("/", HTTP_GET, WEBrootAP);
/*
* 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);
}
@ -640,6 +670,37 @@ void WebHandler_configured() {
*
*/
void WEB404() {
String body = FPSTR(HTMLheader);
body += "404 - not found";
body += FPSTR(HTMLfooter);
}
void WEBrootUnconfigured() {
byte ssidsAvail = WiFi.scanNetworks();
String body = FPSTR(HTMLheader);
body += "<h1>unconfigured!</h1>";
body += "<h1>WiFi config</h1>";
body += "<form method='post' action='/wifiConfig/save'>";
body += "SSID: <input type='text' name='WIFIssid'><br>";
body += "Password: <input type='password' name='WIFIpassword'><br>";
body += "IP: <input type='text' name='WIFIip'><br>";
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>";
body += "gateway: <input type='text' name='WIFIgateway'><br>";
body += "dns: <input type='text' name='WIFIdns'><br>";
body += "<input type='submit' value='Save'>";
body += "</form>";
body += "<p>Available WiFi <pre>";
for(int i = 0 ; i < ssidsAvail; i++) {
body += WiFi.SSID(ssidsAvail);
body += "\n";
}
body += "</pre></p>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
void WEBroot() {
String body = FPSTR(HTMLheader);
@ -649,30 +710,6 @@ void WEBroot() {
webserver.send(200, "text/html", body);
}
void WEBrootAP() {
String body = FPSTR(HTMLheader);
body += "<h1>unconfigured!</h1>";
body += "<h1>WiFi config</h1>";
body += "<form method='post' action='/wifiConfig/save'>";
body += "SSID: <input type='text' name='WIFIssid'><br>";
body += "Password: <input type='password' name='WIFIpassword'><br>";
body += "IP: <input type='text' name='WIFIip'><br>";
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>";
body += "gateway: <input type='text' name='WIFIgateway'><br>";
body += "<input type='submit' value='Save'>";
body += "</form>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
void WEB404() {
String body = FPSTR(HTMLheader);
body += "404 - not found";
body += FPSTR(HTMLfooter);
}
/*
*
@ -686,6 +723,7 @@ void POSTwifiConfig() {
String WIFIip_new = webserver.arg("WIFIip");
String WIFInetmask_new = webserver.arg("WIFInetmask");
String WIFIgateway_new = webserver.arg("WIFIgateway");
String WIFIdns_new = webserver.arg("WIFIdns");
WIFIssid_new.toCharArray(WIFIssid, 32);
WIFIpassword_new.toCharArray(WIFIpassword, 64);
@ -693,6 +731,7 @@ void POSTwifiConfig() {
WIFIip.fromString(WIFIip_new);
WIFInetmask.fromString(WIFInetmask_new);
WIFIgateway.fromString(WIFIgateway_new);
WIFIdns.fromString(WIFIdns_new);
configured = true;
@ -701,6 +740,7 @@ void POSTwifiConfig() {
EEPROM.put(96, WIFIip);
EEPROM.put(112, WIFInetmask);
EEPROM.put(128, WIFIgateway);
EEPROM.put(144, WIFIdns);
EEPROM.put(511, configured);
EEPROM.commit();
@ -719,6 +759,8 @@ void POSTwifiConfig() {
Serial.println(WIFInetmask_new);
Serial.print("WIFIgateway: ");
Serial.println(WIFIgateway_new);
Serial.print("WIFIdns: ");
Serial.println(WIFIdns_new);
Serial.print("configured: ");
Serial.println(configured);