Read data from EEPROM problem

Hi,I’m implementing data writing&reading with EEPROM of NodeMCU to save Wifi’s SSID and password,

I Power On NodeMCU Serial print infomation below:

EEPROM data is empty,can not get value,need SmartConfig!

Wait for SmartConfig
.......

And when i do the smart config i get:

SSID:Superbox_Wifi_6117
PSW:6j6VAF6J7j
WiFi config saved to EEPROM SSID=Superbox_Wifi_6117,password=6j6VAF6J7j
IP address: 192.168.1.140

So everything seems working but when i unplug and plug the nodemcu i get:

Connect WiFi Timeout ,need to ReConfig!

So it seems like it doesnt store the date on eprom could you please tell me what i am doing wrong?


#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>

int addr = 0; 
int val =0;  //val=1 mean  EPROM saved data , val=0 mean EPROM is empty
String wifi;
String pwd;
Ticker timer0;  
WiFiClient espClient;

struct CONFIG
{
   char ssid[32];
   char pass[32];
};
 
CONFIG buf;

void ResetFlag()
{
   EEPROM.begin(100);
   EEPROM.write(addr,0);     
   EEPROM.end();
}

void SetFlag()
{
   EEPROM.begin(100);
   EEPROM.write(addr,1);     
   EEPROM.end();
}

void ReConfig()
{
   ResetFlag();
   Serial.println("Connect WiFi Timeout ,need to ReConfig!");
   
}
void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for SmartConfig");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    digitalWrite(LED_BUILTIN, 0);
    delay(200);
    digitalWrite(LED_BUILTIN, 1);
    delay(200);
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      wifi=WiFi.SSID();
      pwd =WiFi.psk();
      saveConfig();

      Serial.print("WiFi config saved to EEPROM SSID=");
      Serial.print (buf.ssid);
      Serial.print(",password=");
      Serial.println(buf.pass);

      SetFlag();
      
      delay(5000);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      break;
    }
  }
}

void saveConfig()  /*save config to eeprom */
{
  EEPROM.begin(512);
  strcpy(buf.ssid, WiFi.SSID().c_str());
  strcpy(buf.pass, WiFi.psk().c_str());
  EEPROM.put<CONFIG>(0, buf);
  EEPROM.commit();
}

void loadConfig()  /* load config from eeprom */
{
  EEPROM.begin(512);
  EEPROM.get<CONFIG>(0, buf);
}

void get_eeprom()
{
  EEPROM.begin(100);
  val = EEPROM.read(addr);
  if (val==0) 
  { 
    Serial.println("EEPROM data is empty,can not get value,need SmartConfig!");
    smartConfig();
  }
  else 
  {
    Serial.println("Connect to Wifi");
    loadConfig();
    Serial.print("ssid=");Serial.println(buf.ssid);
    Serial.print("password=");Serial.println(buf.pass);
    WiFi.mode(WIFI_STA);
    WiFi.begin(buf.ssid,buf.pass);
    timer0.attach(10,ReConfig);
    while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    }
}

void setup()
{
  Serial.begin(115200);
  if(!Serial){;}
  delay(3000);
  Serial.println("ESP8266 Module Starting....");
  get_eeprom();
}

void loop()  
{
}

test.ino (2.6 KB)

Hello

timer0.attach(10,ReConfig);
while (WiFi.status() != WL_CONNECTED) 
{
    delay(500);
    Serial.print(".");
}

You never stop this timer0 so ReConfig will be called even when Wifi has connected

I suggest that you don't use this Ticker thing, instead increase a counter inside the while loop and call ReConfig when this counter reach a value like 20

Thank you for your reply but when i do this it still wont connect the wifi it will just slow down the serail print and connection attemp. Am i wrong?

I don't really understand what you are trying to do, the smartConfig will already store SSID and password in EEPROM, you don't need to do that manually.

yes i want to store them on eprom so when i unplug and plug the nodemcu i want it to connect to same wifi back again

  • Try connect to wifi
  • If it doesn't connect after a timeout of say 10 seconds, start smartConfig

That's all you have to do. Once smartConfig is done it will save SSID and password for later connections

Thank you for your reply

i did that but it doesnt connect back that is the problem i am trying to solve.

I've just tried, it worked for me

See this quick example

#include <ESP8266WiFi.h>

void setup()
{
    Serial.begin( 115200 );
    WiFi.mode( WIFI_STA );
    WiFi.begin();

    while(WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");

        static uint8_t counter = 0;
        if( ++counter == 20 )
        {
            Serial.println("Timeout\nStarting SmartConfig");
            WiFi.beginSmartConfig();

            while( !WiFi.smartConfigDone() )
            {
                delay(100);
            }

            Serial.println("SmartConfig done");
            break;
        }
    }

    Serial.println( "Should be connected to WiFi" );
}

void loop()
{
}

Thank you. Now it is working as i intented but i still dont understand where i went wrong. Was it the implementation of the ticker?