eeprom string storage

Hello people.

I need some help, I will have to store many 9 character strings example (234597801, 675789501, 657896510) are ID coming from an external equipment.

So I need to store many of these numbers using the smallest eeprom address number, I use esp8266 with 4096 eerpom addresses, however if I write one character to each address I will have 455 ID's saved but I need to store more values as I could do that?

thanks in advance

9 decimal digit chars represent a range from 0 to 999999999. Ther range of an unsigned 32 bit integer is 0 to 4294967295.

Convert your numbers to uint32_t (strtoul) and you can store 1024 ids.

If you need more, add EEPROM or FRAM or a SD.

Ok, thanks for the reply.

But could you give me an example, or if I need some special library.

What did google tell you about 'strtoul'?

Use EEPROM.put() and EEPROM.get() to store and retrieve the values.

Sorry, I googled, but that’s too much for me.

I’ll wait if someone can post a simple example of how to do

rodrigo_cirilo:
Sorry, I googled, but that’s too much for me.

I’ll wait if someone can post a simple example of how to do

While you’re waiting use your time productively and peruse the Arduino reference and playground. Maybe do a site search on ‘eeprom’.

Just sayin’.

rodrigo_cirilo: Sorry, I googled, but that's too much for me.

I'll wait if someone can post a simple example of how to do

You know that brains atrophy if they are not used?

haha ha. Yes I know..

It's just that I'm programming both for ESP and for an app, there will be communication between them.

Anyway, now I will not have time to analyze and test everything I have available, in C for PIC (microchip) I know how to do this, I used a lot already, but now in Arduino IDE I am time this problem.

If it's easy for you to put a simple example of how to do it, I'd be grateful, but if it's hard work you can let me turn around and run after the information.

I gave you massive hints, if that is not enough to write the code, you will have to look up the three functions.

And no, I don't support copy and paste programmers.

Yes I do not support either, I usually take examples and modify my need.

However, I saw a lot of material available in English, I am Brazilian, and my English is very beginner, I confuse myself with words, so it is a little complicated to understand all material available on the Internet.

However I thought of a way to continue taking up less eeprom space, I'll take the ID (string) split into 5 2-character strings (45, 68, 89 for example) then convert to byte, then instead of occupying 9 addresses. eeprom to save each ID, I would only occupy 5.

It may not be the best option, but so I think I can do it, realize that it is not lack of willpower, it is by not knowing the correct paths and a simple example would help me to follow the best path.

Whandall: I gave you massive hints, if that is not enough to write the code, you will have to look up the three functions.

You don't need to excuse your lazyness.

Don't whine, look up the three functions you were hinted to use, I'm shure each one comes with an example.

I will not comment your alternative strategy.

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:

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?)

Whandall: Use EEPROM.put() and EEPROM.get() to store and retrieve the values.

Do they work in ESP8266?

GolamMostafa:
Do they work in ESP8266?

Where did you read they don’t?

Whandall: Where did you read they don't?

Or did I experiment with ESP8266? My result of experiment has many supportive posts in the web.

Maybe it was not enumerated enough for you.

  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

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)

#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:

eepromESP.png

GolamMostafa:
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.

#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() {}
rBack = 0xDEADBEEF

I did not see the very first rBack value, but I assume it to be 0xFFFFFFFF.

  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

Whandall:
This works for me on a NodeMCU.

This is the results of your codes of Post#17 (after execution) in my NodeMCU (ESP8266).

espW.png