EEPROM data corrupted after power reset

Hi,

I have been playing Arduino for couple of days as expected hitting some road blocks. My project needs to store auth details in order to authenticate. So I used EEPROM to store those value and It worked as expected but the problem arose after resetting power. The stored got corrupted after resetting the power. I have added the output of the serial monitor before and after resetting the power. All your help would be much appreciated. Thanks
Before power reset

09:15:25.209 -> 265653221541465
09:15:25.255 -> 0$Nzjvw2sxOOnXVnJCYvthgeojvC6vYwGxxXOyvuyJZiROnVUZGOUHyse

After power reset

09:13:49.227 -> ⸮l⸮⸮⸮e⸮Z{⸮$⸮⸮⸮
09:13:49.273 -> ⸮ ⸮nlf⸮⸮F⸮qsX⸮⸮E ⸮̋G⸮⸮\⸮⸮⸮⸮EmL⸮+⸮⸮_⸮z⸮ք ⸮E⸮n/

Welcome to the community.

So to help you we would need to know more about what you are doing.

I understand you are new but asking a question without information describe what you are doing (except a power reset) is kinda useless.

So can you:

  1. post your program using "code tags" i.e. </> from the above ribbon.

  2. Tell us what hardware you have. Arduino type, EEPROM type if external.

  3. What is your process of "resetting power". Is it while the program is running? Could you simply be removing power when writing to the EEPROM?

struct customObj {
  String vehicle;
  String password;
};
customObj customVarR;
void saveCred(String vehicle, String password) {
  customObj customVarW = {
    vehicle,
    password
  };
  EEPROM.put(0, customVarW);
}
void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);
  client.connect(server);
  char* sms = "26265653221541465:$2y$10$Nzjvw2sxOOnXVnJCYvthgeojvC6vYwGxxXOyvuyJZiROnVUZGOUHyse";
  parseSms(sms);
  EEPROM.get(0, customVarR);
  Serial.println(customVarR.vehicle);
  Serial.println(customVarR.password);
  client.setDataArrivedDelegate(dataArrived);
}
void parseSms(char* sms) {
  if (sms != "") {
    String smsString = String(sms);
    String vehicle = strtok(sms, "[::]");
    String password = smsString.substring(vehicle.length() + 4, smsString.length());
    saveCred(vehicle, password);
  }
}

I am using Uno R3 and internal EEPROM. After wrote to EEPROM I simply turn off and on the power and the stored value became corrupted like below

09:13:49.227 -> ⸮l⸮⸮⸮e⸮Z{⸮$⸮⸮⸮
09:13:49.273 -> ⸮⸮nlf⸮⸮F⸮qsX⸮⸮E⸮̋G⸮⸮\⸮⸮⸮⸮EmL⸮+⸮⸮_⸮z⸮ք ⸮E⸮n/

I can't be sure of this but I seem to recall that writing structures to a EEPROM is problematic.

You should try something simpler and see if it is any different.

You can not write String objects to the eeprom. They are pointers to another area of memory. You need to store the underlying character arrays.

1 Like

This. All you can really store into EEPROM are bytes; if you try to store a struct of Strings like you are doing, you are asking for trouble. Store your data into fixed-size c-strings. Since they are arrays of chars and a char is 8 bits long, it's easy to shuttle them in and out of EEPROM.

I hope you are being sensible with EEPROM usage and you are not writing to it more often than necessary.

I think it can be done with some overhead, but they have to be structures of fixed-size elements. Something like

struct FooBar
{
    char foo[20];
    char bar[10];
};

should work, because we know beforehand that it's always going to take up 30 bytes in EEPROM. Of course, as long as the size is fixed, you can always flatten the structure into single bytes, store these into EEPROM and then reconstruct the original structure from EEPROM readings. A String is problematic because we don't know how big it's going to end up being at compile time.

If I experienced this issue I would first test with the most basic (easy) data configuration. Then if that does not get corrupted on restart I would incrementally move towards the structure I wanted.
But that's just me.

That seems reasonable: I do follow a similar approach. Sometimes, I'd start with dummy, hard-coded data, just to test the functions downstream. And, generally (i.e. not only in cases like this one) I steer clear of String. It's just too messy.

Thanks for all your guidance. I have implemented my own method.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.