ESP8266 EEPROM returning different value after reset

I am reading an LDR sensor and in my script have a set value to compare it to.
When ldr value is above this, IR lights are off. if it goes below for x amount of time,the IR leds turn on

I want to be able to change the value it is compared to (int ldrLow) through a serial connection.
It won't change often, just for tweaking when needed.

After some fiddling, i got the serial stuff going (mainly m having a bitch-in with data types)
I am trying to store the value in EEPROM to save it during reboots.

When i send the set command, it sets ldrLow. When i send the get command (made for debugging) it returns the correct value. When i reset the board, a whole different number is returned from memory. it's doing my head in, help!

the code:

#include <EEPROM.h>

int ldrLow = 400;
String msg;

void serialEvent() {
  Serial.println("serial things happened");
  runSerial();

}

void runSerial() {
  while (Serial.available()) {
    delay(10);
    if (Serial.available() > 0) {
      char c = Serial.read();
      if (c != '\n' && c != '\r') {
        msg += c;
      }
    }
  }
  if (msg != "") {
    if (msg.indexOf("setldr=") != -1) {
      msg.remove(0, 7);
      int lengte = msg.length();
      if (lengte > 4) {
        lengte = 4;
      }
      int what = msg.substring(0, lengte).toInt();
      if (what != 0 && what < 1025) {
        EEPROM.put(0, what);
        ldrLow = what;
      }
    }
    if (msg.indexOf("getldr") != -1) {
      int tmp = 1;
      EEPROM.get(0, tmp);
      Serial.println(tmp);
    }

    //Serial.println (msg);
  }
  Serial.flush();
  msg = "";
}

void setup() {
  Serial.begin(115200);
  EEPROM.begin(2);
  EEPROM.get(0, ldrLow);
}

void loop() {

}

And the serial output. first i type setldr=600 then getldr, then a reset and another getldr..

serial things happened
600
serial things happened
600
SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
serial things happened
1714512486

Always...right.
So after a few hours of frustration, right after posting this i bumped into the solution

The esp8266 stores EEPROM stuff in simulated EEPROM in RAM untill you use the undocumented function EEPROM.commit();

Adding that to the code and now it works as expected.

1 Like

Well done.

Reading your code, I spotted a potential error:

    if (msg.indexOf("setldr=") != -1) {
      msg.remove(0, 7);

What if "setldr" does not appear at the very start of the String? Then your .remove(0, 7) may not remove the characters you were expecting.

1 Like

Yes you are correct on that one. it is not a neat way. does arduino even do regulair expressions ?
When it removes the text in a different location, the remaining string most likely will also not only contain integers anymore, in which case the

int what = msg.substring(0, lengte).toInt();

Will return 0, and that is where things end then.

So it has some fault tolerance, but not as good as it should.

I guess you could try

  if (msg != "") {
    int i = msg.indexOf("setldr=");
    if ( i != -1) {
      int what = msg.substring(i+7).toInt();
      if (what > 0 && what < 1025) {
        EEPROM.put(0, what);
        ldrLow = what;
      }
    }

because if .toInt() finds something that is not a number, it will return 0, which seems like it is not valid in your sketch, so will be ignored.

1 Like

oh doh, of course. that was a stupid oversight. Things that happen when you realy should go to bed but want to find a solution to a problem. sloppy scripting :stuck_out_tongue:

Thanks!

If using an esp you must end with EEPROM. commit(); after your put, otherwise nothing will be saved