Hey, i have my WiFi credentials for my nodemcu stored in a local string variable which captures string data from WiFiManager library, the variable gives a string output and works perfectly fine, but the problem is that if the module reboots when it was disconnected from wifi, the WiFiManager library deletes the saved SSID and PASS, and the module reads them on boot up, so if the library deletes them, the local variable will also be filled with an empty string. so i wanted someone to help me with a code that can read and write the output of GETSSID and GETPASS variables in EEPROM of the module. the rest all is working fine, hence i have not provided the entire code, the output of the variables is also in string format and is working perfectly fine, i just wanted a code to store the output in EEPROM and read it back when needed.
String GETSSID = wifiManager.getSSID();
String GETPASS = wifiManager.getPassword();
Hey peter,
i tried all the functions of EEPROM library that you mentioned, i even tried EEPROM.put() which i believed will work, but it didnt. i always get output as 0 whenever i read it again. any more suggestions??
String GETSSID = wifiManager.getSSID();
String GETPASS = wifiManager.getPassword();
You cannot store a String object in eeprom. It is only a pointer to an underlying character array. You must store that character array, not a pointer to it.
Use EEPROM.put with GETSSID.c_str() and GETPASS.c_str().
EEPROM.get() will also return a null terminated character array, not a String object.
thank you for your help but it dont seems to be working
can anyone please just give me a code extract of how to write a string variable output to EEPROM? btw i checked all the examples and documentations.
I do not use Strings and String functions very often, and it appears that .c_str() does not convert to a storable character array. Use strcpy() to place the conversion to a defined character array. These sketchs store and retrieve on a WemosD1.
Sketch to Save with eeprom.put()
#include <EEPROM.h>
String myStringPassword = "some_password";
String myStringSSID = "some_routerID";
char myCharPassword[20] = "";
char myCharSSID[20] = "";
int addr = 0;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("esp8266 String save to eeprom example");
EEPROM.begin(512);
Serial.println(myStringPassword.c_str());
Serial.println(myStringSSID.c_str());
strcpy(myCharPassword, myStringPassword.c_str());
strcpy(myCharSSID, myStringSSID.c_str());
//EEPROM.put(addr, myStringPassword.c_str() );
//EEPROM.put(addr + 50, myStringSSID.c_str());
EEPROM.put(addr, myCharPassword );
EEPROM.put(addr + 50, myCharSSID);
EEPROM.commit();
}
void loop()
{
Serial.print("myStringPassword = ");
Serial.println(myStringPassword);
Serial.print("myStringPassword = ");
Serial.println(myStringSSID);
delay(1000);
}
Sketch to Retreive with eeprom.get()
#include <EEPROM.h>
char myCharPassword[20] = "";
char myCharSSID[20] = "";
String myStringPassword;
String myStringSSID;
int addr = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("esp8266 String get from eeprom example");
EEPROM.begin(512);
EEPROM.get(addr, myCharPassword);
EEPROM.get(addr + 50, myCharSSID);
Serial.print("myCharPassword = ");
Serial.println(myCharPassword);
Serial.print("myCharSSID = ");
Serial.println(myCharSSID);
myStringPassword = myCharPassword;
myStringSSID = myCharSSID;
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("myStringPassword = ");
Serial.println(myStringPassword);
Serial.print("myStringPassword = ");
Serial.println(myStringSSID);
}
void loop()
{
Serial.print("myStringPassword = ");
Serial.println(myStringPassword);
Serial.print("myStringPassword = ");
Serial.println(myStringSSID);
delay(1000);
}
1 Like
Thanks a lot cattledog for soo much help, but its not working out for me, no matter whatever i try, i am always getting the output from EEPROM as 0.

Please post an MCVE which is a minimal reproducible example for your issue.
Can you run the basic EEPROM library example on your NODEMCU? Can you store and retrieve anything? Can you successfully run the code I provided?
hey cattle dog, yes i can run a basic eeprom code of storing HIGH or LOW values in eeprom, in fact i am using the same code in the current nodemcu along with the wifi one and its working perfectly fine, but i dont know why the code you provided is not working, i was pretty positive about that code that it'll work but it didnt 
i am providing the wifi manager library cpp code extract below, these two commands are used to retrive SSID and PASSWORD from the library, please suggest any change in this code so that this command helps me with the EEPROM function too
String WiFiManager::getSSID() {
if (_ssid == "") {
DEBUG_WM(F("Reading SSID"));
_ssid = WiFi.SSID();
DEBUG_WM(F("SSID: "));
DEBUG_WM(_ssid);
}
return _ssid;
}
String WiFiManager::getPassword() {
if (_pass == "") {
DEBUG_WM(F("Reading Password"));
_pass = WiFi.psk();
DEBUG_WM("Password: ");
DEBUG_WM(_pass);
}
return _pass;
}
and this is how i retrive it in IDE
String WIFI_SSID = wifiManager.getSSID();
String WIFI_PASS = wifiManager.getPassword();
String WIFI_SSID = wifiManager.getSSID();
String WIFI_PASS = wifiManager.getPassword();
Without trying to store or retreive
What do you see when you print out WIFI_SSID and WIFI_PASS?
What do you see when you print out WIFI_SSID.c_str() and WIFI_PASS.c_str()?
What do you see when you print out the character arrays after the strcpy() opertion?
WIFI_SSID and WIFI_PASS are local variables, they are not defined in library, so if i don't declare them anywhere in code, it will just give not declared in this scope error
The library functions return a String. You have assigned the return values to two local variables typed as Strings. I'm not certain I see what you are concerned about.
If you think there is an issue with the variables being local, then make them global until you work out the issue.
What prevents you from printing them out to confirm that you have what you think you have? That is what I asked for in my previous post.
Are you doing the transfer to character arrays within the scope where WIFI_SSID and WIFI_PASS have the values from the library?
If you can store and retrieve "Hello World" to the eeprom using .put() and .get() you should be able to do the same with the character arrays created from the function returns.
one general question, does EEPROM storage value have to do something with this? like you have define EEPROM.begin(512), whereas i have define EEPROM.begin(1) in my code since beginning, this value was working just fine for reading and writing 4 values that i was using for my last relay states, so i never changed it, does it have something to do with all this?? do i need to change to a higher value??
i have define EEPROM.begin(1) in my code since beginning, this value was working just fine for reading and writing 4 values that i was using for my last relay states, so i never changed it, does it have something to do with all this?? do i need to change to a higher value??
Absolutely. There is a default minimum of 4, which is why your EEPROM.begin(1) worked for your 4 values.
You have been wasting your and my time by not paying attention to fundamentals. This is why we ask to see running code which demonstrates the issue, and not snippets of code.
Sorry cattledog, for wasting your precious time, but i didn't knew it coz i am a newbie in arduino coding and some shitty youtube tutorial taught me that 1 stands for 1 MB of EEPROM storage which would be enough for storing upto 1024 values. I am extremely sorry for this, and thanks for your help mate, you helped me alot. I'll update the final outcome soon. Also can you just give me a last help, i want to use an if else condition that checks that any ssid is stored in EEPROM or not.
Cheers,
Parth Kharia
Also can you just give me a last help, i want to use an if else condition that checks that any ssid is stored in EEPROM or not.
A simple method is use a separate eeprom address to store a value (0 or 1 is easy enough) which tells you if the ssid has been stored or not.
if(EEPROM.read(addr) == 1)
{
//ssid has been stored
//do what needs to be done if ssid is stored
//perhaps set a flag to do something later in the program
}
else
{
//ssid has not been stored
// do what needs to be done if ssid not stored
//perhaps set a flag to do something later in the program
}