0

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:

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)

EEPROM.put(4, item_inside.serial);

enter image description here

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

Lukas
  • 29
  • 8

2 Answers2

0

Juraj - you are right. i had some problems with addresses. I could not post the code properly in comment so il make a new answer. item_inside serial is initialized as following:

char item_inside.serial[10];
    int address=0;
    EEPROM.writeByte(address,item_inside.quantity);       
    address += sizeof(byte);
    
    EEPROM.writeString(address, item_inside.serial);
    address += 10;

    EEPROM.commit();

Now it properly writes to EEPROM

Lukas
  • 29
  • 8
0

Use a structure to hold the data record:

struct SERIAL_DATA
{
    uint_8 numBytes;
    char   data[10];
};

#define BEGIN_OF_PAD 0

Save to EEPROM:

int addr = BEGIN_OF_PAD;
DATA_RECORD serialData;                            // Instanciate a record

serialData.numBytes = item_inside.quantity;        // Add field to record

String tmpStr = String(item_inside.serial);        // Could use strcpy() too. 
                                                   // I prefer to use String in case I
                                                   // need to validate the data. 
tmpStr.toCharArray(serialData.data, tmpStr.length()); // Add field to the record 

EEPROM.put(addr, serialData);                      // Put record into EEPROM buffer
EEPROM.commit();                                   // Commit

addr += sizeof(serialData);                        // Advance address for next record

Retrieve from EEPROM:

#import <iostream>
using namespace std;

addr = BEGIN_OF_PAD;
DATA_RECORD retrievedData;                         // Instanciate a record

EEPROM.get(addr, retrievedData);                   // Retrieve a record from the EEPROM

cout<<"Data size: "<<retrievedData.numBytes<<endl; // Process the record as needed
cout<<"Data: "     <<retrievedData.data    <<endl;

addr += sizeof(serialData);                        // Advance address for next record

:::::::::::::::
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
TonyB
  • 1
  • 1