Retained Variables vs EEPROM

I’m trying to understand the difference between using Retained Variables and EEPROM.

I’ve looked thru the docs and see the descriptions, but I’m still a bit confused.

Does the EEPROM survive a battery disconnect? It seems that the Retained Variables does not.

I’m looking to write a half dozen variables just once that will then be used in communications and calculations unique to the location of an electron that is part of a product. My plan is to send these variables to a new installation by a publish/subscribe process, and only to update them if the unit is relocated.

The retained variables seem much simpler to use, but I’m thinking that the EEPROM will be more robust?

Yes, in the sense that the simulated EEPROM keeps its values even with no power to the device. With retained variables, you would need a small battery (coin cell) attached to VBAT to hold the values. EEPROM isn't that complicated to use, especially if you gather all your variables into a struct, and then you can write and read them with a singe put() and get() respectively.

1 Like

OK, so working from the example in Docs, and my previous code, I have come up with the following.
but I get this error,

eepromstore.ino:53:99: conversion from 'int*' to non-scalar type 'storeInEEPROM()::MyObject' requested

I'm not quite sure if I need to use placeholders for myObj... or if I have the wrong variable type...
int addr;

char myID[32];
char deviceName[25];
int myChannelNumber;
char myWriteAPIKey[32];
int sensorDistanceMM;
int TankDepth;

void storeInEEPROM() {
    addr = 20;
    
    struct MyObject {
        char myID;
        char deviceName;
        int myChannelNumber;
        char myWriteAPIKey;
        int TankDepth; 
    };
    
    MyObject myObj = (deviceName, myID, &myChannelNumber, myWriteAPIKey, &sensorDistanceMM, &TankDepth);
    EEPROM.put(addr, myObj);
}

Your strings need to be char[] not char nor char*.
Also initialisation of a struct is done with curly braces not parentheses.
Your integers want to be initialised without the address-of operator (&) .
Finally you try to initialise five variables but provide six “values”.

BTW, why don’t you just declare your struct globally?
If you do that, you’d just EEPROM.get() your data into the struct and from then on work with exactly these fields and not independent variables.

2 Likes

OK, I made changes but still get this same error on the MyObject MyObj = line

eepromstore.ino:53:99: conversion from ‘int*’ to non-scalar type ‘storeInEEPROM()::MyObject’ requested

Your strings need to be char not char nor char*.

I think I did that, but not sure it's done right.

Also initialisation of a struct is done with curly braces not parentheses.

Done

your integers want to be initialised without the address-of operator (&) .

Done

Finally you try to initialise five variables but provide six “values”.

Done

Feels like I'm getting close, but I'm still not grasping the how of some of this. I'm sure there is an easier way to get the myObj populated, but I'll work on that later.

Code:

char myID[32];
char deviceName[25];
int myChannelNumber;
char myWriteAPIKey[32];   
int sensorDistanceMM;
int TankDepth;

struct MyObject {
    char myID[];
    char deviceName[];
    int myChannelNumber;
    char myWriteAPIKey[];
    int sensorDistanceMM;
    int TankDepth;
    };

void storeInEEPROM() {
addr = 20;
MyObject myObj = (deviceName, myID, myChannelNumber, myWriteAPIKey, sensorDistanceMM, TankDepth);
EEPROM.put(addr, myObj);
}

Thx J

That's not really any different to char *deviceName - the empty square brackets still don't reserve any space for the variable to be stored to.

And did you miss the comment about not to use parentheses but curly braces { ... } for initialising?

You wrote Done but then you have

where it should be

MyObject myObj = { deviceName, myID, myChannelNumber, myWriteAPIKey, sensorDistanceMM, TankDepth };

But since character arrays aren't copied like that, this won't actually do what you want either.
You don't want the pointer copied but the actual string content which is done via strcpy() or similar.

Thanks, @ScruffR, I thought the curly braces was referring to the struct… Got it now.

I think that part of what was giving me trouble was that I’m trying to build this struct array, but was trying to use the same variables in the array that I was already getting via a string via a webhook from another particle device. I think I’ll manage it once I use new variables in the array, and populate that array with the variables parsed out of the string.

Which Brings me to a new question: Is it possible to declare the struct array in a photon that I’m using as the server sending the variables, and to send the entire myObj thru a publish event? I’m not aware of such a thing, but it sure would be handy right about now…

J

No, that is not possible, the data for a publish needs to be a const char* or a String object.