[GH-ISSUE #1213] Can't reconnect with waitForConnectResult after disconnect #1035

Open
opened 2026-02-28 01:28:13 +03:00 by kerem · 11 comments
Owner

Originally created by @Primus007 on GitHub (Feb 15, 2021).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1213

Basic Infos

Arduino IDE 1.8.13

Hardware

WiFimanager Branch/Release:

  • Master
  • [ x] Development

Esp8266/Esp32:

  • [ x] ESP8266
  • ESP32

Hardware: ESP-12e, esp01, esp25

  • ESP01
  • [ x] ESP12 E/F/S (nodemcu, wemos, feather)
  • Other

ESP Core Version: 2.4.0, staging

  • 2.3.0
  • 2.4.0
  • v2.7.4
  • staging (master/dev)

Description

I use your WifiManager to connect to my home Network. It works fine. But after i get some infos from the internet i disconnect the wifi in my Code. it should reconnect if i press a button. but if i do that, always the AP Config mode fires up and i get the error: [ERROR] wifi begin failed

Settings in IDE

Module: Wemos D1 & R2 mini

Additional libraries:

Sketch

#include <WiFiManager.h>

WiFiManager wm;

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.println("");
  WiFi.mode(WIFI_STA); //  Station Mode 
  Serial.print("try to connect to wifi ");
  Serial.println("SSID: " + WiFi.SSID());
  startWiFi(); // Connect
  delay(2000);
  wm.disconnect();
  Serial.println("Wifi Disconnected");
  Serial.println("");
  delay(2000);
  Serial.print("Try to RECONNECT to wifi ");
  Serial.println("SSID: " + WiFi.SSID());
  startWiFi(); // Connect
}

void loop() {
//
}


// -------------------------------------------------------------------------
//                               startWiFi
// -------------------------------------------------------------------------
   //for reference:
    //typedef enum {
    //    WL_NO_SHIELD = 255,   // for compatibility with WiFi Shield library
    //    WL_IDLE_STATUS = 0,
    //    WL_NO_SSID_AVAIL = 1,
    //    WL_SCAN_COMPLETED = 2,
    //    WL_CONNECTED = 3,
    //    WL_CONNECT_FAILED = 4,
    //    WL_CONNECTION_LOST = 5,
    //    WL_DISCONNECTED = 6
    //} wl_status_t;
void startWiFi() {
    Serial.print("Is WiFi Configured and saved: ");
    Serial.println((String)wm.getWiFiIsSaved() ? "YES" : "NO");

    if (!WiFi.isConnected()) { 
        Serial.println("SSID: " + WiFi.SSID());
        int conn_result = WiFi.waitForConnectResult(10000); // connect try it for 10 seconds
        Serial.print("conn_result: ");
        Serial.println(conn_result, DEC);
    }
    if (WiFi.isConnected()) {
        Serial.println("Wifi is connected!");
    }
    else {
        Serial.println("Wifi is NOT connected!");
    }
    
    Serial.println("");
}

The serial result:
First connect:
conn_result: 3
Wifi is connected!

Wifi Disconnected

Try to RECONNECT to wifi SSID: InProg1
Is WiFi Configured and saved: YES
SSID: InProg1
conn_result: 0
Wifi is NOT connected!

I use the WiFi.waitForConnectResult(10000); method because i want limit the waiting time to connect (10 Seconds).

The first connect works fine. But if i disconnect an try to connect again with the same method as before, then no connection is possible. The result is: WL_IDLE_STATUS.
Why can i not connect again with the same method as before after a disconnect?
Is it impossible? Is there another way to reconnect to the same wifi with a limit time method for connection?

Thank's for Help.

Originally created by @Primus007 on GitHub (Feb 15, 2021). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1213 ### Basic Infos Arduino IDE 1.8.13 #### Hardware **WiFimanager Branch/Release:** - [ ] Master - [ x] Development **Esp8266/Esp32:** - [ x] ESP8266 - [ ] ESP32 **Hardware: ESP-12e, esp01, esp25** - [ ] ESP01 - [ x] ESP12 E/F/S (nodemcu, wemos, feather) - [ ] Other **ESP Core Version: 2.4.0, staging** - [ ] 2.3.0 - [ ] 2.4.0 - [x] v2.7.4 - [ ] staging (master/dev) ### Description I use your WifiManager to connect to my home Network. It works fine. But after i get some infos from the internet i disconnect the wifi in my Code. it should reconnect if i press a button. but if i do that, always the AP Config mode fires up and i get the error: [ERROR] wifi begin failed ### Settings in IDE Module: Wemos D1 & R2 mini Additional libraries: ### Sketch ```#include <ESP8266WiFi.h> #include <WiFiManager.h> WiFiManager wm; void setup() { Serial.begin(115200); delay(100); Serial.println(""); WiFi.mode(WIFI_STA); // Station Mode Serial.print("try to connect to wifi "); Serial.println("SSID: " + WiFi.SSID()); startWiFi(); // Connect delay(2000); wm.disconnect(); Serial.println("Wifi Disconnected"); Serial.println(""); delay(2000); Serial.print("Try to RECONNECT to wifi "); Serial.println("SSID: " + WiFi.SSID()); startWiFi(); // Connect } void loop() { // } // ------------------------------------------------------------------------- // startWiFi // ------------------------------------------------------------------------- //for reference: //typedef enum { // WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library // WL_IDLE_STATUS = 0, // WL_NO_SSID_AVAIL = 1, // WL_SCAN_COMPLETED = 2, // WL_CONNECTED = 3, // WL_CONNECT_FAILED = 4, // WL_CONNECTION_LOST = 5, // WL_DISCONNECTED = 6 //} wl_status_t; void startWiFi() { Serial.print("Is WiFi Configured and saved: "); Serial.println((String)wm.getWiFiIsSaved() ? "YES" : "NO"); if (!WiFi.isConnected()) { Serial.println("SSID: " + WiFi.SSID()); int conn_result = WiFi.waitForConnectResult(10000); // connect try it for 10 seconds Serial.print("conn_result: "); Serial.println(conn_result, DEC); } if (WiFi.isConnected()) { Serial.println("Wifi is connected!"); } else { Serial.println("Wifi is NOT connected!"); } Serial.println(""); } ``` The serial result: First connect: conn_result: 3 Wifi is connected! Wifi Disconnected Try to RECONNECT to wifi SSID: InProg1 Is WiFi Configured and saved: YES SSID: InProg1 conn_result: 0 Wifi is NOT connected! I use the WiFi.waitForConnectResult(10000); method because i want limit the waiting time to connect (10 Seconds). The first connect works fine. But if i disconnect an try to connect again with the same method as before, then no connection is possible. The result is: WL_IDLE_STATUS. Why can i not connect again with the same method as before after a disconnect? Is it impossible? Is there another way to reconnect to the same wifi with a limit time method for connection? Thank's for Help.
Author
Owner

@tablatronix commented on GitHub (Feb 15, 2021):

Try a non persistent disconnect first, stuff gets stuck in esp and I suspect there are some lib bugs or sdk bugs atm.

wm.disconnect();

Or try connecting twice, another esp bug

<!-- gh-comment-id:779291406 --> @tablatronix commented on GitHub (Feb 15, 2021): Try a non persistent disconnect first, stuff gets stuck in esp and I suspect there are some lib bugs or sdk bugs atm. wm.disconnect(); Or try connecting twice, another esp bug
Author
Owner

@Primus007 commented on GitHub (Feb 15, 2021):

A second try to connect again does not solve the problem.

How do i an non persistent disconnect?

<!-- gh-comment-id:779377227 --> @Primus007 commented on GitHub (Feb 15, 2021): A second try to connect again does not solve the problem. How do i an non persistent disconnect?
Author
Owner

@tablatronix commented on GitHub (Feb 15, 2021):

using the wm func i pasted above

<!-- gh-comment-id:779379561 --> @tablatronix commented on GitHub (Feb 15, 2021): using the wm func i pasted above
Author
Owner

@Primus007 commented on GitHub (Feb 15, 2021):

I use wm.disconnect(); for disconnect . You can see it in the code in my first post.

At first i connect with: WiFi.waitForConnectResult(10000);
Then a delay
Then wm.disconnect();
and then again
WiFi.waitForConnectResult(10000);

The first connect works fine, the second not. Why?

What are the alternatives for a reconnect to the saved wifi with a connection timeout eg waitForConnectResult(10000)?

<!-- gh-comment-id:779434536 --> @Primus007 commented on GitHub (Feb 15, 2021): I use wm.disconnect(); for disconnect . You can see it in the code in my first post. At first i connect with: WiFi.waitForConnectResult(10000); Then a delay Then wm.disconnect(); and then again WiFi.waitForConnectResult(10000); The first connect works fine, the second not. Why? What are the alternatives for a reconnect to the saved wifi with a connection timeout eg waitForConnectResult(10000)?
Author
Owner

@tablatronix commented on GitHub (Feb 16, 2021):

Ah sorry did not see that not sure whats going on

<!-- gh-comment-id:779529913 --> @tablatronix commented on GitHub (Feb 16, 2021): Ah sorry did not see that not sure whats going on
Author
Owner

@tablatronix commented on GitHub (Feb 16, 2021):

Ill see what mine does, maybe try a diff version for esp?

<!-- gh-comment-id:779530172 --> @tablatronix commented on GitHub (Feb 16, 2021): Ill see what mine does, maybe try a diff version for esp?
Author
Owner

@Primus007 commented on GitHub (Feb 16, 2021):

I have tryed Core v2.7.0 - .4 always the same problem. Arduino sdk or Visual Studio always the same problem.

I have also tryed the latest Wifimanager master version. Same problem.

the result of 3 trying to connect again after the disconnect with debug messages. you see after the disonnect are no new debug messages.

try to connect to wifi SSID: Inprog111
Is WiFi Configured and saved: YES
SSID: Inprog111
wifi evt: 2
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 11
cnt

connected with Inprog111, channel 5
dhcp client start...
wifi evt: 0
ip:192.168.1.75,mask:255.255.255.0,gw:192.168.1.1
wifi evt: 3
conn_result: 3
Wifi is connected!

*WM: [1] Disconnecting
state: 5 -> 0 (0)
rm 0
Wifi Disconnected

wifi evt: 1
STA disconnect: 8

first try
Try to RECONNECT to wifi SSID: Inprog111
Is WiFi Configured and saved: YES
SSID: Inprog111
conn_result: 0
Wifi is NOT connected!

Second try
Try to RECONNECT to wifi SSID: Inprog111
Is WiFi Configured and saved: YES
SSID: Inprog111
conn_result: 0
Wifi is NOT connected!

third try
Try to RECONNECT to wifi SSID: Inprog111
Is WiFi Configured and saved: YES
SSID: Inprog111
conn_result: 0
Wifi is NOT connected!

<!-- gh-comment-id:779737104 --> @Primus007 commented on GitHub (Feb 16, 2021): I have tryed Core v2.7.0 - .4 always the same problem. Arduino sdk or Visual Studio always the same problem. I have also tryed the latest Wifimanager master version. Same problem. the result of 3 trying to connect again after the disconnect with debug messages. you see after the disonnect are no new debug messages. try to connect to wifi SSID: Inprog111 Is WiFi Configured and saved: YES SSID: Inprog111 wifi evt: 2 scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 11 cnt connected with Inprog111, channel 5 dhcp client start... wifi evt: 0 ip:192.168.1.75,mask:255.255.255.0,gw:192.168.1.1 wifi evt: 3 conn_result: 3 Wifi is connected! *WM: [1] Disconnecting state: 5 -> 0 (0) rm 0 Wifi Disconnected wifi evt: 1 STA disconnect: 8 first try Try to RECONNECT to wifi SSID: Inprog111 Is WiFi Configured and saved: YES SSID: Inprog111 conn_result: 0 Wifi is NOT connected! Second try Try to RECONNECT to wifi SSID: Inprog111 Is WiFi Configured and saved: YES SSID: Inprog111 conn_result: 0 Wifi is NOT connected! third try Try to RECONNECT to wifi SSID: Inprog111 Is WiFi Configured and saved: YES SSID: Inprog111 conn_result: 0 Wifi is NOT connected!
Author
Owner

@tablatronix commented on GitHub (Feb 16, 2021):

Where are you actually starting wifi ?

<!-- gh-comment-id:779900541 --> @tablatronix commented on GitHub (Feb 16, 2021): Where are you actually starting wifi ?
Author
Owner

@Primus007 commented on GitHub (Feb 16, 2021):

I have startet the wifimanager config portal and setup my wifi one times.

In the sketch from my first post is no start wifi. it is all what you see in my first post.
I use this line
int conn_result = WiFi.waitForConnectResult(10000); // connect try it for 10 seconds
to connect to the bevore saved wifi. I use this because i want to continue my program if the wifi is not available. a wifi connection is not absolutely necessary for my program

How should i start wifi? WiFi.begin need ssid and password. i want to connect to the saved WiFi.

<!-- gh-comment-id:779964731 --> @Primus007 commented on GitHub (Feb 16, 2021): I have startet the wifimanager config portal and setup my wifi one times. In the sketch from my first post is no start wifi. it is all what you see in my first post. I use this line `int conn_result = WiFi.waitForConnectResult(10000); // connect try it for 10 seconds` to connect to the bevore saved wifi. I use this because i want to continue my program if the wifi is not available. a wifi connection is not absolutely necessary for my program How should i start wifi? WiFi.begin need ssid and password. i want to connect to the saved WiFi.
Author
Owner

@tablatronix commented on GitHub (Feb 16, 2021):

WiFi.begin() or wm autoconnect

That just waits for the result to change, it should be in a loop, you should relook at examples as this is wrong

<!-- gh-comment-id:779984410 --> @tablatronix commented on GitHub (Feb 16, 2021): WiFi.begin() or wm autoconnect That just waits for the result to change, it should be in a loop, you should relook at examples as this is wrong
Author
Owner

@Primus007 commented on GitHub (Feb 16, 2021):

Many thanks you for your help. Now it's working.
I didn't know that it was just waiting for the result. I was confused that it connected at the first try.

<!-- gh-comment-id:780071369 --> @Primus007 commented on GitHub (Feb 16, 2021): Many thanks you for your help. Now it's working. I didn't know that it was just waiting for the result. I was confused that it connected at the first try.
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#1035
No description provided.