Firmware WIP

This commit is contained in:
Marcus 2024-04-13 15:46:40 +02:00
parent 56d988153d
commit b7eb2a4398

View file

@ -48,8 +48,6 @@ bool WIPE;
* EEPROM variables
*/
// Size of EEPROM
//const uint8_t EEPROMsize = 512;
bool configured;
char WIFIssid[32];
char WIFIpassword[64];
@ -345,48 +343,76 @@ void wipeEEPROM() {
// write a 0 to all 512 bytes of the EEPROM
Serial.print("wiping EEPROM... ");
for (int i = 0; i < 512; i++) { EEPROM.write(i, 0); }
EEPROM.commit();
// commit everything to EEPROM and end here
EEPROM.end();
Serial.println("DONE");
// set D4 PIN_WIPE internal LED to Output to give feedback WIPE
// was done
pinMode(PIN_WIPE, OUTPUT);
digitalWrite(PIN_WIPE, LOW);
Serial.println("Please restart the device.");
delay(5000);
Serial.println("!! Device will restart in 5 seconds !!");
byte i = 0;
while( i <= 10 ) {
if( i % 2 ) {
digitalWrite(PIN_WIPE, LOW);
} else {
digitalWrite(PIN_WIPE, HIGH);
}
delay(500);
i++;
}
ESP.restart();
}
bool loadEEPROM() {
// read var configured from Byte 0
//EEPROM.get(0, configured);
// read var WIFIssid, 32 byte long
EEPROM.get(0, WIFIssid);
// read var WIFIpassword, 64 byte long
EEPROM.get(32, WIFIpassword);
/*
// read var ip, 16 byte long
EEPROM.get(112, WIFIip);
// read var netmask, 16 byte long
EEPROM.get(128, WIFInetmask);
// read var gateway, 16 byte long
EEPROM.get(144, WIFIgateway);
* WIFI data
*/
// 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);
Serial.print("configured: ");
Serial.println(configured);
*/
// read var WIFIip from address 96, 16 byte long
EEPROM.get(96, WIFIip);
// read var WIFInetmask from address 112, 16 byte long
EEPROM.get(112, WIFInetmask);
// read var WIFIgateway from address 128, 16 byte long
EEPROM.get(128, WIFIgateway);
/*
* Grow data
*/
//TBD
/*
* 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
*/
EEPROM.get(511, configured);
// print values to Serial output
Serial.println(":: EEPROM loaded ::");
Serial.print("WIFIssid: ");
Serial.println(WIFIssid);
Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword);
/*
Serial.print("WIFIip: ");
Serial.println(WIFIip);
Serial.print("WIFInetmask: ");
Serial.println(WIFInetmask);
Serial.print("WIFIgateway: ");
Serial.println(WIFIgateway);
*/
Serial.print("configured: ");
Serial.println(configured);
return(configured);
}
@ -418,14 +444,12 @@ void setup() {
// Start Serial
Serial.begin(115200);
// Write an empty line, because before there is some garbage in serial
// output
Serial.println("");
Serial.println(".:: CanGrow Start ::.");
// load stored values from EEPROM
loadEEPROM();
// initialise Wire for I2C
Wire.begin();
@ -459,21 +483,27 @@ void setup() {
// 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);
// when PIN_WIPE is set to LOW, wipe EEPROM
if( WIPE == 0 ) {
Serial.print("PIN_WIPE (D4) is set to LOW, wiping EEPROM now..");
// wipe EEPROM
wipeEEPROM();
} else {
// load stored values from EEPROM
loadEEPROM();
}
//
// Webserver handlers
//
/*
* Webserver handlers
*/
// when not configured, webroot is WEBrootAP
// nothing else configured
@ -565,18 +595,19 @@ void POSTwifiConfig() {
WIFIip.fromString(WIFIip_new);
WIFInetmask.fromString(WIFInetmask_new);
WIFIgateway.fromString(WIFIgateway_new);
//configured = 1;
configured = true;
EEPROM.put(0, WIFIssid);
EEPROM.put(32, WIFIpassword);
EEPROM.put(96, WIFIip);
EEPROM.put(112, WIFInetmask);
EEPROM.put(128, WIFIgateway);
EEPROM.put(511, configured);
EEPROM.commit();
Serial.println(":: POSTwifiConfig ::");
//Serial.print("configured: ");
//Serial.println(configured);
Serial.print("WIFIssid: ");
Serial.println(WIFIssid_new);
@ -584,25 +615,16 @@ void POSTwifiConfig() {
Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword_new);
Serial.println(WIFIpassword);
/*
Serial.print("WIFIip: ");
Serial.println(WIFIip_new);
Serial.print("WIFInetmask: ");
Serial.println(WIFInetmask_new);
Serial.print("WIFIgateway: ");
Serial.println(WIFIgateway_new);
*/
char rofl[32];
EEPROM.get(0, rofl);
Serial.println("READ EEPROM WIFIssid");
Serial.println(rofl);
Serial.print("configured: ");
Serial.println(configured);
Serial.println("READ EEPROM WIFIpassword");
char lol[64];
EEPROM.get(32, lol);
Serial.println(lol);
webserver.send(200, "text/html", "wifiConfig saved, please restart");
}