CanGrow Firmware WIP

This commit is contained in:
Marcus 2024-04-13 03:24:47 +02:00
parent 0d97eacd76
commit 9577788dfa

View file

@ -38,13 +38,17 @@
*
*/
// When D4 is HIGH at start, WIPE is true and EEPROM get cleared
// When D4 is LOW at start, WIPE is true and EEPROM get cleared
// D4 hast to be shorted to GND withing the 2 sec delay in setup();
// DO NOT PULL D4 DOWN AT START/POWER ON !!! BOOT WILL FAIL
bool WIPE;
/*
* EEPROM variables
*/
// Size of EEPROM
const uint8_t EEPROMsize = 512;
bool configured;
char WIFIssid[32];
char WIFIpassword[64];
@ -64,9 +68,9 @@ char WIFIpassword[64];
const char *APssid = "CanGrow-unconfigured";
const char *APpass = "CanGrow";
IPAddress ip(192,168,4,20);
IPAddress netmask(255,255,255,0);
IPAddress gateway(192,168,4,254);
IPAddress WIFIip(192,168,4,20);
IPAddress WIFInetmask(255,255,255,0);
IPAddress WIFIgateway(192,168,4,254);
@ -335,6 +339,15 @@ int getLightchirp() {
return lightchirp;
}
void wipeEEPROM() {
// write a 0 to all 512 bytes of the EEPROM
Serial.print("wiping EEPROM... ");
for (int i = 0; i < EEPROMsize; i++) { EEPROM.write(i, 0); }
Serial.println("DONE");
pinMode(PIN_WIPE, OUTPUT);
digitalWrite(PIN_WIPE, LOW);
delay(5000);
}
bool loadEEPROM() {
// read var configured from Byte 0
@ -344,11 +357,11 @@ bool loadEEPROM() {
// read var WIFIpassword, 64 byte long
EEPROM.get(33, WIFIpassword);
// read var ip, 16 byte long
EEPROM.get(113, ip);
EEPROM.get(113, WIFIip);
// read var netmask, 16 byte long
EEPROM.get(129, netmask);
EEPROM.get(129, WIFInetmask);
// read var gateway, 16 byte long
EEPROM.get(145, gateway);
EEPROM.get(145, WIFIgateway);
Serial.print("EEPROM loaded, CanGrow configured is: ");
Serial.println(configured);
@ -378,14 +391,11 @@ void setup() {
digitalWrite(PINled, LOW);
digitalWrite(PINpump, LOW);
// read status from PIN_WIPE to WIPE
WIPE = digitalRead(PIN_WIPE);
// Start Serial
Serial.begin(115200);
// Start EEPROM
EEPROM.begin(512);
EEPROM.begin(EEPROMsize);
// initialise Wire for I2C
Wire.begin();
@ -409,23 +419,52 @@ void setup() {
// initialise DHT11
dht.begin(); //TODO: Do only, when configured
WiFi.softAPConfig(ip, gateway, netmask);
WiFi.softAPConfig(WIFIip, WIFIgateway, WIFInetmask);
WiFi.softAP(APssid);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
// load stored values from EEPROM
loadEEPROM();
// wait a few seconds to let the user pull D4 down to wipe EEPROM
// and we can enjoy the boot screen meanwhile :p
delay(2000);
// read status from PIN_WIPE to WIPE
WIPE = digitalRead(PIN_WIPE);
Serial.print("WIPE is ");
Serial.println(WIPE);
if( WIPE == 0 ) {
wipeEEPROM();
}
//
// Webserver handlers
//
webserver.on("/", HTTP_GET, webRoot);
// server.onNotFound(handleNotFound);
// webserver.on("/save", HTTP_POST, handleSave);
// webserver.on("/control", HTTP_GET, handleControl);
// when not configured, webroot is WEBrootAP
// nothing else configured
if(configured == true) {
webserver.on("/", HTTP_GET, WEBroot);
} else {
webserver.on("/", HTTP_GET, WEBrootAP);
}
// generic handler
webserver.on("/wifiConfig/save", HTTP_POST, POSTwifiConfig);
// 404 handling
//webserver.onNotFound(handleNotFound);
/*
webserver.on("/", HTTP_GET, WEBroot);
webserver.on("/control", HTTP_GET, handleControl);
*/
webserver.begin();
loadEEPROM();
Serial.print("WIPE is ");
Serial.println(WIPE);
delay(2000); //TODO: replace with millis()
}
@ -447,7 +486,7 @@ void loop() {
*
*/
void webRoot() {
void WEBroot() {
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
@ -456,19 +495,48 @@ void webRoot() {
webserver.send(200, "text/html", body);
}
void webRootAP() {
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);
}
/*
*
* POSTs
*
*/
void POSTwifiConfig() {
String WIFIssid_new = webserver.arg("WIFIssid");
String WIFIpassword_new = webserver.arg("WIFIpassword");
String WIFIip_new = webserver.arg("WIFIip");
String WIFInetmask_new = webserver.arg("WIFInetmask");
String WIFIgateway_new = webserver.arg("WIFIgateway");
WIFIssid_new.toCharArray(WIFIssid, 32);
WIFIpassword_new.toCharArray(WIFIpassword, 64);
WIFIip.fromString(WIFIip_new);
WIFInetmask.fromString(WIFInetmask_new);
WIFIgateway.fromString(WIFIgateway_new);
EEPROM.put(0, 1);
EEPROM.commit();
webserver.send(200, "text/html", "wifiConfig saved, please restart");
}