[GH-ISSUE #424] Using autoconnect to connect to WIFI, then startConfigPortal, fails to connect/save new credentials #355

Closed
opened 2026-02-28 01:24:55 +03:00 by kerem · 6 comments
Owner

Originally created by @Jsolo1 on GitHub (Sep 15, 2017).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/424

Hi, the idea is the following, by default I'm using Autoconnect to connect to a saved wifi network, but I also want to bring up the Configuration Portal by pressing a button.
It connects to wifi fine, it brings up the portal fine in case I press the button, but if I select a new wifi SSID and password, it seems to ignore it, It doesn't connect to it and it doesn't save the credentials.

From the code it never resets even if it hits the timeout.

Any idea? Thanks

setup(){
wifiManager.setConfigPortalTimeout(180);
wifiManager.autoConnect("WIFI", "password");
}
loop{
 if (portal == 1) {
    if (!wifiManager.startConfigPortal("WIFI", "password")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);
    }
    else{
      Serial.println("exit with positive value");
      WiFi.mode(WIFI_STA);
    }
    portal = 0;
  }
  
}
Originally created by @Jsolo1 on GitHub (Sep 15, 2017). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/424 Hi, the idea is the following, by default I'm using Autoconnect to connect to a saved wifi network, but I also want to bring up the Configuration Portal by pressing a button. It connects to wifi fine, it brings up the portal fine in case I press the button, but if I select a new wifi SSID and password, it seems to ignore it, It doesn't connect to it and it doesn't save the credentials. From the code it never resets even if it hits the timeout. Any idea? Thanks ``` setup(){ wifiManager.setConfigPortalTimeout(180); wifiManager.autoConnect("WIFI", "password"); } loop{ if (portal == 1) { if (!wifiManager.startConfigPortal("WIFI", "password")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } else{ Serial.println("exit with positive value"); WiFi.mode(WIFI_STA); } portal = 0; } } ```
kerem closed this issue 2026-02-28 01:24:55 +03:00
Author
Owner

@tablatronix commented on GitHub (Sep 15, 2017):

Did you enable debug and watch serial monitor when saving?

<!-- gh-comment-id:329765649 --> @tablatronix commented on GitHub (Sep 15, 2017): Did you enable debug and watch serial monitor when saving?
Author
Owner

@Jsolo1 commented on GitHub (Sep 15, 2017):

I think I found what the problem is.
In the function :


int WiFiManager::connectWifi(String ssid, String pass) {
  DEBUG_WM(F("Connecting as wifi client..."));
  DEBUG_WM(WiFi.SSID());

  // check if we've got static_ip settings, if we do, use those.
  if (_sta_static_ip) {
    DEBUG_WM(F("Custom STA IP/GW/Subnet"));
    WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
    DEBUG_WM(WiFi.localIP());
  }
  //fix for auto connect racing issue
  if (WiFi.status() == WL_CONNECTED) {
    DEBUG_WM("Already connected. Bailing out.");
    return WL_CONNECTED;
  }
  //check if we have ssid and pass and force those, if not, try with last saved values
  if (ssid != "") {
	//WiFi.persistent(false);
	//WiFi.disconnect();
	//WiFi.persistent(true);
    WiFi.begin(ssid.c_str(), pass.c_str());
  } else {
    if (WiFi.SSID()) {
      DEBUG_WM("Using last saved values, should be faster");
      //trying to fix connection in progress hanging
      ETS_UART_INTR_DISABLE();
      wifi_station_disconnect();
      ETS_UART_INTR_ENABLE();

      WiFi.begin();
    } else {
      DEBUG_WM("No saved credentials");
    }
  }

  int connRes = waitForConnectResult();
  DEBUG_WM ("Connection result: ");
  DEBUG_WM ( connRes );
  //not connected, WPS enabled, no pass - first attempt
  if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
    startWPS();
    //should be connected at the end of WPS
    connRes = waitForConnectResult();
  }
  return connRes;
}

If it's already connected, it doesn't try to connect again. Even commenting the lines:

 if (WiFi.status() == WL_CONNECTED) {
    DEBUG_WM("Already connected. Bailing out.");
    return WL_CONNECTED;
  }

Still doesn't work because WiFi.begin() won't work if there is already a connection made.
So the only solution would be to disconnect the Wifi (without deleting the credetials) and then try to connect again.

Maybe something like:

WiFi.disconnect(false);

Or

WiFi.persistent(false);
WiFi.disconnect();
WiFi.persistent(true);

Would do the trick?

<!-- gh-comment-id:329769518 --> @Jsolo1 commented on GitHub (Sep 15, 2017): I think I found what the problem is. In the function : ``` int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(F("Connecting as wifi client...")); DEBUG_WM(WiFi.SSID()); // check if we've got static_ip settings, if we do, use those. if (_sta_static_ip) { DEBUG_WM(F("Custom STA IP/GW/Subnet")); WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); DEBUG_WM(WiFi.localIP()); } //fix for auto connect racing issue if (WiFi.status() == WL_CONNECTED) { DEBUG_WM("Already connected. Bailing out."); return WL_CONNECTED; } //check if we have ssid and pass and force those, if not, try with last saved values if (ssid != "") { //WiFi.persistent(false); //WiFi.disconnect(); //WiFi.persistent(true); WiFi.begin(ssid.c_str(), pass.c_str()); } else { if (WiFi.SSID()) { DEBUG_WM("Using last saved values, should be faster"); //trying to fix connection in progress hanging ETS_UART_INTR_DISABLE(); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); WiFi.begin(); } else { DEBUG_WM("No saved credentials"); } } int connRes = waitForConnectResult(); DEBUG_WM ("Connection result: "); DEBUG_WM ( connRes ); //not connected, WPS enabled, no pass - first attempt if (_tryWPS && connRes != WL_CONNECTED && pass == "") { startWPS(); //should be connected at the end of WPS connRes = waitForConnectResult(); } return connRes; } ``` If it's already connected, it doesn't try to connect again. Even commenting the lines: ``` if (WiFi.status() == WL_CONNECTED) { DEBUG_WM("Already connected. Bailing out."); return WL_CONNECTED; } ``` Still doesn't work because WiFi.begin() won't work if there is already a connection made. So the only solution would be to disconnect the Wifi (without deleting the credetials) and then try to connect again. Maybe something like: `WiFi.disconnect(false);` Or ``` WiFi.persistent(false); WiFi.disconnect(); WiFi.persistent(true); ``` Would do the trick?
Author
Owner

@Jsolo1 commented on GitHub (Sep 15, 2017):

Yes, it works! This is the code:

int WiFiManager::connectWifi(String ssid, String pass) {
  DEBUG_WM(F("Connecting as wifi client..."));
  DEBUG_WM(WiFi.SSID());

  // check if we've got static_ip settings, if we do, use those.
  if (_sta_static_ip) {
    DEBUG_WM(F("Custom STA IP/GW/Subnet"));
    WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
    DEBUG_WM(WiFi.localIP());
  }
  //fix for auto connect racing issue
  //if (WiFi.status() == WL_CONNECTED) {
  //  DEBUG_WM("Already connected. Bailing out.");
  //  return WL_CONNECTED;
  //}
  //check if we have ssid and pass and force those, if not, try with last saved values
  if (ssid != "") {
	WiFi.persistent(false);
	WiFi.disconnect();
	WiFi.persistent(true);
    WiFi.begin(ssid.c_str(), pass.c_str());
  } else {
    if (WiFi.SSID()) {
      DEBUG_WM("Using last saved values, should be faster");
      //trying to fix connection in progress hanging
      ETS_UART_INTR_DISABLE();
      wifi_station_disconnect();
      ETS_UART_INTR_ENABLE();

      WiFi.begin();
    } else {
      DEBUG_WM("No saved credentials");
    }
  }

  int connRes = waitForConnectResult();
  DEBUG_WM ("Connection result: ");
  DEBUG_WM ( connRes );
  //not connected, WPS enabled, no pass - first attempt
  if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
    startWPS();
    //should be connected at the end of WPS
    connRes = waitForConnectResult();
  }
  return connRes;
}

The only problem are those commented lines to "fix for auto connect racing issue" Any idea what do they do?
Thanks

<!-- gh-comment-id:329774506 --> @Jsolo1 commented on GitHub (Sep 15, 2017): Yes, it works! This is the code: ``` int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(F("Connecting as wifi client...")); DEBUG_WM(WiFi.SSID()); // check if we've got static_ip settings, if we do, use those. if (_sta_static_ip) { DEBUG_WM(F("Custom STA IP/GW/Subnet")); WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); DEBUG_WM(WiFi.localIP()); } //fix for auto connect racing issue //if (WiFi.status() == WL_CONNECTED) { // DEBUG_WM("Already connected. Bailing out."); // return WL_CONNECTED; //} //check if we have ssid and pass and force those, if not, try with last saved values if (ssid != "") { WiFi.persistent(false); WiFi.disconnect(); WiFi.persistent(true); WiFi.begin(ssid.c_str(), pass.c_str()); } else { if (WiFi.SSID()) { DEBUG_WM("Using last saved values, should be faster"); //trying to fix connection in progress hanging ETS_UART_INTR_DISABLE(); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); WiFi.begin(); } else { DEBUG_WM("No saved credentials"); } } int connRes = waitForConnectResult(); DEBUG_WM ("Connection result: "); DEBUG_WM ( connRes ); //not connected, WPS enabled, no pass - first attempt if (_tryWPS && connRes != WL_CONNECTED && pass == "") { startWPS(); //should be connected at the end of WPS connRes = waitForConnectResult(); } return connRes; } ``` The only problem are those commented lines to "fix for auto connect racing issue" Any idea what do they do? Thanks
Author
Owner

@tablatronix commented on GitHub (Sep 15, 2017):

Yeah thats a known issue, already fixed in next release.

I have no idea what those lines are for, either, I think it is to handle a race condition when the esp is already connecting internally. I think there are some bugs in the older sdk that say do not call begin when already connected, ir it can crash, so they disconnect first, this just bails.

#407

<!-- gh-comment-id:329776263 --> @tablatronix commented on GitHub (Sep 15, 2017): Yeah thats a known issue, already fixed in next release. I have no idea what those lines are for, either, I think it is to handle a race condition when the esp is already connecting internally. I think there are some bugs in the older sdk that say do not call begin when already connected, ir it can crash, so they disconnect first, this just bails. #407
Author
Owner

@Jsolo1 commented on GitHub (Sep 20, 2017):

Thank you

<!-- gh-comment-id:330821838 --> @Jsolo1 commented on GitHub (Sep 20, 2017): Thank you
Author
Owner

@tablatronix commented on GitHub (Sep 20, 2017):

I think a workaround, would be to disconnect in your own code beforehand. Not sure if we can hotfix this or not

<!-- gh-comment-id:330846288 --> @tablatronix commented on GitHub (Sep 20, 2017): I think a workaround, would be to disconnect in your own code beforehand. Not sure if we can hotfix this or not
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#355
No description provided.