Skip to main content

EEPROM.write(pos, val) writeEEPROM.write(pos, val) writes one byte (valval) at the address giving by pospos. An "int" in ESP8266 takes 4 bytes, so it's a little more complicated, because EEPROM works in bytes, not int'sints.

Here is a code for writing one int "val"val at some position "pos"pos in the EEPROM:

void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}
void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}

and, of course, you need to read it back:

int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}
int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}

In Arduino you call EEPROM.begin()EEPROM.begin(), but in ESP8266 you have to call EEPROM.begin(n)EEPROM.begin(n), where "n"n is the total number of bytes you will need.

And remember that EEPROM have a short life span (by number of writes). You must minimize the number of writes!

EEPROM is permanent; you don't need to do nothing.

EEPROM.write(pos, val) write one byte (val) at the address giving by pos. An "int" in ESP8266 takes 4 bytes, so it's a little more complicated, because EEPROM works in bytes, not int's.

Here is a code for writing one int "val" at some position "pos" in the EEPROM:

void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}

and, of course, you need to read it back:

int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}

In Arduino you call EEPROM.begin(), but in ESP8266 you have to call EEPROM.begin(n), where "n" is the total number of bytes you will need.

And remember that EEPROM have a short life span (by number of writes). You must minimize the number of writes!

EEPROM is permanent; you don't need to do nothing.

EEPROM.write(pos, val) writes one byte (val) at the address giving by pos. An "int" in ESP8266 takes 4 bytes, so it's a little more complicated, because EEPROM works in bytes, not ints.

Here is a code for writing one int val at some position pos in the EEPROM:

void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}

and, of course, you need to read it back:

int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}

In Arduino you call EEPROM.begin(), but in ESP8266 you have to call EEPROM.begin(n), where n is the total number of bytes you will need.

And remember that EEPROM have a short life span (by number of writes). You must minimize the number of writes!

EEPROM is permanent; you don't need to do nothing.

Source Link
user31481
user31481

EEPROM.write(pos, val) write one byte (val) at the address giving by pos. An "int" in ESP8266 takes 4 bytes, so it's a little more complicated, because EEPROM works in bytes, not int's.

Here is a code for writing one int "val" at some position "pos" in the EEPROM:

void eeWriteInt(int pos, int val) {
    byte* p = (byte*) &val;
    EEPROM.write(pos, *p);
    EEPROM.write(pos + 1, *(p + 1));
    EEPROM.write(pos + 2, *(p + 2));
    EEPROM.write(pos + 3, *(p + 3));
    EEPROM.commit();
}

and, of course, you need to read it back:

int eeGetInt(int pos) {
  int val;
  byte* p = (byte*) &val;
  *p        = EEPROM.read(pos);
  *(p + 1)  = EEPROM.read(pos + 1);
  *(p + 2)  = EEPROM.read(pos + 2);
  *(p + 3)  = EEPROM.read(pos + 3);
  return val;
}

In Arduino you call EEPROM.begin(), but in ESP8266 you have to call EEPROM.begin(n), where "n" is the total number of bytes you will need.

And remember that EEPROM have a short life span (by number of writes). You must minimize the number of writes!

EEPROM is permanent; you don't need to do nothing.