Go Down

Topic: Saving String Variable In EEPROM Of NODEMCU (Read 167 times) previous topic - next topic

Parth_Kharia

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.
Code: [Select]
String GETSSID = wifiManager.getSSID();
String GETPASS = wifiManager.getPassword();

Peter-CAD-HST

Hello Parth_Kharia
How are you?
Take a look here.
br Peter
and stay healthy
KISS Q&D - keep calm and carry on - mind the gap - beware of sharks! -

Parth_Kharia

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??

Peter-CAD-HST

Hello  Parth_Kharia
did you study, check and ran the examples from the tutorial for  EEPROM.get and EEPROM.put?
wbr Peter
and stay healty
KISS Q&D - keep calm and carry on - mind the gap - beware of sharks! -

cattledog

Code: [Select]
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.

Parth_Kharia

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. 

cattledog

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()
Code: [Select]

#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()
Code: [Select]

#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);
}


TheUNOGuy

It's better if you check the internet  ( your friend ) before posting..! :D
#Learn how to use the FORUM - https://forum.arduino.cc/index.php?topic=148850.0

Parth_Kharia

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. :( :(

cattledog

#9
Today at 07:29 am Last Edit: Today at 07:30 am by cattledog
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?

Peter-CAD-HST

The evil of Arduino Strings...
Hello TheUNOGuy
Many thanks for the forwarding of the link. KARMA+
wbr Peter
and stay healthy
KISS Q&D - keep calm and carry on - mind the gap - beware of sharks! -

Parth_Kharia

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
Code: [Select]
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
Code: [Select]
String WIFI_SSID = wifiManager.getSSID();
String WIFI_PASS = wifiManager.getPassword();




cattledog

Code: [Select]
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?

Go Up