0
votes

I'm trying to store some data encapsulated into the object in the flash memory of ESP32.

It seems that writing object into the memory by putBytes works good, but I haven't an idea how to read it.

The way I tried to do this doesn't work...

#include <Preferences.h>
Preferences eeprom;

typedef struct {
  long  var1 = -1;
  long  var2 = -1;
  long  var3 = 0;
  byte  var4 = 0;
} someObject;

someObject object_[4][24];
someObject object_1_[4][24];

void setup() {
  Serial.begin(115200);
  eeprom.begin("Settings", false);

  object_[0][0].var1  = 25889;
  object_[0][0].var2   = 25890;
  object_[0][0].var3 = 25891;

  object_[1][2].var1  = 25892;
  object_[1][2].var2   = 25893;
  object_[1][2].var3 = 25894;

  eeprom.putBytes("someObject", &object_[4][24], sizeof(object_[4][24]));

  Serial.print("sizeof(object_[4][24]) = "); Serial.println(String(sizeof(object_[4][24])));

  size_t schLen = eeprom.getBytes("someObject", NULL, NULL);

  Serial.print("sizeof(someObject) = "); Serial.println(String(schLen));

  char buffer[schLen];

  eeprom.getBytes("someObject", &object_1_[4][24], schLen); // I know use of "&" is wrong, but havn't idea to fix it

  Serial.println("--------**********************************");
  Serial.println(String(object_[0][0].var1));
  Serial.println(String(object_[0][0].var2));
  Serial.println(String(object_[0][0].var3));

  Serial.println("--------");
  Serial.println(String(object_1_[1][2].var1));
  Serial.println(String(object_1_[1][2].var2));
  Serial.println(String(object_1_[1][2].var3));

  Serial.println("===========================================");

  Serial.println(buffer);
}

void loop() {}

Thanks in advance for any idea!

2
Shouldn't your getBytes call write the bytes to the buffer first? See: github.com/espressif/arduino-esp32/blob/master/libraries/…AniSkywalker
AniSkywalker, thanks for your reply! I use the example you have sent to write my code. but it's problematic for me to understand why they use array before writing the object... and how to adapt it for my needsIgor T.
I'm glad I could help! Don't forget to mark the solution that helped so others can find the answer as well.AniSkywalker

2 Answers

2
votes

Like in the example usage of Preferences (https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino), you need to read the bytes into the buffer before casting to a struct.

In your case, you are declaring the buffer but never using it. Since you know the exact size of the array, you should be able to directly copy it from buffer to object_.

char buffer[schLen]; // prepare a buffer for the data
eeprom.getBytes("someObject", buffer, schLen);
memcpy(object_, buffer, schLen);
0
votes

thanks for the help! working code:

#include <Preferences.h>
Preferences eeprom;

typedef struct {
  long  var1 = -1;
  long  var2 = -1;
  long  var3 = 0;
  byte  var4 = 0;
} someObject;

someObject object_[4][24];
someObject object_1_[4][24];

void setup() {
  Serial.begin(115200);
  eeprom.begin("Settings", false);

  object_[0][0].var1  = 25889;
  object_[0][0].var2   = 25890;
  object_[0][0].var3 = 25891;

  object_[1][2].var1  = 25892;
  object_[1][2].var2   = 25893;
  object_[1][2].var3 = 25894;

  eeprom.putBytes("someObject", &object_, sizeof(object_));

  Serial.print("sizeof(object_) = "); Serial.println(String(sizeof(object_)));

  size_t schLen = eeprom.getBytes("someObject", NULL, NULL);

  char buffer[schLen]; // prepare a buffer for the data

  eeprom.getBytes("someObject", buffer, schLen);
  memcpy(object_1_, buffer, schLen);

  Serial.print("sizeof(someObject) = "); Serial.println(String(schLen));

  Serial.println("--------**********************************");
  Serial.println(String(object_[0][0].var1));
  Serial.println(String(object_[0][0].var2));
  Serial.println(String(object_[0][0].var3));

  Serial.println("--------");
  Serial.println(String(object_1_[1][2].var1));
  Serial.println(String(object_1_[1][2].var2));
  Serial.println(String(object_1_[1][2].var3));

  Serial.println("===========================================");

  Serial.println(buffer);
}

void loop() {}