The ESP32-S3 module on the Arduino UNO R4 WiFi supports a variety of mainstream network protocols, including HTTPS, MQTT, UDP, etc., all of which have been officially tested and verified with guaranteed stability. Whether building an Internet of Things (IoT) node, realizing remote data transmission, or controlling the development board through the network, mastering its WiFi connection method is a basic prerequisite.
This program connects the Arduino UNO R4 WiFi board to a local WiFi network in STA (Station) Mode, outputs network information upon successful connection, and continuously monitors the connection status.
The WiFi functionality of the UNO R4 WiFi relies entirely on the onboard ESP32-S3 module, eliminating the need for additional wireless modules. You also need to use the official dedicated library to call related functions properly.
| Item | Description |
|---|---|
| Hardware | Arduino UNO R4 WiFi Development Board |
| Core Library | WiFiS3 (Built-in for UNO R4 series) |
| Core Library | Serial (Arduino built-in serial library) |
Functions:
WiFiS3 is a WiFi communication library designed specifically for the ESP32-S3 module on the Arduino UNO R4 WiFi. It provides the following core functions:
| Function Category | Specific Function | Example Functions |
|---|---|---|
| Connection Management | Connect/disconnect WiFi network | WiFi.begin(), WiFi.disconnect() |
| Status Query | Get connection status and network info | WiFi.status(), WiFi.localIP() |
| Network Information | Get IP, MAC, signal strength | WiFi.macAddress(), WiFi.RSSI() |
| Mode Switching | STA mode and AP mode | WiFi.mode() |
Installation Method:
The WiFiS3 library is pre-installed in the board's core firmware at the factory. No manual download or installation is required. Simply import it in your code to use.
Exception Handling (Missing Library File):
If the Arduino IDE shows WiFiS3.h: No such file or directory during compilation, it means the board's core library is not installed correctly or the version is too old. Follow these steps to resolve:
Functions:
The Serial library is part of the Arduino core library, used for USB serial communication with a computer:
| Function Category | Specific Function | Example Functions |
|---|---|---|
| Initialization | Set serial baud rate | Serial.begin(9600) |
| Data Transmission | Send strings and data | Serial.print(), Serial.println() |
| Data Reception | Read serial data | Serial.read(), Serial.available() |
| Status Query | Check if serial is ready | Serial (boolean) |
Installation Method:
The Serial library is a built-in Arduino core library. No separate installation is required for any Arduino board.
The UNO R4 WiFi supports two WiFi working modes, suitable for different scenarios with significantly different configuration methods. Choose according to your actual needs.
STA mode, also known as client mode, is the most commonly used working mode. The board acts as a WiFi client, connecting to an existing wireless network (such as a home router or office WiFi). After accessing the LAN, it can realize functions like Internet access, communication with other devices in the LAN, and remote data transmission.
Configuration: Enter the WiFi name (SSID) and password of the target wireless network (router), and the board will access the network using this information.
AP mode, also known as hotspot mode, allows the board to emit WiFi signals itself, equivalent to a small router. Devices like mobile phones and computers can directly search for and connect to this hotspot without relying on an external router. This mode is suitable for scenarios without an external WiFi environment or when direct short-distance communication with the board is required (such as controlling the board directly with a mobile phone).
Configuration: Customize the WiFi hotspot name (SSID) and password (recommended ≥ 8 characters), and other devices connect to the board using this name and password.
Mastering the following core functions allows you to implement WiFi connection, status judgment, and information acquisition for the UNO R4 WiFi. All functions can only be used after importing the WiFiS3.h library.
WiFi.begin(ssid, password)
ssid is the router's WiFi name (string type), password is the router's WiFi password (string type);WiFi.status().After connecting to WiFi, you can obtain key information such as connection status, signal strength, and IP address through the following functions to determine if the connection is normal or to meet project development needs.
Function: Get the status code of the current WiFi connection, mainly used to determine if the connection is successful.
Common Status Codes (Focus on the first 3 for beginners):
| Status Code Name | Value | Description |
|---|---|---|
WL_CONNECTED |
3 | WiFi connection successful, normal network communication possible |
WL_NO_SSID_AVAIL |
1 | Specified SSID not found (wrong WiFi name or weak signal) |
WL_CONNECT_FAILED |
4 | Connection failed, most likely due to wrong WiFi password |
WL_IDLE_STATUS |
0 | WiFi is in idle state, trying to connect |
WL_DISCONNECTED |
6 | WiFi has been disconnected |
Function: Get the current WiFi signal strength. The return value is an integer (unit: dBm).
Key Explanation: The closer the signal strength value is to 0, the better the signal; the smaller the value (the larger the absolute value of the negative number), the weaker the signal. For example: -30dBm means extremely strong signal, -80dBm means weak signal, and -100dBm means extremely weak signal (may cause unstable connection).
Function: Get the LAN IP address assigned to the board after accessing the network. This is the core indicator to judge whether the network is truly available.
Two Result Types:
| Result Type | Description |
|---|---|
| Normal | Returns a valid IP address (e.g., 192.168.1.100), indicating WiFi connection successful and IP obtained, normal network communication possible |
| Abnormal | Returns 0.0.0.0, indicating the board has physically connected to the router (WiFi signal connected), but the router has not assigned an IP address (DHCP allocation failure), network communication not possible at this time |
#include <WiFiS3.h>
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED || WiFi.localIP() == IPAddress(0,0,0,0)) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Signal Strength: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
}
void loop() {
Serial.print("Connection Status: ");
Serial.println(WiFi.status() == WL_CONNECTED ? "Connected" : "Disconnected");
delay(1000);
}
Usage: Open the WIFI_STA_MODE file in the same folder, modify the SSID and password to match your WiFi credentials, then upload the program to your Arduino UNO R4 board.
#include <WiFiS3.h>
Includes the WiFi library designed specifically for Arduino UNO R4 WiFi, providing all WiFi-related functions.
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";
| Parameter | Description |
|---|---|
ssid |
WiFi network name (case-sensitive) |
password |
WiFi network password (case-sensitive) |
⚠️ Important: Replace "YOUR SSID" and "YOUR PASSWORD" with your actual WiFi network credentials.
Serial.begin(9600);
while (!Serial);
| Code | Purpose |
|---|---|
Serial.begin(9600) |
Start serial communication at 9600 baud rate |
while (!Serial) |
Wait for Serial Monitor to open (required for some boards) |
WiFi.begin(ssid, password);
Function: Initiates WiFi connection with the specified SSID and password.
while (WiFi.status() != WL_CONNECTED || WiFi.localIP() == IPAddress(0,0,0,0)) {
delay(500);
Serial.print(".");
}
⚠️ Critical Point: This is the most important part of the code!
The condition checks two things:
| Condition | Meaning |
|---|---|
WiFi.status() != WL_CONNECTED |
Checks if WiFi is connected |
WiFi.localIP() == IPAddress(0,0,0,0) |
Checks if IP address is valid |
Why double check?
The UNO R4 WiFi has a firmware bug where WiFi.status() may return WL_CONNECTED before the IP address is properly assigned. This results in WiFi.localIP() returning 0.0.0.0, meaning no actual network access.
By adding the IP address check, we ensure the board has a valid IP before proceeding.
Serial.println("");
Serial.println("WiFi connected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Signal Strength: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
| Function | Description |
|---|---|
WiFi.localIP() |
Returns the board's local IP address (e.g., 192.168.1.100) |
WiFi.RSSI() |
Returns WiFi signal strength in dBm (range: -100 to 0, closer to 0 is better) |
void loop() {
Serial.print("Connection Status: ");
Serial.println(WiFi.status() == WL_CONNECTED ? "Connected" : "Disconnected");
delay(1000);
}
Continuously monitors and prints the WiFi connection status every 1 second.
| Parameter | Value | Description |
|---|---|---|
ssid |
String | WiFi network name |
password |
String | WiFi network password |
Serial.begin(9600) |
9600 | Serial baud rate (must match Serial Monitor) |
delay(500) |
500ms | Wait interval during connection attempt |
delay(1000) |
1000ms | Status update interval in main loop |
Connecting to WiFi: MyHomeWiFi
.....
WiFi connected successfully!
IP Address: 192.168.1.105
Signal Strength: -58 dBm
Connection Status: Connected
Connection Status: Connected
Connection Status: Connected
...
| Problem | Cause | Solution |
|---|---|---|
IP address shows 0.0.0.0 |
UNO R4 firmware bug / DHCP allocation failure | Use the double-check condition in while loop; confirm connecting to 2.4G WiFi; check if router DHCP is enabled; restart board and router |
| Connection timeout | Wrong SSID/password | Verify credentials (case-sensitive) |
| No dots appearing | Serial Monitor not opened | Open Serial Monitor at 9600 baud rate |
| Weak signal | Board too far from router | Move closer to WiFi router |
| Compilation error: missing library | Core library not installed or outdated | Install/update Arduino UNO R4 core library following the exception handling steps in "Hardware and Library Dependencies" |
| Connection failed (WL_CONNECT_FAILED) | Wrong WiFi password | Check if WiFi password is correct (case-sensitive) or WiFi name is filled incorrectly |
In some cases, using the STA mode program with automatic IP (DHCP) may encounter problems:
| Scenario | Problem Description |
|---|---|
| DHCP Allocation Failure | Router's DHCP service is abnormal, unable to assign IP to the board |
| IP Address Conflict | Other devices in the network occupy the same IP address |
| IP Address Changes | IP address may change each time reconnection occurs, not conducive to remote control |
| Firmware Bug | UNO R4 WiFi may have 0.0.0.0 IP issue |
Purpose of FIXED_IP_ADDRESS program: By manually configuring a static IP, bypass the DHCP automatic allocation mechanism, ensuring the board always uses a fixed IP address, which can 100% solve the above problems.
| Comparison Item | WIFI_STA_MODE | FIXED_IP_ADDRESS |
|---|---|---|
| IP Acquisition Method | Automatic (DHCP) | Manual (Static IP) |
| IP Address | Dynamically assigned, may change | Fixed and unchanged |
| Suitable Scenario | Ordinary home/office network | Scenarios requiring fixed IP |
| Configuration Complexity | Simple (only SSID and password) | Medium (IP, gateway, subnet mask needed) |
| Connection Speed | Slightly slower (needs DHCP wait) | Faster (no allocation wait needed) |
IPAddress ip(192, 168, 1, 166); // Fixed IP of the board (customizable, avoid conflict with other devices)
IPAddress gateway(192, 168, 1, 1); // Router gateway (usually 192.168.1.1 or 192.168.0.1)
IPAddress subnet(255, 255, 255, 0); // Subnet mask (usually 255.255.255.0 by default)
Critical Step: Must call WiFi.config(ip, gateway, subnet) first to configure static IP, then call WiFi.begin(ssid, password) to start connection. The order cannot be reversed.
The complete code is located in the FIXED_IP_ADDRESS folder within the same directory.
Note: The static IP must be in the same network segment as the router (e.g., if router gateway is 192.168.0.1, the IP should be changed to 192.168.0.xxx) to avoid IP conflict.
-50 dBm = excellent, -80 dBm = poor, -100 dBm = no connection