Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7c79cdd

Browse filesBrowse files
Fixed issue where esp32 won't reconnect to WiFi AP if the AP was restarted. (espressif#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>
1 parent 161b167 commit 7c79cdd
Copy full SHA for 7c79cdd

File tree

Expand file treeCollapse file tree

3 files changed

+176
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+176
-0
lines changed
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# WiFiClientConnect Example
2+
3+
This example demonstrates how to connect to the WiFi and manage the status and disconnection from STA.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 |
10+
| ----------------- | ----- | -------- | -------- |
11+
12+
## How to Use Example
13+
14+
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide).
15+
16+
#### Using Arduino IDE
17+
18+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
19+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
20+
21+
#### Using Platform IO
22+
23+
* Select the COM port: `Devices` or set the `upload_port`` option on the `platformio.ini` file.
24+
25+
## Example/Log Output
26+
27+
```
28+
[WiFi] Connecting to MyWiFiNetwork
29+
[ 66][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
30+
[ 150][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
31+
[ 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
32+
[ 151][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
33+
[WiFi] WiFi is disconnected
34+
[ 234][V][WiFiGeneric.cpp:353] _arduino_event_cb(): STA Connected: SSID: MyWiFiNetwork, BSSID: xx:xx:xx:xx:xx:xx, Channel: 8, Auth: WPA2_PSK
35+
[ 235][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
36+
[ 560][V][WiFiGeneric.cpp:367] _arduino_event_cb(): STA Got New IP:192.168.68.114
37+
[ 561][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
38+
[ 564][D][WiFiGeneric.cpp:1004] _eventCallback(): STA IP: 192.168.68.114, MASK: 255.255.255.0, GW: 192.168.68.1
39+
[WiFi] WiFi is connected!
40+
[WiFi] IP address: 192.168.68.114
41+
[WiFi] Disconnecting from WiFi!
42+
[ 2633][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: MyWiFiNetwork, BSSID: xx:xx:xx:xx:xx:xx, Reason: 8
43+
[ 2634][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
44+
[ 2635][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
45+
[ 2641][W][WiFiGeneric.cpp:953] _eventCallback(): Reason: 8 - ASSOC_LEAVE
46+
[ 2654][D][WiFiGeneric.cpp:975] _eventCallback(): WiFi the station is disconnected
47+
[ 2661][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
48+
[WiFi] Disconnected from WiFi!
49+
...
50+
```
51+
52+
## Troubleshooting
53+
54+
***Important: Be sure you're using a good quality USB cable that has enough power for your project.***
55+
56+
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
57+
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
58+
59+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
60+
61+
## Contribute
62+
63+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
64+
65+
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!
66+
67+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
68+
69+
## Resources
70+
71+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
72+
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
73+
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
74+
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
75+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Wi-Fi STA Connect and Disconnect Example
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
9+
*/
10+
#include <WiFi.h>
11+
12+
const char* ssid = "your-ssid";
13+
const char* password = "your-password";
14+
15+
int btnGPIO = 0;
16+
int btnState = false;
17+
18+
void setup()
19+
{
20+
Serial.begin(115200);
21+
delay(10);
22+
23+
// Set GPIO0 Boot button as input
24+
pinMode(btnGPIO, INPUT);
25+
26+
// We start by connecting to a WiFi network
27+
// To debug, please enable Core Debug Level to Verbose
28+
29+
Serial.println();
30+
Serial.print("[WiFi] Connecting to ");
31+
Serial.println(ssid);
32+
33+
WiFi.begin(ssid, password);
34+
// Auto reconnect is set true as default
35+
// To set auto connect off, use the following function
36+
// WiFi.setAutoReconnect(false);
37+
38+
// Will try for about 10 seconds (20x 500ms)
39+
int tryDelay = 500;
40+
int numberOfTries = 20;
41+
42+
// Wait for the WiFi event
43+
while (true) {
44+
45+
switch(WiFi.status()) {
46+
case WL_NO_SSID_AVAIL:
47+
Serial.println("[WiFi] SSID not found");
48+
break;
49+
case WL_CONNECT_FAILED:
50+
Serial.print("[WiFi] Failed - WiFi not connected! Reason: ");
51+
return;
52+
break;
53+
case WL_CONNECTION_LOST:
54+
Serial.println("[WiFi] Connection was lost");
55+
break;
56+
case WL_SCAN_COMPLETED:
57+
Serial.println("[WiFi] Scan is completed");
58+
break;
59+
case WL_DISCONNECTED:
60+
Serial.println("[WiFi] WiFi is disconnected");
61+
break;
62+
case WL_CONNECTED:
63+
Serial.println("[WiFi] WiFi is connected!");
64+
Serial.print("[WiFi] IP address: ");
65+
Serial.println(WiFi.localIP());
66+
return;
67+
break;
68+
default:
69+
Serial.print("[WiFi] WiFi Status: ");
70+
Serial.println(WiFi.status());
71+
break;
72+
}
73+
delay(tryDelay);
74+
75+
if(numberOfTries <= 0){
76+
Serial.print("[WiFi] Failed to connect to WiFi!");
77+
// Use disconnect function to force stop trying to connect
78+
WiFi.disconnect();
79+
return;
80+
} else {
81+
numberOfTries--;
82+
}
83+
}
84+
}
85+
86+
void loop()
87+
{
88+
// Read the button state
89+
btnState = digitalRead(btnGPIO);
90+
91+
if (btnState == LOW) {
92+
// Disconnect from WiFi
93+
Serial.println("[WiFi] Disconnecting from WiFi!");
94+
// This function will disconnect and turn off the WiFi (NVS WiFi data is kept)
95+
if(WiFi.disconnect(true, false)){
96+
Serial.println("[WiFi] Disconnected from WiFi!");
97+
}
98+
delay(1000);
99+
}
100+
}

‎libraries/WiFi/src/WiFiGeneric.cpp

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiGeneric.cpp
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
10701070

10711071
bool WiFiGenericClass::_isReconnectableReason(uint8_t reason) {
10721072
switch(reason) {
1073+
case WIFI_REASON_UNSPECIFIED:
10731074
//Timeouts (retry)
10741075
case WIFI_REASON_AUTH_EXPIRE:
10751076
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.