I am trying to add eeprom to a sketch with an esp8226 board, but I am apparently not doing it right. I am having a function called for different things within the sketch
Ex. void Opened()
Within this function a motor is turned off once a limit switch is reached. The time is then recorded to a variable from a web server via:
hourOpened = (timeClient.getHours());
minuteOpened = (timeClient.getMinutes());
This lets me know when the door was opened. I am trying to save this time within eeprom so that if power is cut, I will still know what that time was when the board is powered back up. Here is the complete function:
void Opened()
{
digitalWrite(RelayDown, LOW);
digitalWrite(RelayUp, LOW);
doorMoving = false;
if (digitalRead(topSwitch) == LOW) {
topSwitchDetected = "yes";
bottomSwitchDetected = "no";
closed = false;
opened = true;
EEPROM.write(10, closed);
EEPROM.write(11, opened);
hourOpened = (timeClient.getHours());
minuteOpened = (timeClient.getMinutes());
EEPROM.write(16, minuteOpened);
if (hourOpened > 12) { // This changes from military time to regular
hourOpened = (hourOpened - 12);
pm = true;
}
else {
hourOpened = hourOpened;
pm = false;
}
EEPROM.write(15, hourOpened);
EEPROM.end();
}
}
I read this information with a checkDoorState() function within setup() as soon as the board is powered up:
The problem is when I disconnect and reconnect the board, I do get something from eeprom in the variables hourOpened and minuteOpened, but it is not accurate. It is usually an old time that was recorded prior to the last time the door opened.
I don't know if it has anything to do with it, but I don't understand how the address location works in eeprom.
If I assign address 1 to save data, is that single address location able to record a two digit number? Or would it need multiple address locations to record this two digit number?
If I use EEPROM.end doesn't this erase any past data that was stored in this location AND record the new data?
Edit: I can put entire code if necessary, but it is long and I figured nobody would want to look at the entire code.
An EEPROM address can only hold a single byte, the value of which can be between 0 and 255. So, if you have a 2 digit number such as 49 in a byte variable you can save it. If, however, the number is actually a string such as "49" then it will consist of 3 bytes and cannot be saved in a single EEPROM location. The EEPROM.put() function allows you to save multi byte data but it will save it to multiple EEPROM locations starting at the address you specify and it is up to you to ensure that multi byte data saved does not overlap other locations used
I have them saved as int..... I guess that is a problem huh? If I save them as byte this would be ok because they should never get larger than 60 right?
My favoured way to save a group of related data would be to put the data into a struct and put()/get() the struct with single commands, thus avoiding the need to know the address of the individual data items
I did try .commit() previously, but it did the same thing. I don’t understand the difference in it and .end().
I will try changing variable type.
Thanks!
EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.
You put() the struct to an address that you specify and the entire struct is saved to EEPROM in multiple bytes
When you want the data back you get() the struct from the previously used address then use the data in the struct rather than directly from EEPROM hence avoiding the need to know the address of the individual data items
Until the commit() happens the data is held in RAM. Once the commit() occurs that RAM copy is not needed so the memory that it occupies is released to be used for other purposes
Remember that each EEPROM address can only hold a single byte of data so there is no "other data" in that address
Because EEPROM has only a relatively small number of guaranteed write cycles per address before it may fail the EEPROM functions generally only write to EEPROM addresses where the data has changed. So, if you write() a byte to an address then write a second byte with the same value to the same address it will not actually be written as there is no need.