Fixed issue where esp32 won't reconnect to WiFi AP if the AP was restarted. (#7512)
* Fixed issue where esp32 won't reconnect to WiFi AP if the AP was restarted * Added the autoReconnect condition and removed the reconnect reason * Chenges on the WiFi reconnection and added new example * Added new line at the end * Changes reverted with some minor changes. New example remained * Changes reverted and sync Co-authored-by: pedro.minatel <pedro.minatel@espressif.com>
This commit is contained in:
parent
161b1673d9
commit
7c79cdd6e3
3 changed files with 176 additions and 0 deletions
75
libraries/WiFi/examples/WiFiClientConnect/README.md
Normal file
75
libraries/WiFi/examples/WiFiClientConnect/README.md
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
# WiFiClientConnect Example
|
||||||
|
|
||||||
|
This example demonstrates how to connect to the WiFi and manage the status and disconnection from STA.
|
||||||
|
|
||||||
|
# Supported Targets
|
||||||
|
|
||||||
|
Currently, this example supports the following targets.
|
||||||
|
|
||||||
|
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 |
|
||||||
|
| ----------------- | ----- | -------- | -------- |
|
||||||
|
|
||||||
|
## How to Use Example
|
||||||
|
|
||||||
|
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide).
|
||||||
|
|
||||||
|
#### Using Arduino IDE
|
||||||
|
|
||||||
|
* Before Compile/Verify, select the correct board: `Tools -> Board`.
|
||||||
|
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
|
||||||
|
|
||||||
|
#### Using Platform IO
|
||||||
|
|
||||||
|
* Select the COM port: `Devices` or set the `upload_port`` option on the `platformio.ini` file.
|
||||||
|
|
||||||
|
## Example/Log Output
|
||||||
|
|
||||||
|
```
|
||||||
|
[WiFi] Connecting to MyWiFiNetwork
|
||||||
|
[ 66][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
|
||||||
|
[ 150][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
|
||||||
|
[ 151][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
|
||||||
|
[ 151][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
|
||||||
|
[WiFi] WiFi is disconnected
|
||||||
|
[ 234][V][WiFiGeneric.cpp:353] _arduino_event_cb(): STA Connected: SSID: MyWiFiNetwork, BSSID: xx:xx:xx:xx:xx:xx, Channel: 8, Auth: WPA2_PSK
|
||||||
|
[ 235][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
|
||||||
|
[ 560][V][WiFiGeneric.cpp:367] _arduino_event_cb(): STA Got New IP:192.168.68.114
|
||||||
|
[ 561][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
|
||||||
|
[ 564][D][WiFiGeneric.cpp:1004] _eventCallback(): STA IP: 192.168.68.114, MASK: 255.255.255.0, GW: 192.168.68.1
|
||||||
|
[WiFi] WiFi is connected!
|
||||||
|
[WiFi] IP address: 192.168.68.114
|
||||||
|
[WiFi] Disconnecting from WiFi!
|
||||||
|
[ 2633][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: MyWiFiNetwork, BSSID: xx:xx:xx:xx:xx:xx, Reason: 8
|
||||||
|
[ 2634][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
|
||||||
|
[ 2635][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
|
||||||
|
[ 2641][W][WiFiGeneric.cpp:953] _eventCallback(): Reason: 8 - ASSOC_LEAVE
|
||||||
|
[ 2654][D][WiFiGeneric.cpp:975] _eventCallback(): WiFi the station is disconnected
|
||||||
|
[ 2661][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
|
||||||
|
[WiFi] Disconnected from WiFi!
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
***Important: Be sure you're using a good quality USB cable that has enough power for your project.***
|
||||||
|
|
||||||
|
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
|
||||||
|
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
|
||||||
|
|
||||||
|
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
|
||||||
|
|
||||||
|
## Contribute
|
||||||
|
|
||||||
|
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
|
||||||
|
|
||||||
|
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
|
||||||
|
|
||||||
|
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
|
||||||
|
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
|
||||||
|
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
|
||||||
|
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
|
||||||
|
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
|
||||||
|
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
|
||||||
100
libraries/WiFi/examples/WiFiClientConnect/WiFiClientConnect.ino
Normal file
100
libraries/WiFi/examples/WiFiClientConnect/WiFiClientConnect.ino
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
/* Wi-Fi STA Connect and Disconnect Example
|
||||||
|
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
|
||||||
|
*/
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
const char* ssid = "your-ssid";
|
||||||
|
const char* password = "your-password";
|
||||||
|
|
||||||
|
int btnGPIO = 0;
|
||||||
|
int btnState = false;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
// Set GPIO0 Boot button as input
|
||||||
|
pinMode(btnGPIO, INPUT);
|
||||||
|
|
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
// To debug, please enable Core Debug Level to Verbose
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("[WiFi] Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
// Auto reconnect is set true as default
|
||||||
|
// To set auto connect off, use the following function
|
||||||
|
// WiFi.setAutoReconnect(false);
|
||||||
|
|
||||||
|
// Will try for about 10 seconds (20x 500ms)
|
||||||
|
int tryDelay = 500;
|
||||||
|
int numberOfTries = 20;
|
||||||
|
|
||||||
|
// Wait for the WiFi event
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
switch(WiFi.status()) {
|
||||||
|
case WL_NO_SSID_AVAIL:
|
||||||
|
Serial.println("[WiFi] SSID not found");
|
||||||
|
break;
|
||||||
|
case WL_CONNECT_FAILED:
|
||||||
|
Serial.print("[WiFi] Failed - WiFi not connected! Reason: ");
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
case WL_CONNECTION_LOST:
|
||||||
|
Serial.println("[WiFi] Connection was lost");
|
||||||
|
break;
|
||||||
|
case WL_SCAN_COMPLETED:
|
||||||
|
Serial.println("[WiFi] Scan is completed");
|
||||||
|
break;
|
||||||
|
case WL_DISCONNECTED:
|
||||||
|
Serial.println("[WiFi] WiFi is disconnected");
|
||||||
|
break;
|
||||||
|
case WL_CONNECTED:
|
||||||
|
Serial.println("[WiFi] WiFi is connected!");
|
||||||
|
Serial.print("[WiFi] IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.print("[WiFi] WiFi Status: ");
|
||||||
|
Serial.println(WiFi.status());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
delay(tryDelay);
|
||||||
|
|
||||||
|
if(numberOfTries <= 0){
|
||||||
|
Serial.print("[WiFi] Failed to connect to WiFi!");
|
||||||
|
// Use disconnect function to force stop trying to connect
|
||||||
|
WiFi.disconnect();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
numberOfTries--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
// Read the button state
|
||||||
|
btnState = digitalRead(btnGPIO);
|
||||||
|
|
||||||
|
if (btnState == LOW) {
|
||||||
|
// Disconnect from WiFi
|
||||||
|
Serial.println("[WiFi] Disconnecting from WiFi!");
|
||||||
|
// This function will disconnect and turn off the WiFi (NVS WiFi data is kept)
|
||||||
|
if(WiFi.disconnect(true, false)){
|
||||||
|
Serial.println("[WiFi] Disconnected from WiFi!");
|
||||||
|
}
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1070,6 +1070,7 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
|
||||||
|
|
||||||
bool WiFiGenericClass::_isReconnectableReason(uint8_t reason) {
|
bool WiFiGenericClass::_isReconnectableReason(uint8_t reason) {
|
||||||
switch(reason) {
|
switch(reason) {
|
||||||
|
case WIFI_REASON_UNSPECIFIED:
|
||||||
//Timeouts (retry)
|
//Timeouts (retry)
|
||||||
case WIFI_REASON_AUTH_EXPIRE:
|
case WIFI_REASON_AUTH_EXPIRE:
|
||||||
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
|
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue