0

I'm using Wemos mini in my project. I wish to store a Boolean value in cell #0, which changes every boot.

Since writing to EEPROM is limited to roughly 100,000 cycles - I want to store in cell #1 the amount of writing cycles done so far, so when reaching 100,000 , value will be stored in cell#2 and writing cycles will be stored in cell#3 and so on.

Reading and writing using #include <EEPROM.h> is done as needed, but since each cell can store a byte, it can only reach 255.

My question is how to store a value of 10^6 (except of convert it to a*2^0 + b*2^1 + c*2^2 + d*2^3....).

Guy

EDIT1

Part or relevant code:

void setup() {
        Serial.begin(9600);
        Serial.println("BEGIN:");
        if (HARD_REBOOT) {
                EEPROM.begin(1024);
        }
        if (HARD_REBOOT) {
                EEPROM.write(0,100000);
                EEPROM.commit();
                delay(50);
                Serial.print("value in flash: ");
                Serial.println(EEPROM.read(0));
        }
}

and its output:

Ȥl⸮⸮BGS[⸮<b8ɗ⸮BEGIN:
22:40:58.732 -> value in flash: 160
  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO Sep 8 at 20:58
2

The esp doesn't have an eeprom, but emulates it in its flash. Therefore 100.000 cylces will not apply, it's more like 10.000 cycles for flash.

The easiest way to prepare a datatype to be stored as bytes is to use union like follows:

union Converter{
  uint32_t ui32;
  byte eeprom_bytes[4];
}convert;

void convert_for_eeprom (uint32_t your_number) {
  convert.ui32=your_number;
  // convert.eeprom_bytes contains the 4-byte represenstation of your number, write those to eeprom
}

uint32_t convert_from_eeprom () {
  // read four bytes from eeprom and assign those to convert.eeprom_bytes
  return convert.ui32;
}

Note that this isn't a type conversion and the value of convert.eeprom_bytes are not meant to be processed, as they are simply interpreted as if they were a four-byte array, indeed those bytes contain "dumb" values. This way you can "convert" any datatype for byte-wise storage and read-back respectively.

Another option to convert a 32 bit integer to 4 bytes is by using bit-wise operations:

uint32_t your_number=100000;
byte b1=your_number;
byte b2=your_number>>8;
byte b3=your_number>>16;
byte b4=your_number>>24;

But this only makes sense for integers and I suggest taking a look at union.

  • 1
    EEPROM.put() can write any type. that is the easiest way – Juraj Sep 7 at 13:27
2

to save any variable to EEPROM use EEPROM.put(), to read it use EEPROM.get(). example

EEPROM.put(EEPROM_COUNT_ADDR, count);

EEPROM.get(EEPROM_COUNT_ADDR, count);

On esp8266 EEPROM is emulated in flash and there the whole page wears out. The EEPROM library of esp8266 does what can be done to protect the flash pages used for EEPROM emulation.

  • But still can use ‘put’ and ‘get’? – Guy . D Sep 9 at 11:26
  • yes. the esp8266 EEPROM library has put() and get() too. – Juraj Sep 9 at 12:55
  • substitute write in put, was OK, but when using get yielded : error: no matching function for call to 'EEPROMClass::get(int)' Serial.println(EEPROM.get(0)); – Guy . D Sep 9 at 20:11
  • EEPROM.get(EEPROM_COUNT_ADDR, count); ! Use second parameter for a variable to be filled. – Juraj Sep 10 at 6:06
  • @Guy.D, does it work? – Juraj Sep 12 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.