Go Down

Topic: eeprom string storage (Read 336 times) previous topic - next topic

Whandall

Maybe it was not enumerated enough for you.

Code: [Select]
  template<typename T>
  T &get(int const address, T &t) {
    if (address < 0 || address + sizeof(T) > _size)
      return t;

    memcpy((uint8_t*) &t, _data + address, sizeof(T));
    return t;
  }

  template<typename T>
  const T &put(int const address, const T &t) {
    if (address < 0 || address + sizeof(T) > _size)
      return t;
    if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) {
      _dirty = true;
      memcpy(_data + address, (const uint8_t*)&t, sizeof(T));
    }

    return t;
  }


https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/EEPROM.h
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

GolamMostafa

#16
Aug 30, 2019, 08:28 pm Last Edit: Aug 31, 2019, 05:04 am by GolamMostafa
Experiment and web information have established apparently reveal the following facts: [edit]

1.  EEPROM.put() and EEPROM.get() functions are not valid working for ESP8266's EEPROM. [edit]


2.  EEPROM.write() and EEPROM.read() functions are valid for ESP8266's EEPROM.

3.  EEPROM.begin() and EEPROM.commit() are also two needed functions for ESP8266's EEPROM.

Example Codes (Writing the string "234597801" into EEPROM of ESP8266 and reading back)
Code: [Select]
#include <EEPROM.h>
char myData[] = "234597801";
byte readArray[4];
unsigned long y;

union
{
  unsigned long x;
  byte myData[4];
} data;

void setup()
{
  Serial.begin(115200);
  y = strtoul(myData, nullptr, 10);
  Serial.println(y, HEX);  //shows: DFBADA9 => 0DFBADA9
  Serial.println("====");
  data.x = y;

  EEPROM.begin(512);  //Initialize EEPROM
  for (int i = 0; i < 4; i++)
  {
    EEPROM.write(0x0010 + i, data.myData[i]);
  }
  EEPROM.commit();    //Store data to EEPROM
}

//--read back written data from EEPROM and show on Serial Monitor at 1-sec interval-
void loop()
{
  int addr = 0x0010;
  Serial.println(""); //go next line as ESP prints garbage
  Serial.println("Reading back previously written from EEPROM of ESP8266");
  for (int i = 0; i < 4; i++)
  {
    readArray[i] = EEPROM.read(0x0010 + i);
    if (readArray[i] < 0x10)
    {
      Serial.print('0');
    }
    Serial.print(readArray[i], HEX);
    Serial.print(' ');
  }
  delay(1000);
}


Screnshot:

Whandall

1.  EEPROM.put() and EEPROM.get() functions are not valid for ESP8266's EEPROM.
Enumerated, but wrong information.

This works for me on a NodeMCU.

Code: [Select]
#include <EEPROM.h>

uint32_t tValue = 0xDEADBEEF;
uint32_t rBack = 0x2BAD2BAD;

void setup() {
  Serial.begin(250000);
  EEPROM.begin(512);
  EEPROM.get(0, rBack);
  EEPROM.put(0, tValue);
  Serial.print("rBack = 0x");
  Serial.println(rBack, HEX);
  EEPROM.commit();
}

void loop() {}

Code: [Select]
rBack = 0xDEADBEEF

I did not see the very first rBack value, but I assume it to be 0xFFFFFFFF.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

cattledog

Quote
1.  EEPROM.put() and EEPROM.get() functions are not valid for ESP8266's EEPROM.
Incorrect. See this thread
https://forum.arduino.cc/index.php?topic=629263.0


GolamMostafa

#19
Aug 31, 2019, 04:31 am Last Edit: Aug 31, 2019, 04:53 am by GolamMostafa
This works for me on a NodeMCU.
This is the results of your codes of Post#17 (after execution) in my NodeMCU (ESP8266).


GolamMostafa

#20
Aug 31, 2019, 04:48 am Last Edit: Aug 31, 2019, 05:35 am by GolamMostafa
Incorrect. See this thread
https://forum.arduino.cc/index.php?topic=629263.0
For some reasons (because, I did not insert EEPROM.commit()) EEPROM.put() and EEPROM.get() did not work for me before in my ESP8266....


Now I have tried being warned by your post, and the methods (.put() and .get()) are found to be working on the same ESP8266. I stand corrected with + for you. The difference between the two program is: the present program contains the EEPROM.commit(); whereas, the 'non working program' did not contain EEPRO.commit().

Code: [Select]
#include <EEPROM.h>
unsigned long y = 0x123456CD;
unsigned long z;

void setup()
{
  Serial.begin(115200);
  EEPROM.begin(512);  //Initialize EEPROM
  /*
  for (int i = 0; i < 4; i++) //loop upto string lenght www.length() returns length of string
  {
    EEPROM.write(0x0010 + i, data.myData[i]); //Write one by one with starting address of 0x0F
  }*/
  EEPROM.put(0x0010, y);
  EEPROM.commit();    //Store data to EEPROM
}

//--read back written data from EEPROM and show on Serial Monitor at 1-sec interval-
void loop()
{
  int addr = 0x0010;
  Serial.println(""); //go next line as ESP prints garbage
  Serial.println("Reading back previously written from EEPROM of ESP8266");
  /*
  for (int i = 0; i < 4; i++)
  {
    readArray[i] = EEPROM.read(0x0010 + i);
    if (readArray[i] < 0x10)
    {
      Serial.print('0');
    }
    Serial.print(readArray[i], HEX);
    Serial.print(' ');
  }*/
  EEPROM.get(0x0010, z);
  Serial.print(z, HEX);
  delay(1000);
}



cattledog

If you want to read the espressive boot log from the ROM, set the baud rate for 74888 and the ??? garbage is printed out.

I would write @Whandall's sketch a little differently, but if I run it as written using a Wemos D1, I see

rBack = 0xDEADBEEF

After the program loads 0xDEADBEEF into eeprom,  I modify the sketch to remove all references to .put() and up load again. The 0xDEADBEEF is retained for the .get().

I can cycle power to the Wemos, push the reset button and see the OXDEADBEEF read back.

I have used EEPROM.put() and EEPROM.get() many times successfully with the 2.4.2
Arduino core for the esp8266.

Please post some esp8266 code where the EEPROM.put() and EEPROM.get() functions do not work properly.



rodrigo_cirilo

1.  Let us take one string: 234597801

2.  Memory mapping of string of Step-1 is: 32 33 34 35 39 37 38 30 31 (spaces are for clarity).

3.  You want to save it into EEPROM of ESP using minimum locations --
(1)  Normal storage of the data of Step-2 would require: 9 locations.
(2)  Equivalent integer (234597801 ==> 0x0DFBADA9) would require: 4 locations.
(3)  Convert string data of Step-2 into unsigned long integer data:
Code: [Select]
void setup()
{
  Serial.begin(9600);
  char myData[] = "234597801";
 
  unsigned long x = strtoul(myData, nullptr, 10);
  Serial.print(x, HEX);  //shows: DFBADA9 => 0DFBADA9
}

void loop()
{
 
}


4.  Store 4-byte data of Step-3(2) into internal EEPROM (starting at location 0x0010) of ESP8266 using codes to be written by OP. (My codes read back 0. Have data been written?)
Thank you so much for your example.

I managed to solve using 5 memory addresses, but your help will save me even more memory addresses.

And thank you all for even more useful information that has been posted.

Go Up