ESP32 writing string data to EEPROM

Thread Starter

zazas321

Joined Nov 29, 2015
666
Hello. I need to save some data to EEPROM because I want to retain that value even when the device is switched OFF. When the device is powered again, I need to remember what was the last value.

I have no problems saving integer value to EEPROM as following:

Code:
void parse_item_inside(char* payload){

    cJSON* jsonObj = cJSON_Parse((char*)(payload));
  // Get pointer to specific field in JSON object
    cJSON* serial = cJSON_GetObjectItem(jsonObj, "Serial");
    cJSON* quantity = cJSON_GetObjectItem(jsonObj, "Quantity");
     //Get integer value - other fields allow access to strings, floats etc.

    strcpy(item_inside.serial,serial->valuestring);
    item_inside.quantity = quantity->valueint;

    EEPROM.put(2, item_inside.quantity);
    Serial.print("Checking serial before puting to eeprom=");
    Serial.println(item_inside.serial);
    EEPROM.put(4, item_inside.serial);
    //EEPROM.write(4,item_inside.serial);

    EEPROM.commit();

    Serial.print("ITEM_INSIDE_QUANTITY=");
    Serial.println(EEPROM.read(2));



    Serial.print("ITEM_INSIDE_SERIAL READ=");
    Serial.println(EEPROM.read(4));
    Serial.print("ITEM_INSIDE_SERIAL get=");
    Serial.println(EEPROM.get(4,local_serial));
    OLED_display(item_inside.quantity,number_to_pick);

// Delete JSON object (don't forget this)
  cJSON_Delete(jsonObj);

    }
However, when I try to put a string variable ( item_inside.serial)

Code:
EEPROM.put(4, item_inside.serial);
1597991059582.png



As you can see both functions, EEPROM.read and EEPROM.get did not work

Also, can someone help me understand when should I use EEPROM.write and EEPROM.put. Both of these seem to work fine for integer variable, However, EEPROM.write does not compile with string as it expects integer.

Also, is using EEPROM.read correct in this case , or should I use EEPROM.get function instead as I am not sure what are the differences
 

Attachments

Last edited:

Thread Starter

zazas321

Joined Nov 29, 2015
666
I have created a simple sketch to recreate the problem:

Code:
#include <EEPROM.h>

char test_string[10]="hello";
char read_eeprom[10];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  EEPROM.begin(10);


    EEPROM.put(4, test_string);
    EEPROM.commit();
    Serial.print("ITEM_INSIDE_SERIAL get=");
    EEPROM.get(4,read_eeprom);
    Serial.println(read_eeprom);
}

void loop() {
  // put your main code here, to run repeatedly:

}
1597993439716.png
 

trebla

Joined Jun 29, 2019
209
For my knowledge, EEPROM.write expects byte size (char) variable, not integer. If you want use this function to write a string you must create a loop which increases address value and pointer to string element in each pass. As ESP uses emulated EEPROM you must make EEPROM.commit() after this writing loop.
 

Thread Starter

zazas321

Joined Nov 29, 2015
666
I have found a nice solution:

on Arduino IDE example codes for ESP32 EEPROM, i have found these functions:
Code:
    EEPROM.writeUShort(address, item_inside.quantity);   
    address += sizeof(unsigned short);  
    EEPROM.writeString(address, item_inside.serial);
    address += 10;
    EEPROM.commit();
Now im able to write integer and a string without any problems
 
Top