Problem Storing Strings on the EEPROM

I’ve been trying to store strings on the Photons EEPROM. I pass the String as an argument to a function which calculates the proper index and stores it at that address. The String comprises of data that is being updated from sensors. However, when several strings have been stored and i try to read back all the strings, they are all updated to the current string (one that has not been stored yet) and basically output the same data.

You can’t pass a String value to EEPROM.put() or get(). It won’t actually save the String, because the EEPROM is designed to work with fixed-length things like int, double and structures, not variable-length String objects.

You need to copy it into a char[] buffer first. I posted an example of it here:

2 Likes

Thanks @rickkas7
I actually tried that already but it still does the same thing that i’ve explained above. I’m putting this part aside as of now but once i get back to it and try it out again, ill will post my results here.

I think you misunderstood @rickkas7.
His point was “you cannot use String or char* variables but must use a char[] array” (casting win’t do either).
If you tried that, I’m sure you’d have gotten the correct behaviour.

1 Like

I see @ScruffR. I’ll try this out and see if it works.

The reason you shouldn’t store class objects or anything allocated is because all those pointers will likely be at a different memory address that hasn’t been properly allocated or initialized every time you restart, so they are pointing to random addresses, plus something like a String class has lots of function pointers and variables that waste the limited EEPROM space. It’s best to just create a struct that has all of your variables as only 8/16/32 bit values and arrays that you can rewrite any time something changes. There’s also a finite number of erases and writes that can be made to EEPROM, so try to keep it infrequent. It’s also easier to add up everything in the struct or print a sizeof() to see if it’s all going to fit.