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-----");
}
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
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 :
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;
}
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..
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 !