Auto Reconnect to Station without disconnecting AP
Posted: Mon Dec 20, 2021 7:19 am
Hello,
my sketch uses WIFI_AP_STA as WiFi mode.
The AP setup works fine, but the connection to the station is sometimes lost.
As I know by now, unlike the ESP8266, the ESP32 does not then automatically connect to the station after losing the wifi connection.
So I wonder: what is the correct way to re-trigger the connection with the station?
I often read that a reboot would be the best way or this way:
wifi.disconnect();
wifi.begin();
or
wifi.disconnect();
wifi.reconnect();
But:
If I do a WiFi.disconnect(), my AP will also be terminated, right?
And I want to keep the AP always up and reconnect only the STA.
So I decided to use this variant:
Is this ok or is it bad to use a Wifi.begin() without a previous Wifi.disconnect()?
Thanks already for your help
Best wishes
Daniel
my sketch uses WIFI_AP_STA as WiFi mode.
The AP setup works fine, but the connection to the station is sometimes lost.
As I know by now, unlike the ESP8266, the ESP32 does not then automatically connect to the station after losing the wifi connection.
So I wonder: what is the correct way to re-trigger the connection with the station?
I often read that a reboot would be the best way or this way:
wifi.disconnect();
wifi.begin();
or
wifi.disconnect();
wifi.reconnect();
But:
If I do a WiFi.disconnect(), my AP will also be terminated, right?
And I want to keep the AP always up and reconnect only the STA.
So I decided to use this variant:
Code: Select all
if (WiFi.status() != WL_CONNECTED) {
if (millis() - reconnmillis > reconn_timer) {
WiFi.begin(ssid, password);
unsigned long pause=5000;
unsigned long connTimer = millis();
while (WiFi.status() != WL_CONNECTED && (millis()-connTimer < pause)) {
delay(500);
yield();
}
reconnmillis = millis();
}
}
Thanks already for your help
Best wishes
Daniel