13. Wi-Fi
ESP32-S3 supports the 2.4 GHz Wi-Fi 4, 802.11n, standard and provides data rates up to 150 Mbps. It supports STA, Station, mode, AP, Access Point, mode, and Wi-Fi Direct mode, allowing it to connect flexibly to other devices or create its own network. ESP32-S3 also supports hardware-accelerated Wi-Fi encryption algorithms, including WPA/WPA2-PSK and WPA3-SAE. This makes encryption and decryption faster and improves overall system performance and security.
The Wi-Fi library supports configuring and monitoring ESP32 Wi-Fi network functions.
It has three modes:
- Station mode, also called STA mode or Wi-Fi client mode, where the ESP32 connects to an access point, AP.
- AP mode, also called Soft-AP mode or access point mode, where stations connect to the ESP32.
- AP-STA coexistence mode, where the ESP32 is both an access point and a station connected to another access point.
API Introduction
The following are commonly used Wi-Fi-related functions in the ESP32S3 Arduino library:
WiFi.begin(ssid, password)Connects to a Wi-Fi network. The SSID and password of the target network must be provided as parameters.WiFi.disconnect()Disconnects the current Wi-Fi connection.WiFi.status()Returns the current Wi-Fi connection status. Possible return values include:WL_CONNECTED: connected to a Wi-Fi network.WL_DISCONNECTED: not connected to a Wi-Fi network.WL_IDLE_STATUS: Wi-Fi is idle.WL_NO_SSID_AVAIL: the specified Wi-Fi network was not found.WiFi.localIP()Returns the local IP address assigned to the ESP32S3 device in the Wi-Fi network.WiFi.macAddress()Returns the MAC address of the ESP32S3 device.WiFi.scanNetworks()Scans nearby available Wi-Fi networks. It returns an integer indicating the number of networks found. Other functions, such asWiFi.SSID()andWiFi.RSSI(), can be used to obtain details for each network.WiFi.SSID(networkIndex)Returns the SSID of the scanned Wi-Fi network at the specified index.WiFi.RSSI(networkIndex)Returns the signal strength, RSSI, of the scanned Wi-Fi network at the specified index.
STA Mode
In STA mode, ESP32-S3 establishes a Wi-Fi connection to an existing Wi-Fi hotspot and accesses the internet through that hotspot. STA mode is widely used. For example, in smart home, IoT device, and industrial control applications, devices need to connect to a network over Wi-Fi to transmit data. Example: connect to the external Wi-Fi network LCKFB. When the connection succeeds, output the IP address through serial port 0 and blink the onboard LED.
#include <WiFi.h>
#define LED 48
// Define the Wi-Fi name and password to connect to
const char* ssid = "LCKFB";
const char* password = "12345678";
void setup() {
Serial.begin(9600);
// Disconnect the previous connection
WiFi.disconnect(true);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
// Check whether the connection is successful
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected successfully");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Use the onboard LED to indicate a successful connection
pinMode(LED, OUTPUT);
}
void loop() {
// LED blinking indicates successful connection
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
AP Mode
An access point, AP, is a device that provides Wi-Fi network access and connects to a wired network. Although ESP32S3 does not have a wired network interface, it can provide similar functionality. This operating mode is called soft access point, or soft-AP. The maximum number of stations that can connect to soft-AP can be set to 4, and the default is 4.
When ESP32S3 operates in AP mode alone, it can be considered a local Wi-Fi router node without internet access. It can accept connection requests from various devices and establish TCP or UDP connections with connected devices to exchange data. In local IoT designs, it can act as a data sending and receiving node.
Example Create an external Wi-Fi network named LCKFB. When creation succeeds, output the IP address through serial port 0 and blink the onboard LED.
#include <WiFi.h>
#define LED 48
// Set the hotspot name and password to create
const char* ssid = "LCKFB_ESP32";
const char* password = "12345678";
void setup() {
Serial.begin(9600);
// Create hotspot
WiFi.softAP(ssid, password);
// Print hotspot IP
Serial.print("Wi-Fi AP IP: ");
Serial.println(WiFi.softAPIP());
// Use the onboard LED to indicate successful creation
pinMode(LED, OUTPUT);
}
void loop() {
// LED blinking indicates hotspot creation is successful
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29