How can I have a value survive Power Off?

I have a program which (among other things) develops a value (of the form int) which I would like to be used the next time power is applied.

My processor is the NodeMCU, and my program uses the library function wifiManager.autoConnect(); which stores the WiFi credentials perfectly. (Any time power is turned off and then on again, the network comes up correctly. If I go to a different location, I use WiFi.disconnect(); to erase the original credentials and replace them with new ones.) However, I have no idea what is going on "behind the screen" to figure out how these functions work.

From what I've read, the EEPROM is available for this, (and I can probably follow some examples to use it) but in case wifiManager.autoConnect(); is using EEPROM as well, I don't want to jeopardize something that is working . Or is the network info being stored somewhere else?

Can someone offer me advice on how to proceed?

Basically yes, you need to store the values in non-volatile memory.
On Arduino, you’d probably use EEPROM, or on an ESP the SPIFFS file system.

EEPROM.put() and .get() will make your life easier.

on an ESP the SPIFFS file system.

Now apparently deprecated in favour of LittleFS

Aah, ok.

mattbaum:
I don't want to jeopardize something that is working . Or is the network info being stored somewhere else?
Can someone offer me advice on how to proceed?

it will not interfere. See the small "graphic" on Filesystem — ESP8266 Arduino Core 3.0.0-12-gbf6f3e7c documentation for the flash layout.

For just "store" some values I would use the eeprom emulation on the ESP.
put() and get() will give you a very easy access to your values.

First, thanks for the suggestions and for giving me some confidence in the system.

However, I still can’t get things to work.
I have included:
#include <EEPROM.h> as well as #include “LittleFS.h”
and have previously defined:
int tcorr;

I’ve tried both:
ESP.put(tcorr); and EEPROM.put(tcorr);
and with “put” in upper, lower and combined cases - all with error messages such as:
‘class EspClass’ has no member named ‘put’
I’d like to understand what I’m doing wrong, but at this point I’d just like to get it to work!
Suggestions?

I have included:
#include <EEPROM.h> as well as #include “LittleFS.h”

why?
read this

https://arduino-esp8266.readthedocs.io/en/latest/libraries.html?highlight=EEPROM#eeprom

follow the examples!

I used this library to read and write data from a NodeMCU.

Using the ESP8266 EEPROM is different from the standard Arduino EEPROM class. You need to call EEPROM.begin(size) before you can start reading or writing, where the size parameter is the number of bytes you want to use store Size can be anywhere between a minimum of 4 and maximum of 4096 bytes.

In my project I learned to use struct for my data so that it would be a single read or write.

I can share code, but it's been more than a year since that project, so my recollection of how it works will be rusty.

Thanks, everyone for your help.

I finally have things working. My program now includes the following, which works:

#include <EEPROM.h>
int tcorr;
tcorr = 123;
EEPROM.begin(4);
EEPROM.write( 1, tcorr );
TT = EEPROM.read(1 );

I assume that this does not overwrite any other stored values, although I have not tested this yet.
In the course of my testing I also tried using put and get - with compiler errors.
I’d still like to have a better “handle” on what is going on, although for now my problem seems to have been solved.

Thanks again -
Matt

int  tcorr;
tcorr = 123;
//later
EEPROM.write( 1, tcorr ); 
TT = EEPROM.read(1 );

EEPROM.write() writes one byte
EEPROM.read() reads one byte
ints are 2 bytes

Can you see a potential problem ?

What problems did you have with put() and get() ?

I'm still not out of the woods!

I read somewhere that for my (NodeMCU) system, ints are 4 bytes.
Therefore (after my initial posts) I have used EEPROM.begin(4), and EEPROM.write(4, tcorr); and EEPROM.read(4, tcorr);
These compile, but what I read is not what I thought I had written! ( It should be an int of 4 bytes.)

Also, I had originally tried things like EEPROM.put( 4, tcorr ) and EEPROM.get(4,tcorr);
with compiler errors. I suspect that this was before I had used EEPROM.begin(4); but the compiler error messages implied that the get() and put() were not legal statements. Now it compiles with either the write/read or the put/get statements.

Now everything compiles and either EEPROM.write(4, tcorr); or EEPROM.put(4, tcorr); seems to work for writing, but either I am reading from the wrong place or else I am fooling myself with the writing - or both. The net result is that I do not get any compiler errors, but what I read is not what I write!
Any help is appreciated.

oh boy, what are you doing?!?

See the IDE ESP8266 examples for EEPROM read and EEPROM write.

EEPROM.begin(512);

will define the reserved space for your EEPROM emulation. don't use something else as long as you don't know what you are doing. This belongs into the setup in sketches where you need the EEPROM emulation

EEPROM.put( 0, tcorr );

will prepare to write your variable tcorr beginning at address 0

EEPROM.commit();
will finalize your write.

later on (or on next startup in setup):

you read again back from start address 0 into your variable:

EEPROM.get(0, tcoor);

I read somewhere that for my (NodeMCU) system, ints are 4 bytes.

That just makes the problem worse

Thanks, noiasca -
I think I have everything working now, using the code you suggested:

EEPROM.begin(512); (In setup)

EEPROM.put( 0, tcorr );
EEPROM.commit(); // to finalize the write.
and
EEPROM.get(0, tcoor); // to read the stored value.

The main thing seemed to be the "commit" line.

This also doesn't affect the network stored values.

However, there are two things I don't understand.

  1. Since I've said EEPROM.begin(512); (In setup), how does the system know where to put my value, and not to wipe out other stored values (such as the network credentials, which seem to survive OK.) ?
  2. get and put work fine, but the "instructions" say to use write and read. Why the difference?

My main concern was to get the system going, which it is now. However I also like to know (if possible) what is really going on. Any further comments will be appreciated.

  1. Since I've said EEPROM.begin(512); (In setup), how does the system know where to put my value, and not to wipe out other stored values (such as the network credentials, which seem to survive OK.) ?

it will not interfere. See the small "graphic" on Filesystem — ESP8266 Arduino Core 3.0.0-12-gbf6f3e7c documentation for the flash layout.

  1. get and put work fine, but the "instructions" say to use write and read. Why the difference?

read and write will handle one byte only, but get and put will handle all bytes needed for your variable. Please check the Arduino description for more details:

https://www.arduino.cc/en/Reference/EEPROMPut
https://www.arduino.cc/en/Reference/EEPROMGet
https://www.arduino.cc/en/Reference/EEPROMWrite
https://www.arduino.cc/en/Reference/EEPROMRead