Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By RIN67630
#96425 I am using a WiFi connection code since ages and it used to reconnect without problem.
Now i have added a new Wemos moduel and it does not load the credentials from eeprom:
It should try from EEPROM else use the Sketch's credentials, else use smart config.
it connects on the sketch's credentials, but shouldn't it connect from EEPROM next time?
do I miss a code to wirtie the credentials to EEPROM?
Thank you for helping.
Here is my code:
Code: Select allvoid getWiFi()              // From Memory , Using Defaults, or using SmartConfig
{
  int retry = 0;
  WiFi.mode(WIFI_STA);      // configure WiFi in Station Mode
  wifi_station_set_auto_connect(true);
  wifi_station_set_hostname(HOST_NAME);
  delay(wifiRepeatInterval);
  Console3.println("Attempt to connect to WiFi network from EEPROM");
  WiFi.begin();
  delay(wifiRepeatInterval);
  while (WiFi.status() != WL_CONNECTED)
  {
    Console3.print(".");
    digitalWrite(STDLED, not digitalRead(STDLED));
    delay(wifiRepeatInterval) ;
    if (retry++ >= wifiMaxTries) break;
  }

  if (WiFi.status() != WL_CONNECTED)
  {
    Console3.println("\nConnection timeout expired! Start with default");
    retry = 0;
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    delay(wifiRepeatInterval * 2 );
    while (WiFi.status() != WL_CONNECTED)
    {
      Console3.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(wifiRepeatInterval);
      if (retry++ >= wifiMaxTries) break;
    }
  }
  if (WiFi.status() != WL_CONNECTED)
  {
    Console3.println("Connection timeout expired! Start SmartConfig…");
    retry = 0;
    WiFi.beginSmartConfig();
    digitalWrite(STDLED, false);
    while (WiFi.status() != WL_CONNECTED)
    {
      Console3.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(wifiRepeatInterval * 4);
      if (retry++ >= wifiMaxTries) break;
      if (WiFi.smartConfigDone())
      {
        Console3.println("SmartConfig success!");
        break; // exit from loop
      }
    }
  }
User avatar
By rooppoorali
#96452 it seems that you are attempting to connect to a WiFi network using credentials stored in the EEPROM. However, it appears that you are missing the code to write the credentials to the EEPROM.

In order to store the WiFi credentials in the EEPROM, you need to add code to write the credentials to the appropriate memory addresses in the EEPROM. Here's an example of how you can do that:

Code: Select all      #include <EEPROM.h>

// Define the addresses in the EEPROM where the credentials will be stored
#define SSID_ADDRESS 0
#define PASS_ADDRESS 32

void saveCredentialsToEEPROM(const char* ssid, const char* password) {
  // Write the SSID to EEPROM
  for (int i = 0; i < strlen(ssid); i++) {
    EEPROM.write(SSID_ADDRESS + i, ssid[i]);
  }
  EEPROM.write(SSID_ADDRESS + strlen(ssid), '\0'); // Null-terminate the SSID string

  // Write the password to EEPROM
  for (int i = 0; i < strlen(password); i++) {
    EEPROM.write(PASS_ADDRESS + i, password[i]);
  }
  EEPROM.write(PASS_ADDRESS + strlen(password), '\0'); // Null-terminate the password string

  // Commit the changes to the EEPROM
  EEPROM.commit();
}

void getWiFi() {
  // ...

  // Attempt to connect using the credentials from EEPROM
  char ssid[32];
  char password[64];
  memset(ssid, 0, sizeof(ssid));
  memset(password, 0, sizeof(password));
 
  for (int i = 0; i < sizeof(ssid); i++) {
    ssid[i] = EEPROM.read(SSID_ADDRESS + i);
    if (ssid[i] == '\0') {
      break;
    }
  }

  for (int i = 0; i < sizeof(password); i++) {
    password[i] = EEPROM.read(PASS_ADDRESS + i);
    if (password[i] == '\0') {
      break;
    }
  }

  if (strlen(ssid) > 0 && strlen(password) > 0) {
    Console3.println("Connecting to WiFi network from EEPROM");
    WiFi.begin(ssid, password);
    // ...
  } else {
    // ...
  }

  // ...
}
     



Make sure to include the EEPROM.h library at the beginning of your sketch for the EEPROM functions to work.

However, I don't know if you are using a ready-made Wimos board or a customized one. If you are using one that you designed, you can share your design here and ask for a review:

https://www.pcbway.com/project/question