Autoreconnect not working after connection with credentials, but..

rin67630
Posts: 141
Joined: Sun Mar 11, 2018 5:13 pm

Autoreconnect not working after connection with credentials, but..

Postby rin67630 » Wed Apr 24, 2024 5:22 pm

... it works after Smart Config !

Hi,

I am trying to build a 3 step strategy to connect an ESP8266 to WiFi.
a) it tries an autoconnection with
WiFi.begin();
WiFi.waitForConnectResult();
b) if the autoconnection fails, it tries from the sketch's credentials
WiFi.begin(WIFI_SSID, WIFI_PASS);
c) if that fails too, it attempts a Smart Config;
WiFi.beginSmartConfig();

After step b) was succesful and a reset occured the autoconnection a) never worked and it always defaulted to step b).

But if I entered wrong credentials for step b) and got a connection over c) Smart Config then after a reboot the
autoconnection a) got successful.

So something is missing in the procedure b) to store the credentials, which is done automatically in Smart Config?
Does someone know what?

Thank you for any clue.
Regards
Laszlo

Here is my code:

Code: Select all

void getWiFi()
{
  Serial.printf("Reconnecting to %s\n", WIFI_SSID);

  byte retry;
  WiFi.begin();
  WiFi.waitForConnectResult();

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected");
  } else {
    Serial.println("Fail, try Credentials\n");
    retry = 0;
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    delay(WIFI_REPEAT * 2 );
    while (WiFi.status() != WL_CONNECTED)
    {
      Serial.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(WIFI_REPEAT);
      if (retry++ >= WIFI_MAXTRIES) break;
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.setAutoReconnect(1);
    WiFi.persistent(1);
  } else {
    Serial.println("Fail, try SmartConfig\n");
    retry = 0;
    WiFi.beginSmartConfig();
    digitalWrite(STDLED, false);
    while (WiFi.status() != WL_CONNECTED)
    {
      Serial.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(WIFI_REPEAT * 10);
      if (retry++ >= WIFI_MAXTRIES) break;
      if (WiFi.smartConfigDone())
      {
        Serial.println("SmartConfig success!");
        break; // exit from loop
      }
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.setAutoReconnect(1);
    WiFi.persistent(1);
    Serial.println("Connection OK!");
    // WiFi.printDiag(Serial);
  } else {
    Serial.println("giving up! no Network");
    WiFi.mode(WIFI_OFF);
  }

#ifdef ARDUINO_ARCH_ESP8266
  WiFi.setOutputPower(WIFI_POWER);         // 0..20
#else
  WiFi.setTxPower(byte(WIFI_POWER));       // 0..78
#endif
  ip = WiFi.localIP();
  Serial.print("Done: RRSI= ");  Serial.print(WiFi.RSSI()); Serial.print("dB");
  sprintf(charbuff, "dB, IP= %03d.%03d.%03d.%03d\n", ip[0], ip[1], ip[2], ip[3]);   Serial.printf(charbuff);
}//end getWiFi

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: Autoreconnect not working after connection with credentials, but..

Postby liaifat85 » Wed Apr 24, 2024 5:54 pm

you can modify your code to include the WiFi.persistent(true) function call after setting the Wi-Fi credentials:

Code: Select all

void getWiFi()
{
  Serial.printf("Reconnecting to %s\n", WIFI_SSID);

  byte retry;
  WiFi.begin();
  WiFi.waitForConnectResult();

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected");
  } else {
    Serial.println("Fail, try Credentials\n");
    retry = 0;
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    delay(WIFI_REPEAT * 2 );
    while (WiFi.status() != WL_CONNECTED)
    {
      Serial.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(WIFI_REPEAT);
      if (retry++ >= WIFI_MAXTRIES) break;
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.setAutoReconnect(1);
    WiFi.persistent(true); // Save credentials to flash memory
  } else {
    Serial.println("Fail, try SmartConfig\n");
    retry = 0;
    WiFi.beginSmartConfig();
    digitalWrite(STDLED, false);
    while (WiFi.status() != WL_CONNECTED)
    {
      Serial.print(".");
      digitalWrite(STDLED, not digitalRead(STDLED));
      delay(WIFI_REPEAT * 10);
      if (retry++ >= WIFI_MAXTRIES) break;
      if (WiFi.smartConfigDone())
      {
        Serial.println("SmartConfig success!");
        break; // exit from loop
      }
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.setAutoReconnect(1);
    WiFi.persistent(true); // Save credentials to flash memory
    Serial.println("Connection OK!");
    // WiFi.printDiag(Serial);
  } else {
    Serial.println("giving up! no Network");
    WiFi.mode(WIFI_OFF);
  }

#ifdef ARDUINO_ARCH_ESP8266
  WiFi.setOutputPower(WIFI_POWER);         // 0..20
#else
  WiFi.setTxPower(byte(WIFI_POWER));       // 0..78
#endif
  ip = WiFi.localIP();
  Serial.print("Done: RRSI= ");  Serial.print(WiFi.RSSI()); Serial.print("dB");
  sprintf(charbuff, "dB, IP= %03d.%03d.%03d.%03d\n", ip[0], ip[1], ip[2], ip[3]);   Serial.printf(charbuff);
}//end getWiFi

rin67630
Posts: 141
Joined: Sun Mar 11, 2018 5:13 pm

Re: Autoreconnect not working after connection with credentials, but..

Postby rin67630 » Wed Apr 24, 2024 6:58 pm

liaifat85 wrote:
Wed Apr 24, 2024 5:54 pm
you can modify your code to include the WiFi.persistent(true) function call after setting the Wi-Fi credentials:
Thank you for your advice.

But it's already there:
WiFi.setAutoReconnect(1);
WiFi.persistent(1); // Save credentials to flash memory
It does not make any differnce if you write AutoReconnect(1); or AutoReconnect(true);

To be sure: I have tried both: exactly the same behaviour.
Regards

Laszlo

Who is online

Users browsing this forum: No registered users and 17 guests