From 56d988153dd217098b0cedda865faa4ebe370393 Mon Sep 17 00:00:00 2001 From: Marcus Date: Sat, 13 Apr 2024 14:39:16 +0200 Subject: [PATCH] heavy WIP - EEPROM Stuff now working in principal --- Arduino/CanGrow/CanGrow.ino | 68 +++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 2e33cc7..355f763 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -29,8 +29,9 @@ #include "DHT.h" #include #include -#include #include +#include + /* * @@ -48,7 +49,7 @@ bool WIPE; */ // Size of EEPROM -const uint8_t EEPROMsize = 512; +//const uint8_t EEPROMsize = 512; bool configured; char WIFIssid[32]; char WIFIpassword[64]; @@ -341,13 +342,15 @@ int getLightchirp() { 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); + // 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(); + Serial.println("DONE"); + pinMode(PIN_WIPE, OUTPUT); + digitalWrite(PIN_WIPE, LOW); + Serial.println("Please restart the device."); + delay(5000); } @@ -366,11 +369,12 @@ bool loadEEPROM() { EEPROM.get(128, WIFInetmask); // read var gateway, 16 byte long EEPROM.get(144, WIFIgateway); - */ - Serial.println(":: EEPROM loaded ::"); + + Serial.print("configured: "); Serial.println(configured); - + */ + Serial.println(":: EEPROM loaded ::"); Serial.print("WIFIssid: "); Serial.println(WIFIssid); Serial.print("WIFIpassword: "); @@ -393,6 +397,9 @@ bool loadEEPROM() { * */ void setup() { + // Start EEPROM + EEPROM.begin(512); + // setup pins pinMode(PINfan, OUTPUT); pinMode(PINdht, INPUT); @@ -411,9 +418,13 @@ 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 ::."); - // Start EEPROM - EEPROM.begin(EEPROMsize); + // load stored values from EEPROM + loadEEPROM(); // initialise Wire for I2C Wire.begin(); @@ -442,8 +453,7 @@ void setup() { 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 @@ -555,35 +565,43 @@ void POSTwifiConfig() { WIFIip.fromString(WIFIip_new); WIFInetmask.fromString(WIFInetmask_new); WIFIgateway.fromString(WIFIgateway_new); - configured = 1; - EEPROM.put(0, configured); - EEPROM.put(1, WIFIssid_new); + //configured = 1; + + + + EEPROM.put(0, WIFIssid); + EEPROM.put(32, WIFIpassword); EEPROM.commit(); Serial.println(":: POSTwifiConfig ::"); - Serial.print("configured: "); - Serial.println(configured); + //Serial.print("configured: "); + //Serial.println(configured); Serial.print("WIFIssid: "); Serial.println(WIFIssid_new); + Serial.println(WIFIssid); 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(1, rofl); + EEPROM.get(0, rofl); Serial.println("READ EEPROM WIFIssid"); Serial.println(rofl); - Serial.println("READ EEPROM configured"); - bool lol; - EEPROM.get(0, lol); + Serial.println("READ EEPROM WIFIpassword"); + char lol[64]; + EEPROM.get(32, lol); Serial.println(lol); webserver.send(200, "text/html", "wifiConfig saved, please restart");