Wifi manager, how to persistently reconnect to known SSID?

Good morning.

I am using WIFI manager to spin up a AP for me to fill in my wifi credentials for home. I understand that if it fails to connect then it spins up the AP again which is fine.

If it connects to my home wifi successfully i want it to connect and retry forever until i reset the wifi settings myself with a button. It seems to just try and connect and then create the AP again...

Here is my code:

void setup() {
  Serial.begin(9600);

  bool spiffsSetup = loadConfigFile();
  if (!spiffsSetup)
  {
    Serial.println(F("Forcing config mode as there is no saved config"));
    forceConfig = true;
  }

  setupWifiManager();
}
void loop() {
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    ESP.restart();
    delay(500);
  }

  reconnect_mqtt();
  client.loop();
}
void setupWifiManager()
{
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 20);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 10);
  WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 50);
  WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, 50);

  // WiFiManager
  // Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //add all your parameters here
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_mqtt_user);
  wifiManager.addParameter(&custom_mqtt_pass);

  //  if (forceConfig)
  //    // Run if we need a configuration
  //  {
  //    if (!wifiManager.startConfigPortal("AP"))
  //    {
  //      Serial.println("failed to connect and hit timeout");
  //      delay(3000);
  //      //reset and try again, or maybe put it to deep sleep
  //      ESP.restart();
  //      delay(5000);
  //    }
  //  }
  //  else
  //  {
  //    if (!wifiManager.autoConnect("AP"))
  //    {
  //      Serial.println("failed to connect and hit timeout");
  //      delay(3000);
  //      // if we still have not connected restart and try all over again
  //      ESP.restart();
  //      delay(5000);
  //    }
  //  }

  //erases the wifi settings from chip. Going to add this to mqtt command and button press.
  //    wifiManager.resetSettings();

  //TEMP AP SSID
  //  wifiManager.autoConnect("AP");

  WiFi.forceSleepWake();
  WiFi.setSleepMode(WIFI_NONE_SLEEP);

  //  if (!(wifiManager.autoConnect("AP"))) // check if wifi is connected within configportaltimeout if not restart
  //  {
  //    Serial.println("Failed to connect");
  //    WiFi.begin();
  //    ESP.restart();
  //    delay(5000);
  //  }
  wifiManager.autoConnect("AP");

  //  if (!wifiManager.autoConnect("AP")) {
  //    Serial.println("failed to connect and hit timeout");
  //    delay(3000);
  //    //reset and try again, or maybe put it to deep sleep
  //    ESP.restart();
  //    delay(5000);
  //  }

  //  wifiManager.setConnectTimeout(180);
  //
  //  wifiManager.setTimeout(180);
  //
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    ESP.restart();
    delay(500);
  }


  // if you get here you have connected to the WiFi
  Serial.println("Connected.");

  strncpy(mqtt_server, custom_mqtt_server.getValue(), sizeof(mqtt_server));
  strncpy(mqtt_port, custom_mqtt_port.getValue(), sizeof(mqtt_port));
  strncpy(mqtt_user, custom_mqtt_user.getValue(), sizeof(mqtt_user));
  strncpy(mqtt_pass, custom_mqtt_pass.getValue(), sizeof(mqtt_pass));

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    saveConfigFile();
  }
  //  server.begin();
}
1 Like

I got the answer i was looking for for all those that need this:

  wifiManager.setConnectTimeout(180);
  wifiManager.setConnectRetries(100);
  if (wifiManager.getWiFiIsSaved()) wifiManager.setEnableConfigPortal(false);
  wifiManager.autoConnect("AP");

This will keep retrying previously connected SSID.

1 Like

Hi @jackkitley this is also what I am looking to do, thanks for posting the solution, although I am confused where I should put the code. Are you able to post where it should go? Thanks

Hey @ajtaylor79 , I was working on an ESP home automation and was trying to figure out how to reconnect to same wifi network.
After days of trying everything I came across this thread and figured out where the above mentioned part of the code is to be placed.

Just take a look at my code and you might figure it out yourself!

also really thanks for the code snippet @jackkitley
it helped me a lot!

@ajtaylor79 Hey dude I also found another solution, Where my code uses WIFI MANAGER to initialize the wifi networks.

But if the wifi is connected and disconnected, my code tries to reconnect every 30secs (reconnection time can be changed).

Apparently when the wifi manager class is declared in a scope, the WiFi.begin() function isn't working idk whether it is to me or not.

the getWiFiSSID() method present in WiFiManager spits out the previously saved wifi SSID and if there is no SSID stored, it returns garbage value.



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.