Arduino: [Solved] NodeMcu v1.0 (ESP8266 12E) Store/Read an (int) using the EEPROM

Created on 22 May 2017  路  3Comments  路  Source: esp8266/Arduino

Hello, I want to store a value (int) of four numbers like (int a=1111) in the NodeMcu v1.0 EEPROM then when I turn off and on again the board the int i will stay equal to 1111.
I tried this code on my Arduino UNO board and it works fine, to store:

#include <EEPROM.h>

void setup() {

  Serial.begin(9600);

  int a=1111;
  int eeAddress = 0;

  EEPROM.put(eeAddress, a);

  eeAddress += sizeof(int);

  EEPROM.put(eeAddress, a);
  Serial.print("Written");
}

void loop() {
  /* Empty loop */
} 

And this one to read the data when I unplug and plug the board again:

#include <EEPROM.h>

void setup() {

  int a;
  int eeAddress = 0; 

  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  EEPROM.get(eeAddress, a);

  Serial.println(a);

}


void loop() {
  /* Empty loop */
} 

Now when I use it on the NodeMcu board I always get the same value whatever the int I store

Most helpful comment

I found the solution It works now.
To store a value

#include <EEPROM.h>

void setup() {

  Serial.begin(115200);
  EEPROM.begin(512);

  int a=5412;
  int eeAddress = 0;

  EEPROM.put(eeAddress, a);

  eeAddress += sizeof(int);

  EEPROM.put(eeAddress, a);
  EEPROM.commit();
  Serial.print("Written");
}

void loop() {
  /* Empty loop */
} 

To read that value

#include <EEPROM.h>

void setup() {

  int a;
  int eeAddress = 0; 

  Serial.begin(115200);
  EEPROM.begin(512);
  while (!Serial) {
    ; 
  }
  EEPROM.get(eeAddress, a);

  Serial.println(a);

}


void loop() {
  /* Empty loop */
}

All 3 comments

I found the solution It works now.
To store a value

#include <EEPROM.h>

void setup() {

  Serial.begin(115200);
  EEPROM.begin(512);

  int a=5412;
  int eeAddress = 0;

  EEPROM.put(eeAddress, a);

  eeAddress += sizeof(int);

  EEPROM.put(eeAddress, a);
  EEPROM.commit();
  Serial.print("Written");
}

void loop() {
  /* Empty loop */
} 

To read that value

#include <EEPROM.h>

void setup() {

  int a;
  int eeAddress = 0; 

  Serial.begin(115200);
  EEPROM.begin(512);
  while (!Serial) {
    ; 
  }
  EEPROM.get(eeAddress, a);

  Serial.println(a);

}


void loop() {
  /* Empty loop */
}

You need EEPROM.commit() after all your EEPROM.put() in the first sketch.
see example
reason: EEPROM is emulated in ram

I did it, now it's fine.
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hoacvxd picture hoacvxd  路  3Comments

dariopb picture dariopb  路  3Comments

pablotix20 picture pablotix20  路  3Comments

mreschka picture mreschka  路  3Comments

Chagui- picture Chagui-  路  3Comments