AutoConnect aims to connect the ESP module as a station to a WiFi access point and equips with various APIs to maintain a WiFi connection as possible while sketch running. The main APIs are [AutoConnect::begin](api.md#begin) and [AutoConnect::handleClient](api.md#handleclient). You can make sketches with flexible WiFi connection capability by properly using these two APIs and the settings by [AutoConnectConfig](apiconfig.md).
- [Automatic reconnect](#automatic-reconnect)
- [Automatic reconnect (Background)](#automatic-reconnect-background)
- [Configure WiFi channel](#configure-wifi-channel)
- [Connects depending on the WiFi signal strength](#connects-depending-on-the-wifi-signal-strength)
- [Detects connection establishment to AP](#detects-connection-establishment-to-ap)
- [Match with known access points by SSID](#match-with-known-access-points-by-ssid)
- [Preserve AP mode](#preserve-ap-mode)
- [Timeout settings for a connection attempt](#timeout-settings-for-a-connection-attempt)
## Automatic reconnect
AutoConnect will change the WiFi mode depending on the situation. The [AutoConnect::begin](lsbegin.md) function starts WiFi mode in **STA** and starts the webserver if the connection is successful by the [**1st-WiFi.begin**](lsbegin.md). But if it will fail to connect with the least recently established access point, it will switch the WiFi mode to **AP_STA** and starts the DNS server to be able to launch a captive portal.
When the captive portal starts, **SoftAP** starts and STA disconnected. At this point, the station configuration information (it is known as the SDK's [station_config](https://github.com/esp8266/Arduino/blob/db75d2c448bfccc6dc308bdeb9fbd3efca7927ff/tools/sdk/include/user_interface.h#L249) structure) that the ESP module has stored on its own is discarded.
AutoConnect can connect to an access point again that has disconnected once, and its control is allowed by [*AutoConnectConfig::autoReconnect*](apiconfig.md#autoreconnect) that option specifies to attempt to reconnect to the past established access point using the saved credentials. If the [**autoReconnect**](apiconfig.md#autoreconnect) is enabled, AutoConnect will not launch SoftAP immediately even if 1st-WiFi.begin fails. When AutoConnect fails to connect WiFi, it will scan the WiFi signal to find out which access points it had connected to in the past. Then if it finds the saved BSSID in the broadcasts, AutoConnect will attempt to connect again applying the matched credential explicitly while still in STA mode. (AutoReconnect works well even with hidden SSID access points)
```cpp hl_lines="3"
AutoConnect Portal;
AutoConnectConfig Config;
Config.autoReconnect = true;
Portal.config(Config);
Portal.begin();
```
The [**autoReconnect**](apiconfig.md#autoreconnect) option is only available for [AutoConnect::begin](api.md#begin) without SSID and PASSWORD parameter. If you use [AutoConnect::begin](api.md#begin) with an SSID and PASSWORD, no reconnection attempt will be made if the 1st-WiFi.begin fails to connect to that SSID.
!!! note "The autoReconnect is not autoreconnect"
The [*WiFiSTAClass::disconnect*](https://github.com/espressif/arduino-esp32/blob/a0f0bd930cfd2d607bf3d3288f46e2d265dd2e11/libraries/WiFi/src/WiFiSTA.h#L46) function implemented in the arduino-esp32 has extended parameters than the ESP8266's arduino-core. The second parameter of WiFi.disconnect on the arduino-esp32 core that does not exist in the [ESP8266WiFiSTAClass](https://github.com/esp8266/Arduino/blob/7e1bdb225da8ab337373517e6a86a99432921a86/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp#L296) has the effect of deleting the currently connected WiFi configuration and its default value is "false". On the ESP32 platform, even if WiFi.disconnect is executed, WiFi.begin without the parameters in the next turn will try to connect to that AP. That is, automatic reconnection is implemented in arduino-esp32 already. Although this behavior appears seemingly competent, it is rather a disadvantage in scenes where you want to change the access point each time. When explicitly disconnecting WiFi from the Disconnect menu, AutoConnect will erase the AP connection settings saved by the arduino-esp32 core. AutoConnect's automatic reconnection is a mechanism independent from the automatic reconnection of the arduino-esp32 core.
## Automatic reconnect (Background)
Combining [**autoReconnect**](advancedusage.md#automatic-reconnect) with [*AutoConnectConfig::reconnectInterval*](apiconfig.md#reconnectinterval) allows you to periodically repeat connection attempts to known access points within [AutoConnect::handleClient](api.md#handleclient). This process is pseudo-asynchronous and does not block the Sketch process in the `loop()` function.
The reconnectInterval specifies the interval time to seek for known access points with saved credentials during the **handleClient** loop and attempt to connect to the AP.
```cpp hl_lines="5 6"
AutoConnect Portal;
AutoConnectConfig Config;
void setup() {
Config.autoReconnect = true; // Attempt automatic reconnection.
Config.reconnectInterval = 6; // Seek interval time is 180[s].
Portal.config(Config);
Portal.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Here to do when WiFi is connected.
}
else {
// Here to do when WiFi is not connected.
}
Portal.handleClient();
}
```
Above Sketch shows a configuration example that you want to keep connecting to known access points as long as possible. When the WiFi connection is lost, it will start seeking the WiFi network every 30 seconds during the handleClient loop.
!!! info "Limitation for automatic reconnection to a specific access point"
An access point that ESP module to reconnect automatically depends on whether the SSID and password argument existence with [AutoConnect::begin](api.md#begin). If the Sketch calls [AutoConnect::begin](api.md#begin) without specifying an SSID or password, the [autoReconnect](apiconfig.md#autoreconnect) will connect to one of the detected access points and cannot be pre-determined.
The other one, the case of the Sketch specifies SSID and password with [AutoConnect::begin](api.md#begin), the [autoReconnect](apiconfig.md#autoreconnect) will try to reconnect to a specified access point periodically during the handleClient loop.
Also, you can combine the background automatic reconnect performing inside the loop function by [handleClient](api.md#handleclient) with [*AutoConnectConfig::retainPortal*](apiconfig.md#retainportal) and [*AutoConnectConfig::autoReset*](apiconfig.md#autoreset), to enable pop up the captive portal automatically on the client device each time the ESP module disconnects from the access point.
```cpp
AutoConnect Portal;
AutoConnectConfig Config;
void setup() {
Config.autoReset = false; // Not reset the module even by intentional disconnection using AutoConnect menu.
Config.autoReconnect = true; // Reconnect to known access points.
Config.reconnectInterval = 6; // Reconnection attempting interval is 3[min].
Config.retainPortal = true; // Keep the captive portal open.
Portal.config(Config);
Portal.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Here to do when WiFi is connected.
}
else {
// Here to do when WiFi is not connected.
}
}
```
!!! info "The effective range of the [reconnectInterval](apiconfig.md#reconnectinterval) depending on the setting value"
The range of values that reconnectInterval can take is 0 to 255. (Actual seconds are from 0 to 255×AUTOCONNECT_UNITTIME)
Reconnect behavior depends on the setting value. If it is 0, reconnection will work if the 1st-WiFi.begin in AutoConnect::begin fails and will suspend during the handleClient loop. If reconnectInterval is greater than 0, AutoConnect will attempt to reconnect both in AutoConnect::begin and during the handleClient loop.
## Configure WiFi channel
Appropriately specifying the WiFi channel to use for ESP8266 and ESP32 is essential for a stable connection with the access point. AutoConnect remembers the WiFi channel with a credential of the access point once connected and reuses it.
The default channel when a captive portal starts and AutoConnect itself becomes an access point is the [*AutoConnectConfig::channel*](apiconfig.md#channel) member. If this channel is different from the channel of the access point you will attempt to connect, WiFi.begin may fail. The cause is that the ESP module shares the same channel in AP mode and STA mode. If the connection attempt is not stable, specifying a proper channel using AutoConnectConfig::channel may result in a stable connection.
## Connects depending on the WiFi signal strength
When the ESP module found the multiple available access points (ie. AutoConnect has connected in the past), the default behavior AutoConnect will attempt to connect to the least recent one. However, If the ESP module can operate properly with any access point, it is advantageous to establish a connection with the best one of the reception sensitivity.
The [*AutoConnectConfig::principle*](apiconfig.md#principle) parameter has the connection disposition, and specifying **AC_PRINCIPLE_RSSI** will attempt to connect to one of the highest RSSI value among multiple available access points. Also You can expect stable WiFi connection by specifying the lower limit of signal strength using [*AutoConnectConfig::minRSSI*](apiconfig.md#minrssi).
Combining these two parameters allows you to filter the destination AP when multiple available access points are found.
[*AutoConnectConfig::principle*](apiconfig.md#principle) affects the behavior of both 1st-WiFi.begin and [**autoReconnect**](advancedusage.md#automatic-reconnect). If you specify **AC_PRINCIPLE_RECENT** for the [**principle**](apiconfig.md#principle), it will try according to the conventional connection rules, but if you specify **AC_PRINCIPLE_RSSI**, it will try to connect to the access point that is sending the strongest WiFi signal at that time instead of the last accessed AP. Also, the static IPs will be restored from a saved credential instead of AutoConnectConfig. (The values specified by AutoConnectConfig is ignored)
SSID & Password |
AutoConnectConfig ::principle |
Which credentials would be selected | Static IPs | |
---|---|---|---|---|
AutoConnect ::begin |
NULL specified | AC_PRINCIPLE_RECENT | Nothing, depends on SDK saves | Use the specified value of AutoConnectConfig |
AC_PRINCIPLE_RSSI | Auto-selected credentials with max RSSI | Restoring static IPs suitable for the SSID from saved credentials | ||
Specified with the Sketch | Not effective | By AutoConnect::begin parameters | Use the specified value of AutoConnectConfig | |
AutoReconnect | Load from saved credential |
AC_PRINCIPLE_RECENT | Recently saved SSID would be chosen | Restoring static IPs suitable for the SSID from saved credentials |
AC_PRINCIPLE_RSSI | Auto-selected credentials with max RSSI |