write a string into ESP8266 eeprom

Hi guys.
I have a 19 characters string and I want to store it into ESP8266 eeprom:

char string1[20]="sampleStringNumer01";

I'm really new to arduino,so please help me with a sample code.
Thanks :wink:

so please help me with a sample code.

for(byte b=0; b<strlen(string1); b++)
{
   char c = string1[b];
   // Store c in position n+b in EEPROM
}

You get to replace the comment with the appropriate code to store data in EEPROM on the not-really-an-Arduino.

When I write to eeprom with the following code:

char test[8]="TESTi10";
void setup(){
   EEPROM.begin(20);                              
  for(int cc=0 ;cc<9;cc++){
    int value = test[cc]-'0'; //convert char to int
    EEPROM.put(cc,value);
    EEPROM.end();
    delay(20);
    Serial.println(value);
    delay(500);
  }
}

I get this in terminal(not bad):

36
21
35
36
57
1
0
-48
-39

But when I read the stored data with the following code:

void loop(){
EEPROM.begin(20);
  for(int cd=0;cd<9;cd++){
    EEPROM.get(cd,raw[cd]);
   
  }
  EEPROM.commit();
    EEPROM.end();
  for(int cd=0;cd<9;cd++){
    Serial.println(raw[cd]);
  }
   
}

I get this in terminal:

36
956301312
20512768
80128
-805306055
-640679935
-2502656
-9776
-39

as you can see only the first character saved on eeprom and others are rubish!
WHY ?? :slightly_frowning_face:

    int value = test[cc]-'0'; //convert char to int

This is meaningless when test[cc] is ‘T’, for instance.

Reading all 8 of the 7 values in the array is a strange thing to do, too.

I can’t figure out why you call EEPROM.begin() before the for loop, and then call EEPROM.end() in the for loop. I would expect that only the first (mangled) character was written to EEPROM.

And, sure enough, that seems to be what happened.

Thank you PaulS.
My code is according to this

This is meaningless when test[cc] is 'T', for instance.

Is that wrong?what is the easy way to convert a character to integer?

Can you please help me with correct code?

Is that wrong?what is the easy way to convert a character to integer?

What is the integer equivalent to T? To E?

I need to convert characters to integer,so I can save them into eeprom and then reconvert integer to character.

For those who may have similar issue.
This code solveed my problem :slight_smile:

#include <EEPROM.h>

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  delay(3000);
  Serial.println();
  Serial.println();
  Serial.println("Startup");
 

//write to eeprom
  String qsid="Global";
  int charLength=qsid.length();
  
  Serial.println("writing eeprom ssid:");
          for (int i = 0; i < qsid.length(); ++i)
            {
              EEPROM.write(i, qsid[i]);
              Serial.print("Wrote: ");
              Serial.println(qsid[i]); 
            }


//read to eeprom
  
  Serial.println("Reading EEPROM ssid");
  String esid;
  for (int i = 0; i < charLength; ++i)
    {
      esid += char(EEPROM.read(i));
    }
    //esid.trim();
    Serial.println(esid.length());
  Serial.print("SSID: ");
  Serial.println(esid);

source

I need to convert characters to integer,so I can save them into eeprom and then reconvert integer to character.

No, you do not. An int can contain a value in the range -32768 to 32767. A char can contain a value in the range -128 to 127. All ASCII characters are in the range 0 to 127. You can only store bytes in EEPROM, which means values in the range 0 to 255.

  String qsid="Global";
  int charLength=qsid.length();
 
  Serial.println("writing eeprom ssid:");
          for (int i = 0; i < qsid.length(); ++i)
            {
              EEPROM.write(i, qsid[i]);

What is the type returned by the operator in the String class? Why didn’t you f**k around trying to convert that char to an int? Oh,yeah, because you don’t need to.

You're right, dear friend.
When I saw that code, I realized my mistake.
Thank you very much for your help :wink:

Hi

Also have a look at

and consider use EEPROM.begin(sizeof(ConfigStruct))
dicussed here Weird behaviour with EEPROM.begin() · Issue #942 · esp8266/Arduino · GitHub