[SOLVED] WiFi.reconnect()

Hello to all,

I setup a Heltec 32 Kit to run as a station.
I try to reconnect Wifi after a lost of link with router AP.

So I tried to use the functions:
WiFi.setAutoReconnect(true); //* Set whether module will attempt to reconnect to an access point in case it is disconnected

and

WiFi.reconnect()
Reconnect the station. This is done by disconnecting from the access point an then initiating connection back to the same AP.

But this feature don’t running from my side.

To reconnect, I just re-use my initial setup made at starting:
WiFi.mode(WIFI_STA); //* Station Mode
WiFi.setAutoConnect(true); //* Configure module to automatically connect on power on to the last used access point
WiFi.setAutoReconnect(true); //* Set whether module will attempt to reconnect to an access point in case it is disconnected
WiFi.setHostname(_HOSTNAME); //* Give an unique name of the hardware
WiFi.begin(_SSID, _PASSWORD); //* Launch Wifi

This solution running, but from my side, I think, it’s not very “clean”.
I read severals topic about this reconnect subject, but from my side I don’t found a “right” solution.

Do you have an idea, to use reconnect feature in right way ?

Best Regards.
Christophe.

I tested the above two functions, WiFi.reconnect () is fully available. But WiFi.setAutoReconnect (true) I failed. According to information on the Internet, it searches by default to the channel to which it is connected, and the channel at this time may already be occupied. I will confirm it further and hope that the problem can be solved.

/*

  • This sketch sends data via HTTP GET requests to data.sparkfun.com service.
  • You need to get streamId and privateKey at data.sparkfun.com and paste them
  • below. Or just customize this script to talk to other HTTP servers.

*/

#include <WiFi.h>

const char* ssid = “Quency”;
const char* password = “147369++”;

void setup()
{
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
delay(500); 

}

void loop()
{
delay(1000);
if(WiFi.status() == WL_CONNECTED){
Serial.println(“wificonnect!!! !”);
}
else{
Serial.println(“failed!!! !”);
WiFi.reconnect();
}
}

Dear Quency-D,

First thanks.
Now it’s ok the WiFi.reconnect() function running in right way.
It’s seems, the adding of WiFi.persistent(true) at minimum one time unlocked the situation.
Even if after, I relaunch with WiFi.persistent(false) the WiFi.reconnect continues to operate.

I don’t think the channel was occupied by an other hardware, because I tested with my phone as AP and I saw that 0 device was connected.

Best Regards.

Hi All…
I faced the same issue with my WifiKit 32 boards. If the board lost WiFi connection, the reconnect will not work in proper way. Now, I tried the sollution from Quency-D, but failed again.
Please finde my functions below. The “OLED_WiFiState(x)” is dealing with messages on the OLED, only.

Any advices to get rid of this will be highly appreciated.
Regards
KLaus

This is the setup for WiFI:

void WifiSetUp()
// stellt die initiale Wlan-Verbindung her und erstattet Meldung
// Author: Sampel-Sketch Wifi_Kit_32
// Stand 08.10.2020

{
Serial.println(ssid);
Serial.println(pass);
OLED_WiFiState(1);

// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.disconnect(true);
delay(1000);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid,pass);
delay(1000);

byte count = 0;
while(WiFi.status() != WL_CONNECTED && count < 10)
{
count ++;
delay(500);
}

if(WiFi.status() == WL_CONNECTED)
{

OLED_WiFiState(2);
WiFi.setAutoConnect(true);
WiFi.persistent(true);

}
else
{
OLED_WiFiState(3);

}
OLED_WiFiState(4);

}

And this is the Wifi-Check called in Loop

void check_WiFi()
// Überprüft ständig die Wlan-Verbindung und erstattet Meldung
// Stand 16.04.2021

{
// überprüft die WiFi-Verbindung, Status-Ausgabe im OLED
if (WiFi.status ()!= WL_CONNECTED)
{
Serial.println(“Keine WLAN verbunden”);
OLED_WiFiState(5);
WiFi.reconnect();

counter_WF++; // counter for debugging reasons only

}
}