Help with EEPROM

I’m attempting to dive into saving some of my configuration variables into EEPROM a couple of short power outages made me realize I need to store some of this stuff so it reboots with the right settings. However, I cannot seem to get it to behave as expected.I’ll post the relevant sections of code. I’m trying to read the data in my setup function but I even made a particle function that should read the data on demand for troubleshooting purposes and I cant seem to work either. I would input my desired settings from the particle functions but on boot it would all be back to 0’s on daily run times and unknown for my chlormodetext and acidmodetext particle variables. Running my particle function to try and manually read the EEPROM doesn’t change he values either. What am I doing wrong?

// I dont think this is necessarily relevant but I put it here in case any of these settings affect eeprom
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // Use external antenna
SYSTEM_MODE(SEMI_AUTOMATIC);    // take control of the wifi behavior to avoid cloud bogging down local functions
SYSTEM_THREAD(ENABLED);    // enable so loop and setup run immediately as well as allows wait for to work properly for the timeout of wifi
// declared relevant variables
int Cdailyruntime = 0;
int Adailyruntime = 0;
int acidmode = 0; // variable to control acid pump mode off = 0 time = 1 or auto =2
String acidmodetext = "unknown"; //acid mode textual version
int chlormode = 0; // variable to control chlorine pump mode off = 0 time = 1 or auto =2
String chlormodetext = "unknown"; //chlorine mode textual version
// EEPROM address locations should be plenty far apart
int cdaddress = 0;
int adaddress = 10;
int amodeaddress = 20;
int cmodeaddress = 30;

void setup() {
    EEPROM.get(cmodeaddress, chlormode);
    if (chlormode == 0){
        chlormodetext == "off";
    }
    if (chlormode == 1){
        chlormodetext == "time";
     }
    if (chlormode == 2){
        chlormodetext == "auto";
    }
    EEPROM.get(amodeaddress, acidmode);
    if (acidmode == 0){
        acidmodetext == "off";
    }
    if (acidmode == 1){
        acidmodetext == "time";
    }
    if (acidmode == 2){
        acidmodetext == "auto";
    }
    EEPROM.get(adaddress, Adailyruntime);
    EEPROM.get(cdaddress, Cdailyruntime);
    //particle functions
    Particle.function("ChlorMode", chlormodefunc);
    Particle.function("AcidMode", acidmodefunc);
    Particle.function("AcidDaily", Adaily);
    Particle.function("ChlorDaily", Cdaily);
    Particle.function("eepromread", eeprom);
    // and particle variables
    Particle.variable("AcidMode", acidmodetext);
    Particle.variable("ChlorMode", chlormodetext);
    Particle.variable("ChlorDaily", Cdailyruntime);
    Particle.variable("AcidDaily", Adailyruntime);
}

void loop(){
}

// turn acid pump modes from the cloud
int acidmodefunc(String command){
    if (command == "0"){
      acidmode = 0;
      acidmodetext = "off";
      EEPROM.put(amodeaddress, acidmode);
      return 0;
    }
    else if (command == "1"){
      acidmode = 1;
      acidmodetext = "time";
      EEPROM.put(amodeaddress, acidmode);
      return 1;
    }
    else if (command == "2"){
      acidmode = 2;
      acidmodetext = "auto";
      EEPROM.put(amodeaddress, acidmode);
      return 2;
    }
    else {
      acidmode = 0;
      acidmodetext = "off";
      EEPROM.put(amodeaddress, acidmode);
      return 0;
    }

}

// turn chlorine pump modes from the cloud
int chlormodefunc(String command){
    if (command == "0"){
      chlormode = 0;
      chlormodetext = "off";
      EEPROM.put(cmodeaddress, chlormode);
      return 0;
    }
    else if (command == "1"){
      chlormode = 1;
      chlormodetext = "time";
      EEPROM.put(cmodeaddress, chlormode);
      return 1;
    }
     else if (command == "2"){
      acidmode = 2;
      chlormodetext = "auto";
      EEPROM.put(cmodeaddress, chlormode);
      return 2;
    }
    else {
      acidmode = 0;
      chlormodetext = "off";
      EEPROM.put(cmodeaddress, chlormode);
      return 0;
    }

}

// cloud trigger for chlorine daily change
int Cdaily(String command){
    Cdailyruntime = atoi (command);
    EEPROM.put(cdaddress, Cdailyruntime);
    return Cdailyruntime;
}

// cloud trigger for acid daily change
int Adaily(String command){
    Adailyruntime = atoi (command);
    EEPROM.put(adaddress, Adailyruntime);
    return Adailyruntime;
}

int eeprom(String command){
    if(command == 0){
        EEPROM.get(cmodeaddress, chlormode);
        if (chlormode == 0){
            chlormodetext == "off";
        }
        if (chlormode == 1){
            chlormodetext == "time";
        }
        if (chlormode == 2){
            chlormodetext == "auto";
        }
        EEPROM.get(amodeaddress, acidmode);
        if (acidmode == 0){
            acidmodetext == "off";
        }
        if (acidmode == 1){
            acidmodetext == "time";
        }
        if (acidmode == 2){
            acidmodetext == "auto";
        }
        EEPROM.get(adaddress, Adailyruntime);
        EEPROM.get(cdaddress, Cdailyruntime);
    }
}    

Just a observation this 0 should be in quotes

int eeprom(String command){
    if(command == 0){

Just some unrelated coding tips

could be shortened like this

const char txtModes[3][8] = { "off", "time", "auto" }
void setup() {
    EEPROM.get(cmodeaddress, chlormode);
    EEPROM.get(amodeaddress, acidmode);
    EEPROM.get(adaddress, Adailyruntime);
    EEPROM.get(cdaddress, Cdailyruntime);  
    chlormodetext = txtModes[chlormode];
    acidmodetext  = txtModes[acidmode];
...
}

I’d also not double that code block in void setup() and int eeprom() but have that only once in a function and use that very function in both instances.

Also instead of

rather

int chlormodefunc(String command){
    int cm = command.toInt();

    if (0 <= cm && cm <= 2)
      chlormode = cm;
    else
      chlormode = 0;

    chlormodetext = txtModes[chlormode];
    EEPROM.put(cmodeaddress, chlormode);

    return chlormode
}

But the main point is, that you frequently use == for assignments which won’t actually change the lvalue at all - you want to use a single = instead :wink:
You obviously also have some copy/paste errors where you didn’t adapt the code correctly (e.g. in chlormodefunc() you are setting acidmode instead of chlormode in the cases "2" and else).

1 Like

good catch I probably wasn’t even running the command like i thought I was. I put in some return values in there as well so I can tell for sure if it took. I threw that function together in a hurry just for troubleshooting when i noticed my block in setup wasn’t running.

But the main point is, that you frequently use == for assignments which won’t actually change the lvalue at all - you want to use a single = instead :wink:

I feel like an idiot I know better than that I must have been tired and when I scanned my code a bunch before giving up and posting here. For some reason it just didn’t stick out but camouflaged in. This was the main fix it all works now and retains the values after a boot.

You obviously also have some copy/paste errors where you didn’t adapt the code correctly (e.g. in chlormodefunc() you are setting acidmode instead of chlormode in the cases "2" and else ).

Thanks for catching that who knows how long I would have gone before noticing that one. hint probably a long time as this project has been running with that section of code minus the EEPROM bits for a year now haha also probably why when i tested putting in some values for eeprom i didnt get my expected result

Also as far as the shortening stuff man I’m a total newb and writing my code in a cleaner more elegant fashion is something i need to work on… Trust me I’ve come a long ways since my first attempt at teaching myself which was on a photon haha I’ll have a look through what you suggested to see if I can make sense of it and use it in the future as that does look nicer. I think the c strings and arrays and all these native c things just confuse me a bit but what seem like a powerful tool to understand. For whatever reason just simple if this then do this is simply all I’ve managed to wrap my noodle around so far.

1 Like