How Do I Read EEPROM into Variables?

Hello,

I have a list of global variables for my sketch that I’d like to store in EEPROM and read from EEPROM.

unsigned int udpLocalPort = 5040;
char timeServer[] = "time.nist.gov";
int timeZone = -5;
IPAddress moduleIp(172,20,85,10);
IPAddress gateway(172,20,85,1);
IPAddress subMask(255,255,255,0);
IPAddress domainNameServer(172,20,85,1);
boolean daylightSavings = 1;
boolean enableDHCP = 1;
boolean twelveOrTwentyFour;
boolean enableAmPm;

I think I can put the variables into EEPROM using EEPROM.put(), but I am unclear how to read them back into the variable declarations above when the microcontroller is turned on.

If I use EEPROM.read(address), on an IPAddress, for example, Ethernet.h describes and IPAddress as a comma-delimited list. Is that a 4 byte array or 7 bytes or 15 bytes? Are they stored in contiguous adresses?

Should I use a struct for the booleans?

Do I set aside 64 adresses for the timeserver How do I know what the length actually is? Do I need to reference the length before putting it in the EEPROM and store the length in EEPROM in another address?

For the timeZone and udpLocalPort, are they in contiguous addresses? Do I need to index through 2 sequential addresses, then a |= along with << statement to shift the second byte into the variable?

Do I need to typecast? I always mess things up going from bytes (unsigned 8-bit values) to signed ints etc.

I think I can put the variables into EEPROM using EEPROM.put(), but I am unclear how to read them back into the variable declarations above when the microcontroller is turned on.

Hint: What's the opposite of put?

PaulS:
Hint: What's the opposite of put?

I would thing "get" is the opposite of "put"? but there isn't a library construct that is the opposite as I can see. "Read" appears to be the opposite of "Write" and "Put" uses "Update"? I tried to read the EEPROM.h, along with avr/eeprom.h which it's based on, and still don't understand how "Put" works, but I also don't see an equal opposite construct.

Perehama:
I would thing “get” is the opposite of “put”?

10 points!

Perehama:
but there isn’t a library construct that is the opposite as I can see.

Really?

-20 points :stuck_out_tongue:

:blush: /me shakes my head.

I swear I thought I looked everywhere for that, and it's been there the whole time...

Sometimes, I can't see the forest for the trees....

:smiley:

Perehama:
:blush: /me shakes my head.

I swear I thought I looked everywhere for that, and it’s been there the whole time…

Sometimes, I can’t see the forest for the trees…

To make life a heck of a lot easier, put all of your EEPROM preferences into a single struct:

#include <EEPROM.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>

struct ConfigurationSettings{
  unsigned int udpLocalPort;
  char timeServer[32];
  int timeZone;
  IPAddress moduleIp;
  IPAddress gateway;
  IPAddress subMask;
  IPAddress domainNameServer;
  boolean daylightSavings;
  boolean enableDHCP;
  boolean twelveOrTwentyFour;
  boolean enableAmPm;
};

ConfigurationSettings settings = {
  5040,
  "time.nist.gov",
  -5,
  {172,20,85,10},
  {172,20,85,1},
  {255,255,255,0},
  {172,20,85,1},
  1,
  1,
  false,
  false
};

void setup(void) 
{
  //EEPROM.put(0,settings);  uncomment this the first time to initialize the EEPROM to "settings", then comment it out
  EEPROM.get(0, settings);
}

void loop(void) 
{
//  if(somethingInEprommChanged)
//  {
//    EEPROM.put(0, settings);
//  }
}

and then use the dot operator in your code to access the user settings…

Thank you for the Struct,

That was my eventual plan, but I hadn't considered it for the entire list.

That does take care of keeping the variables from writing over each other...

I have one variable that is a char array of indefinite length.

I can limit the max length to 255 or whatever.

Do I need to fix the length, or does Put and Get take care of that for me?

Perehama:
I have one variable that is a char array of indefinite length.

That is impossible with C++... Array sizes are defined on compile time.

Perehama:
Thank you for the Struct,

That was my eventual plan, but I hadn't considered it for the entire list.

That does take care of keeping the variables from writing over each other...

I have one variable that is a char array of indefinite length.

I can limit the max length to 255 or whatever.

Do I need to fix the length, or does Put and Get take care of that for me?

the library docs (it happens to be very well documented and supported).

Of course you must define the size of the char array (you will get a compile error if you don't) but using the max size is a good starting point, but not more... this will still be in RAM.

Like I said, using the struct object is WAY easier than placing this data individually, keeping track of the addresses and all that nonsense.

BulldogLowell:
To make life a heck of a lot easier, put all of your EEPROM preferences into a single struct:

#include <EEPROM.h>

#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>

struct ConfigurationSettings{
 unsigned int udpLocalPort;
 char timeServer[32];
 int timeZone;
 IPAddress moduleIp;
 IPAddress gateway;
 IPAddress subMask;
 IPAddress domainNameServer;
 boolean daylightSavings;
 boolean enableDHCP;
 boolean twelveOrTwentyFour;
 boolean enableAmPm;
};

ConfigurationSettings settings = {
 5040,
 “time.nist.gov”,
 -5,
 {172,20,85,10},
 {172,20,85,1},
 {255,255,255,0},
 {172,20,85,1},
 1,
 1,
 false,
 false
};

void setup(void)
{
 //EEPROM.put(0,settings);  uncomment this the first time to initialize the EEPROM to “settings”, then comment it out
 EEPROM.get(0, settings);
}

void loop(void)
{
//  if(somethingInEprommChanged)
//  {
//    EEPROM.put(0, settings);
//  }
}




and then use the dot operator in your code to access the user settings...

I am noticing you first declare the struct, then declare a variable of that struct and give values. Then in the void setup(), you reassign the values with the get statement.
Is there are reason for giving the value when settings is declared?
Can you write those lines as:

ConfigurationSettings settings;

void setup() 
{
  EEPROM.get(0, settings);
}

Perehama:
Is there are reason for giving the value when settings is declared?

yes, initially, your EEPROM will not contain what you wish it to contain, so as I pointed out:

  //EEPROM.put(0,settings);  uncomment this the first time to initialize the EEPROM to "settings", then comment it out

you only need to do that one time, or if you add (at the end of your struct please!) new variables to be retained in EEPROM.