-->
Page 1 of 2

Can not reconnect after disconnet, please help.

PostPosted: Fri Feb 12, 2021 1:46 pm
by Tertius21
I Connect to a saved Wifi. it works fine. Then i disconnect and try to reconnect with the same connect method as before, but reconnect is always connection result WL_IDLE_STATUS = 0

Why? What's wrong?

Here is my Code:
Code: Select all#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("");
}


And here the results:
try to connect to wifi SSID: InProg1
Is WiFi Configured and saved: YES
SSID: InProg1
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!

The conn_result 0 comes promptly not after 10 seconds.

Re: Can not reconnect after disconnet, please help.

PostPosted: Sun Feb 14, 2021 8:13 am
by Tertius21
no suggestions?

Re: Can not reconnect after disconnet, please help.

PostPosted: Sun Feb 14, 2021 8:40 am
by Pablo2048
Well it seems like you forget to call .connect() method. In the loop() you call WiFi.disconnect() but never any .connect() so you are waiting for nothing in waitForConnectResult() IMO...

Re: Can not reconnect after disconnet, please help.

PostPosted: Sun Feb 14, 2021 9:20 am
by Tertius21
i have the code a little bit changed for better understanding.

This:
Code: Select allWiFi.waitForConnectResult(10000); // connect try it for 10 seconds

connects to the wifi.