Storing JSON Sting in Photon's Eprom

I want to store simple JSON string, {“name”: “particle”, “data”: “1,2,3,4”}. Please let me know what is the best way to go about without having to parse and do memory mapping.

Thanks.

creating a struct would be one approach:

struct MyDATA{
  char deviceID[25];
  byte data[4];
};

store:

MyDATA info = {"53ba42000b5135fe35321335", 1, 2, 3, 4};
EEPROM.put(0 * sizeof(MyDATA), info)

and get it:

MyDATA buffer = EEPROM.get(0 * sizeof(MyDATA));
char jsonText[128] = "";
snprintf(jsonText, sizeof(jsonText), "{\"name\":\"%s\",\"data\":\"%d,%d,%d,%d\"}", buffer.deviceID, buffer.data[0], buffer.data[1], buffer.data[2], buffer.data[3]);
Serial.println(jsonText);

something like that…

1 Like

thanks.

@BulldogLowell, I just tried your code. I am getting the following compile error on this line:

MyDATA buffer = EEPROM.get(0 * sizeof(MyDATA));

EEPROMClass::get(unsigned int)

@BulldogLowell, thanks. Never mind, I got the fixed.

1 Like

@BulldogLowell, I am getting invalid user-defined conversion from ‘String’ to ‘char’ [-fpermissive] when I do this Particle function call below:

int storeJSON(String args){

MyDATA info ={args}; //(error at this line)

EEPROM.put(0 * sizeof(MyDATA), info);

return 1;

}

I was using this code months back. Now, I cannot compile it. Please help.

Why do you have args wrapped in curly braces in your function?

This is C++, not JavaScript!
:slight_smile:

1 Like

@BulldogLowell; I figured it out. I had to do strcpy(xxx,"");. Thanks anyway.