[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.