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; bool configured;
char WIFIssid[32]; char WIFIssid[32];
char WIFIpassword[64]; char WIFIpassword[64];
//int WIFIssidsAvail;
IPAddress WIFIip(192,168,4,20); IPAddress WIFIip(192,168,4,20);
IPAddress WIFInetmask(255,255,255,0); IPAddress WIFInetmask(255,255,255,0);
IPAddress WIFIgateway(192,168,4,254); IPAddress WIFIgateway(192,168,4,254);
IPAddress WIFIdns(0,0,0,0);
// 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];
@ -91,8 +93,14 @@ byte SoilmoistureLow;
* WiFi * WiFi
*/ */
const char *APssid = "CanGrow-unconfigured"; const char* APssid = "CanGrow-unconfigured";
const char *APpass = "CanGrow"; /*
* 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() { bool loadEEPROM() {
Serial.println(":: loading EEPROM ::");
/* /*
* configured * configured
* *
@ -433,6 +441,8 @@ bool loadEEPROM() {
* would be at the beginning - more a cosmetic thing * would be at the beginning - more a cosmetic thing
*/ */
EEPROM.get(511, configured); EEPROM.get(511, configured);
Serial.print("configured: ");
Serial.println(configured);
// when configured is > 1 (it should == 1) then read EEPROM furher data // when configured is > 1 (it should == 1) then read EEPROM furher data
if(configured > 0) { if(configured > 0) {
@ -452,6 +462,8 @@ bool loadEEPROM() {
EEPROM.get(112, WIFInetmask); EEPROM.get(112, WIFInetmask);
// read var WIFIgateway from address 128, 16 byte long // read var WIFIgateway from address 128, 16 byte long
EEPROM.get(128, WIFIgateway); EEPROM.get(128, WIFIgateway);
// read var WIFIgateway from address 128, 16 byte long
EEPROM.get(144, WIFIdns);
/* /*
* Grow data * Grow data
@ -466,16 +478,45 @@ bool loadEEPROM() {
Serial.print("WIFIssid: "); Serial.print("WIFIssid: ");
Serial.println(WIFIssid); Serial.println(WIFIssid);
Serial.print("WIFIpassword: "); Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword); Serial.println(WIFIpassword);
} }
Serial.println(":: EEPROM loaded ::");
Serial.print("configured: ");
Serial.println(configured);
return(configured); 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 * 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()) { if(loadEEPROM()) {
Serial.print("Configuration found in EEPROM, connect to Wifi ");
Serial.println(WIFIssid); // connect to wifi
wifiConnect();
// Start WiFi connection
WiFi.begin(WIFIssid, WIFIpassword);
// 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(); WebHandler_configured();
// no data found in EEPROM, setup Access Point // configured is 0, setup Access Point
} else { } else {
Serial.println("Creating Accesspoint");
Serial.print("SSID: "); // start an wifi accesspoint
Serial.println(APssid); wifiAp();
Serial.print("Password: "); // use webhandler for unconfigured state
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
WebHandler_unconfigured(); WebHandler_unconfigured();
} }
@ -627,10 +647,20 @@ void loop() {
} }
void WebHandler_unconfigured() { 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() { void WebHandler_configured() {
/*
* WebHandler_unconfigured()
*
* Here all URL routings are defined, in configured state
*/
webserver.on("/", HTTP_GET, WEBroot); 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() { void WEBroot() {
String body = FPSTR(HTMLheader); String body = FPSTR(HTMLheader);
@ -649,30 +710,6 @@ void WEBroot() {
webserver.send(200, "text/html", body); 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 WIFIip_new = webserver.arg("WIFIip");
String WIFInetmask_new = webserver.arg("WIFInetmask"); String WIFInetmask_new = webserver.arg("WIFInetmask");
String WIFIgateway_new = webserver.arg("WIFIgateway"); String WIFIgateway_new = webserver.arg("WIFIgateway");
String WIFIdns_new = webserver.arg("WIFIdns");
WIFIssid_new.toCharArray(WIFIssid, 32); WIFIssid_new.toCharArray(WIFIssid, 32);
WIFIpassword_new.toCharArray(WIFIpassword, 64); WIFIpassword_new.toCharArray(WIFIpassword, 64);
@ -693,6 +731,7 @@ void POSTwifiConfig() {
WIFIip.fromString(WIFIip_new); WIFIip.fromString(WIFIip_new);
WIFInetmask.fromString(WIFInetmask_new); WIFInetmask.fromString(WIFInetmask_new);
WIFIgateway.fromString(WIFIgateway_new); WIFIgateway.fromString(WIFIgateway_new);
WIFIdns.fromString(WIFIdns_new);
configured = true; configured = true;
@ -701,6 +740,7 @@ void POSTwifiConfig() {
EEPROM.put(96, WIFIip); EEPROM.put(96, WIFIip);
EEPROM.put(112, WIFInetmask); EEPROM.put(112, WIFInetmask);
EEPROM.put(128, WIFIgateway); EEPROM.put(128, WIFIgateway);
EEPROM.put(144, WIFIdns);
EEPROM.put(511, configured); EEPROM.put(511, configured);
EEPROM.commit(); EEPROM.commit();
@ -719,6 +759,8 @@ void POSTwifiConfig() {
Serial.println(WIFInetmask_new); Serial.println(WIFInetmask_new);
Serial.print("WIFIgateway: "); Serial.print("WIFIgateway: ");
Serial.println(WIFIgateway_new); Serial.println(WIFIgateway_new);
Serial.print("WIFIdns: ");
Serial.println(WIFIdns_new);
Serial.print("configured: "); Serial.print("configured: ");
Serial.println(configured); Serial.println(configured);