Lesson06: WiFi Scanning and Station Connection¶
1. Lesson Objectives¶
In this lesson, you will learn how to use WiFi Station mode on the ESP32-S3. The program first scans for nearby WiFi networks, then attempts to connect to the router using the account and password specified in the code. Even if no WiFi credentials are entered, you can still use the scanning function in this lesson to view information about nearby wireless networks.
2. What You Need¶
- Completed the ESP32-S3 environment setup in Lesson01.
- This lesson uses the built-in Arduino-ESP32
WiFilibrary. No additional third-party libraries need to be installed.
How to add library files:
- This lesson does not require any additional third-party libraries. It uses the WiFi library included with the ESP32 3.3.3 board package.
- Library download link:
3. Arduino IDE Procedure¶
-
If you need to connect to a router, replace
YOUR_WIFI_SSIDandYOUR_WIFI_PASSWORDwith the actual WiFi information.
-
Click Upload to flash the program, open the Serial Monitor, and select a baud rate of
115200.
4. Hardware Procedure¶
- Connect a USB data cable and place the device where WiFi coverage is available.
-
If you are only testing the scanning function, you do not need to modify the WiFi credentials.
-
If you are testing the connection function, enter the correct WiFi SSID and password in the code beforehand.
-
Open the Serial Monitor and observe the list of nearby WiFi networks, signal strength, channel, and encryption status.
-
If the connection fails, confirm that the router is using 2.4 GHz WiFi, and check whether the account and password are correct.
5. Key Code Explanation¶
WIFI_STA configures the ESP32-S3 to operate as a wireless client, which is the mode used to connect to a router. WiFi.disconnect(true) clears the previous connection state to prevent information from the last connection from affecting the current experiment. The program then scans for nearby networks before attempting to connect, so you can still verify that the WiFi module is working properly even if the credentials have not been configured.
const int networkCount = WiFi.scanNetworks();
for (int index = 0; index < networkCount; ++index) {
Serial.printf("%02d | SSID=%s | RSSI=%ld dBm | channel=%ld | encrypted=%s\n",
index + 1, WiFi.SSID(index).c_str(), WiFi.RSSI(index),
WiFi.channel(index),
WiFi.encryptionType(index) == WIFI_AUTH_OPEN ? "NO" : "YES");
}
WiFi.scanDelete();
WiFi.scanNetworks() returns the number of networks found. The loop reads each network's SSID, RSSI signal strength, channel, and encryption status in sequence. Finally, WiFi.scanDelete() releases the memory occupied by the scan results. This is an important practice on embedded devices.
while (WiFi.status() != WL_CONNECTED && millis() - startedAt < kConnectionTimeoutMs) {
Serial.print('.');
delay(500);
}
This code uses a timeout mechanism to wait for the connection to complete. WiFi.status() does not change to connected immediately, so the code needs to wait in a loop. However, it must not wait indefinitely; otherwise, the program could become stuck when the WiFi password is incorrect. millis() is used to determine whether 30 seconds have elapsed.



