[SOLVED] EEPROM troubles on char array

Hi,
I have an issue with the EEPROM writing and reading, I need to save an id inside the EEPROM, but when I read the stored id, the first char disappear and my char array length change. Here are my reading and writing code :

char * WiFiManager::readCharArrayInMemory(unsigned int addr, int taille)
{
 char retour[19];
 EEPROM.begin(4096);
 Serial.println("----- Récupération-----");
    Serial.println(strlen(retour));
 for (int i = 0; i < taille; i++) {
      retour[i] = EEPROM.read(addr++);
      Serial.print(retour[i]);
 }
 Serial.println();
    Serial.println(strlen(retour));
    Serial.println("\n----- Fin Récupération-----");
    
 return retour;
}

void WiFiManager::writeInMemory(unsigned int addr, char * value)
{
 EEPROM.begin(4096);
 int size = strlen(value);
 Serial.println("----- Ecriture-----");
 for (int i = 0; i < size; i++) {
      char c = value[i];
      EEPROM.write(addr++, c);
      Serial.print(c);
 }
     Serial.println("\n----- Fin Ecriture-----");
}

and here is how I call the write :

        byte sizeOfID = strlen(_product_api_id);
   Serial.println(sizeOfID);
   writeInMemory(0, sizeOfID);
   writeInMemory(sizeof(byte), _product_api_id);
   Serial.println("API sauvegardée.");

And here for the read :

        byte sizeOfID = readByteInMemory(0);
   char * api_id = readCharArrayInMemory(sizeof(byte)-1, sizeOfID);
   Serial.printf("api_id : %s\n", api_id);

The strange thing is that the write seems to work properly, but not the read, I print the size of my "retour" variable, at the end its length is of 24, I don't understand why...

Here is my variable to store : 180606174825_LQBU_5

And I will put the result attach to this message.

Thanks a lot for your time.

Capture d’écran de 2018-06-11 14-45-59.png

        byte sizeOfID = strlen(_product_api_id);
  	Serial.println(sizeOfID);
  	writeInMemory(0, sizeOfID);

sizeOfID is NOT a pointer to char, so you are writing crap to the 1st position in the EEPROM.

The rest of your code depends on having written good data, which you didn't.

PaulS:

        byte sizeOfID = strlen(_product_api_id);

Serial.println(sizeOfID);
  writeInMemory(0, sizeOfID);



sizeOfID is NOT a pointer to char, so you are writing crap to the 1st position in the EEPROM.

The rest of your code depends on having written good data, which you didn't.

I'm sorry but I think I don't understand what you mean, because my sizeOfID is the ID size, the size can change because it's a dynamic allocation from the server, so I need to know the ID length to get it back properly. I don't want it to be a char pointer. Am I missing something ?

Delta_G:
Your function expects a char pointer. When you give it the size it interprets that as a char pointer and finds whatever is in memory at that address and dutifully puts it in eeprom.

You need to either make a string out of that number or overload your write function so it has a version that takes a number instead of a pointer to char.

Oh yeah sorry ! I understand now what is the problem for you, I totally forgot to put on the post this overload :

void WiFiManager::writeInMemory(unsigned int addr, byte value)
{
	EEPROM.begin(4096);
	if(EEPROM.read(addr) != value) {
		EEPROM.put(addr, value);
		EEPROM.commit();
	}
}

That's why I didn't understand, sorry again, but this doesn't work even if I have this function.

 int size = strlen(value);

But, you stored the size as a byte...

 return retour;

retour is a local variable that goes out of scope when the function ends. Returning a pointer to it is NOT going to work.

You COULD make retour static, so that it IS safe to return a pointer to it.

Ok thanks a lots, I fixed almost everything, but I'm facing a second issue... it's impossible for me to write and read a value at the addresse 1... When I put my value inside the EEPROM, I can't read it, the read is faulty... here are my changes :

void WiFiManager::writeInMemory(unsigned int addr, string value)
{
 EEPROM.begin(4096);
 byte size = value.length();
 Serial.println("----- WRITING-----");
 for (int i = 0; i < size; i++) {
      char c = value[i];
      EEPROM.write(addr, c);
      Serial.printf("writing at addr : %d char: %c\n", addr, value[i]);
      addr++;
 }
     Serial.println("\n----- END WRITING-----");
}

string WiFiManager::readStringInMemory(unsigned int addr, int taille)
{
 string retour = "";
 EEPROM.begin(4096);
 Serial.println("----- Reading-----");
 addr--;
 for (int i = 0; i < taille; i++) {
      retour += EEPROM.read(addr);
      addr++;
      Serial.printf("reading addr : %d char: %c\n", addr, retour[i]);
 }
    Serial.println("\n----- End Reading-----");
    return retour;
}

here my main code:

byte sizeOfID = _product_api_id.length();
   writeInMemory(0, sizeOfID);
   writeInMemory(sizeof(byte), _product_api_id);
byte sizeOfID = readByteInMemory(0);
   Serial.printf("size : %d\n", sizeOfID);
   string api_id = readStringInMemory(sizeof(byte), sizeOfID);

and here the results :

PS : When I send the same code inside of my second arduino it makes strange things idk why... I can see that the value put are the correct ones, but the red ones are just not at all the right ones..

Capture d’écran de 2018-06-11 16-50-36.png

Capture d’écran de 2018-06-11 16-50-57.png

Capture d’écran de 2018-06-11 16-51-11.png

string WiFiManager::readStringInMemory(unsigned int addr, int taille)
{
	string retour = "";
	EEPROM.begin(4096);
	Serial.println("----- Reading-----");
	addr--;

Why are you decrementing addr? Pass the correct value to the method.

      retour += EEPROM.read(addr);
      addr++;
      Serial.printf("reading addr : %d char: %c\n", addr, retour[i]);

The address that you claim, in the printf() call, is NOT the address you read from.

Ok I find my mistake... I forgot to put EEPROM.commit() so the value doesn't change... thanks a lot for your help and you was right I did strange things with the decrementing and incrementing, I changed it, thanks again !