logo elektroda
logo elektroda
X
logo elektroda

How to Store String Variables on ESP8266 With EEPROM or Other Non-Volatile Memory?

marcin1301993 3597 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 15999174
    marcin1301993
    Level 12  
    Hello, I need to store 4 variables of type String in a memory that will hold the data despite power off.
    These variables have a maximum of 16 characters.

    So far, I have used EEPROMAnything.h with which to store the entire structure. Unfortunately, everything worked until it was time for the power-off and power-on test.
    There was nothing in the EEPROM, i.e. after reading the structure I got an empty field.

    Therefore I have a question for you, how did you implement the storage of data ( variables in the form of String ) for the time of power off?
  • ADVERTISEMENT
  • #2 15999251
    piotrva
    VIP Meritorious for electroda.pl
    Writing the structure using the default EEPROM driver for the ESP.
    Ot structure simply put in a union with an array of bytes and write/read these bytes from the EEPROM.
    It's worth storing 2 copies of the data (or more) along with a checksum.

    And it is always worth checking that it works before you believe ;)
  • #3 15999358
    marcin1301993
    Level 12  
    Thank you for your interest in the topic.
    I wrote and read the structure using EEPROMAnything from the same address and then sent via serial print individual Strings and they all contained the correct data until I turned ESP off and on again. After this I only obtained four blank fields.
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #5 15999423
    marcin1301993
    Level 12  
    Right, I forgot to post at least a case of code.
    So yes, saving is in the function which is called if any of the values change.
    It looks like this
    EEPROM.begin(512) I've also checked with the value 64 although I don't know what that means if it's the maximum amount of data I'll send or the maximum capacity of the eeprom in bytes, I don't know.
    EEPROMAnything_write(0, str_data)
    EEPROM.commit in some tutorial I found to modify anything.h and add EEPROM.commit there but it didn't do anything.

    This is what writing looks like to me, exactly like this, because I'm going to skip all the debug prints that inform me about each step of the program because I'm already running out of ideas where to look for errors....
    The read function does not precede the EEPROM. Begging, could this be it?

    i am posting the entire code, if anyone has the patience to take a look
    Code: C / C++
    Log in, to see the code
    [/code]

    On the other hand, here are the troublesome EEPROMs
    Code: C / C++
    Log in, to see the code


    And here are the first attempts to "save the EEPROM" and save only the values that have changed

    Code: C / C++
    Log in, to see the code


    Could you explain to me what this EEPROM.begin(value) is all about?
    is the value supposed to be equal to the sum of what is between EEPROM.begin and EEPROM.commit?
    this means that if I write a structure in which I have 4*16 bytes, should I enter 64 in EEPROM.begin?
    And if I write strings one by one, should I write 16 for each one?

    I'm unnecessarily complicating this topic all the time. I just read in the link you sent that before reading the value I should give EEPROM.begin... I will check immediately and let you know

    Unfortunately, adding EEPROM.begin(64) before readAnything didn't do anything. EEPROM.begin(512) didn't either.
    I guess I'll have to write a function to break the String into single bytes and write them myself, or I'll do a hardware power failure detection which will disconnect the CH_PD and support all the variables in RAM with a battery.
    Why is this not working for me? I can't find anywhere on the internet that anyone has had any problems with the EEPROM in ESP....
  • Helpful post
    #6 15999596
    piotrva
    VIP Meritorious for electroda.pl
    1. Try to get rid of the byte-pool - write a dozen or so bytes manually, then read them and, for example, increment them every time you start the program.
    2. If you have ever written something in a loop at high speed then you might have corrupted the memory.
    3. as size you specify the maximum size of the data you want to write, in your case it should be at least sizeof(str_data). Just a couple of comments here: I don't know how the string object will behave, ESP arranges fields in structures a bit strangely.

    At first I would try single byte writes - u not that works.
    Further write functions yourself to write what you need into memory.
    Further didn't use a string object when writing, but an array of characters.
  • #7 16013922
    marcin1301993
    Level 12  
    Hello, I've done some work on my programme and there is progress.

    As you wrote, I switched from String to character arrays and writes to EEPROM character by character (byte by byte).
    It worked, I mean the EEPROM still works because it writes and returns even after powering off which is really unusual :) , but a new problem has arisen and I don't know where the error is.

    I will describe first how the saving procedure looks like for me.

    I want to store 4 names each max of 32 characters.

    Everything starts with EEPROM.begin(512) still in setup() section
    Code: C / C++
    Log in, to see the code

    It then performs 32 array write operations character by character in a for loop, changing the address in the EEPROM and the sequence number of the array with each loop.
    The variable "a" is used when it wants to write another name with an offset of 32 bytes but for now it solves the problem with one name for simplicity.
    Everything is fine until I read the name.
    For example, when the name I entered is "mandarin" after rebooting the device and reading from the EEPROM on the Web page, I get
    "mandaryna������������������������������������������������"
    At first I thought it was the complement to these 32 characters that I "reserve" in the EEPROM, but no, there are more of these stamps. I don't know where they come from and how to get rid of them.
    Does anyone have any idea how to deal with this?

    I will add that the EEPROM reading looks like this:
    Code: C / C++
    Log in, to see the code
  • Helpful post
    #8 16013992
    Anonymous
    Anonymous  
  • ADVERTISEMENT
  • #9 16014092
    marcin1301993
    Level 12  
    This was exactly what was missing, end of character string detection. Thank you very much. Now everything works as it should. Admittedly, I don't know where the random characters at the end came from, but now it doesn't matter because I know when the array ends without knowing its length.
ADVERTISEMENT