0

Hey all I have the following code in my Arduino IDE:

#include <EEPROM.h>
#include <ArduinoJson.h>

bool debuggin         = true;
int brightness        = 255;
int ambientLight      = 30;
struct RGBLA {
  uint8_t R;
  uint8_t G;
  uint8_t B;
  uint8_t L;
  uint8_t A;
};

void setup() {
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  EEPROM.begin(512);
  readSavedSettings();
  updateEEProm(255,255,255,200,35,true,false);
}

RGBLA readEEProm() {
  int addr = 0;
  RGBLA customVars;

  EEPROM.get(addr, customVars);
  return customVars;
}

void readSavedSettings() {
  RGBLA returnedVars = readEEProm();
  if (debuggin) { Serial.println("START readSavedSettings"); }

  if (returnedVars.A == 00 || returnedVars.A == 0) {
    if (debuggin) { Serial.println("'A' not found! Default 30"); }
  } else {
    ambientLight = returnedVars.A;
    if (debuggin) {
      Serial.print("'A' found! ");
      Serial.println(ambientLight);
    }
  }

  for(uint8_t i = 0; i < 3/*strip.numPixels()*/; i++) {
    if (debuggin) {
      Serial.print("LED ");
      Serial.print(i);
      Serial.print(" set to: R: ");
      Serial.print((uint8_t)returnedVars.R);
      Serial.print(", G: ");
      Serial.print((uint8_t)returnedVars.G);
      Serial.print(", B: ");
      Serial.println((uint8_t)returnedVars.B);
    }
  }

  if (debuggin) {
    Serial.println("Turned off RGB LEDS");
    Serial.println("END readSavedSettings");
  }
}

void updateEEProm(uint8_t R, 
                  uint8_t G, 
                  uint8_t B, 
                  uint8_t L, 
                  uint8_t A, 
                  bool saveRGBL,
                  bool saveA) {
  int addr = 0;
  RGBLA customVars;

  //Save old json before clearing EEProm
  EEPROM.get(addr, customVars);

  //Clear EEProm
  for (unsigned int i = 0; i < EEPROM.length(); i++) {
    EEPROM.write(i, 0);
  }

  if (saveRGBL) {
    //Get A value so that we can save it the same value
    DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(5));
    JsonObject& root = jsonBuffer.createObject();

    if (debuggin) {
      Serial.print("Old JSON:");
      Serial.print(" R: ");
      Serial.print((uint8_t)customVars.R);
      Serial.print(" G: ");
      Serial.print((uint8_t)customVars.G);
      Serial.print(" B: ");
      Serial.print((uint8_t)customVars.B);
      Serial.print(" L: ");
      Serial.print((uint8_t)customVars.L);
      Serial.print(" A: ");
      Serial.println((uint8_t)customVars.A);
    }

    root["R"] = (uint8_t)R;
    root["G"] = (uint8_t)G;
    root["B"] = (uint8_t)B;
    root["L"] = (uint8_t)L;
    root["A"] = (uint8_t)customVars.A;

    if (debuggin) {
      Serial.print("New JSON:");
      Serial.print(" R: ");
      Serial.print((uint8_t)root["R"]);
      Serial.print(" G: ");
      Serial.print((uint8_t)root["G"]);
      Serial.print(" B: ");
      Serial.print((uint8_t)root["B"]);
      Serial.print(" L: ");
      Serial.print((uint8_t)root["L"]);
      Serial.print(" A: ");
      Serial.println((uint8_t)root["A"]);
    }

    EEPROM.put(addr, root);
  }

  if (saveA) {
    //Get R,G,B,L values so that we can save it the same value
    DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(5));
    JsonObject& root = jsonBuffer.createObject();

    if (debuggin) {
      Serial.print("Old JSON:");
      Serial.print(" R: ");
      Serial.print((uint8_t)customVars.R);
      Serial.print(" G: ");
      Serial.print((uint8_t)customVars.G);
      Serial.print(" B: ");
      Serial.print((uint8_t)customVars.B);
      Serial.print(" L: ");
      Serial.print((uint8_t)customVars.L);
      Serial.print(" A: ");
      Serial.println((uint8_t)customVars.A);
    }

    root["R"] = (uint8_t)customVars.R;
    root["G"] = (uint8_t)customVars.G;
    root["B"] = (uint8_t)customVars.B;
    root["L"] = (uint8_t)customVars.L;
    root["A"] = (uint8_t)A;

    if (debuggin) {
      Serial.print("New JSON:");
      Serial.print(" R: ");
      Serial.print((uint8_t)root["R"]);
      Serial.print(" G: ");
      Serial.print((uint8_t)root["G"]);
      Serial.print(" B: ");
      Serial.print((uint8_t)root["B"]);
      Serial.print(" L: ");
      Serial.print((uint8_t)root["L"]);
      Serial.print(" A: ");
      Serial.println((uint8_t)root["A"]);
    }

    EEPROM.put(addr, root);
  }
}

void loop() {
}

And this is the output of that code:

START readSavedSettings
'A' found! 96
LED 0 set to: R: 48, G: 255, B: 255
LED 1 set to: R: 48, G: 255, B: 255
LED 2 set to: R: 48, G: 255, B: 255
Turned off RGB LEDS
END readSavedSettings
Old JSON: R: 48   G: 255 B: 255 L: 63   A: 96
New JSON: R: 255  G: 255 B: 255 L: 200  A: 96
Old JSON: R: 48   G: 255 B: 255 L: 63   A: 160
New JSON: R: 48   G: 255 B: 255 L: 63   A: 35

Now when I reset it this is the output:

⸮dOX⸮,R⸮X⸮4xC⸮⸮⸮START readSavedSettings
'A' found! 96
LED 0 set to: R: 48, G: 255, B: 255
LED 1 set to: R: 48, G: 255, B: 255
LED 2 set to: R: 48, G: 255, B: 255
Turned off RGB LEDS
END readSavedSettings
Old JSON: R: 48   G: 255 B: 255 L: 63   A: 96
New JSON: R: 255  G: 255 B: 255 L: 200  A: 96
Old JSON: R: 48   G: 255 B: 255 L: 63   A: 160
New JSON: R: 48   G: 255 B: 255 L: 63   A: 35

How come the Old JSON has the 'L' value as 63 again when it should be the New JSON value of 200? Why did it not save it but saved all the others? Also, Why is 'R' 48 when it should be 255 like G and B?

Is there something wrong in my code?

Update 1

This is the output after adding .end() to it:

START readSavedSettings
'A' found! 160
LED 0 set to: R: 48, G: 255, B: 255
LED 1 set to: R: 48, G: 255, B: 255
LED 2 set to: R: 48, G: 255, B: 255
Turned off RGB LEDS
END readSavedSettings
Old JSON: R: 48   G: 255  B: 255  L: 63 A: 160
New JSON: R: 255  G: 255  B: 255  L: 200 A: 160
Old JSON: R: 128  G: 12   B: 17   L: 17 A: 60
New JSON: R: 128  G: 12   B: 17   L: 17 A: 35

and the code i use:

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  EEPROM.begin(512);
  readSavedSettings();
  updateEEProm(255,255,255,200,35,true,false); //Save all but 'A' value
  EEPROM.end();
  updateEEProm(255,255,255,200,35,false,true); //Save just 'A' value
  EEPROM.end();
}
9
  • you should try SPIFFS instead of EEPROM: ez file interface, 1-3MB of space, no slicing or delimiters needed....
    – dandavis
    Sep 4, 2018 at 15:48
  • @dandavis, true. the emulation library has one small advantage. it is not erased when you upload SPIFFS image
    – Juraj
    Sep 4, 2018 at 16:52
  • Why do you put the Json root to EEPROM at he end of updateEEProm?
    – Juraj
    Sep 5, 2018 at 17:19
  • @Juraj do you mean in the .put() part?
    – StealthRT
    Sep 5, 2018 at 17:21
  • you read the RGBLA struct but you write the json to EEPROM. which is not suitable for EEPROM
    – Juraj
    Sep 5, 2018 at 17:22

1 Answer 1

2

The esp8266 EEPROM emulation library requires a commit() call to save the memory image of EEPROM to flash.

Add EEPROM.commit(); or EEPROM.end(); as last line in your updateEEProm() function. Documentation link:

Second problem is that you don't write the RGBLA struct to EEPROM, instead you try to write the variable root, which is an instance of class JsonObject. Class types can't be put into EEPROM.

3
  • 1
    So this works even when using .put()?
    – StealthRT
    Sep 4, 2018 at 12:08
  • of course. all emulating functions work with the RAM image of the emulated EEPROM. commit() or end() writes the image to flash. begin() reads the image from flash to RAM.
    – Juraj
    Sep 4, 2018 at 12:38
  • Hum, even doing updateEEProm(255,255,255,200,35,true,false); EEPROM.commit(); updateEEProm(255,255,255,200,35,false,true); EEPROM.commit(); still does not seem to save it?
    – StealthRT
    Sep 5, 2018 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.