Arduino UNO R4 WiFi (hereinafter referred to as UNO R4 WiFi) has updated its WiFi functionality, using the ESP32-S3 WiFi module for this upgrade. This module, launched by Espressif Systems, is a WiFi and Bluetooth module that enables remote control and monitoring of devices and data exchange with other devices.
Next, let’s implement the updated WiFi functionality!
By establishing an AP, we can remotely control the UNO R4 WiFi LED switch.
Select the development board.
Select the corresponding COM port.
Click on the example program.
Select the example from the WiFiS3 category, then click on AP_SimpleWebServer. A new window will pop up displaying the example program file, and you will see the following program:
/*
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web.
This sketch will create a new access point (with no password).
It will then launch a new server and print out the IP address to the Serial Monitor. From there, you can open that address in a web browser to turn on and off the LED on pin 13.
If the IP address of your board is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off
created 25 Nov 2012
by Tom Igoe
adapted to WiFi AP by Adafruit
Find the full UNO R4 WiFi Network documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#access-point
*/
#include “WiFiS3.h”
#include “arduino_secrets.h”
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int led = LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println(“Access Point Web Server”);
pinMode(led, OUTPUT); // set the LED pin mode
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println(“Communication with WiFi module failed!”);
// don’t continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println(“Please upgrade the firmware”);
}
// by default the local IP address will be 192.168.4.1
// you can override it with the following:
WiFi.config(IPAddress(192,48,56,2));
// print the network name (SSID);
Serial.print(“Creating access point named: “);
Serial.println(ssid);
// Create open network. Change this line if you want to create a WEP network:
status = WiFi.beginAP(ssid, pass);
if (status != WL_AP_LISTENING) {
Serial.println(“Creating access point failed”);
// don’t continue
while (true);
}
// wait 10 seconds for connection:
delay(10000);
// start the web server on port 80
server.begin();
// you’re connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println(“Device connected to AP”);
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println(“Device disconnected from AP”);
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println(“new client”); // print a message out the serial port
String currentLine = “”; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client’s connected
delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect – otherwise it will loop so fast that SPI will never be served.
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out to the serial monitor
if (c == ‘\n’) { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that’s the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what’s coming, then a blank line:
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-type:text/html”);
client.println();
// the content of the HTTP response follows the header:
client.print(“
Click here to turn the LED on
“);
client.print(“
Click here to turn the LED off
“);
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = “”;
}
}
else if (c != ‘\r’) { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was “GET /H” or “GET /L”:
if (currentLine.endsWith(“GET /H”)) {
digitalWrite(led, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith(“GET /L”)) {
digitalWrite(led, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println(“client disconnected”);
}
}
void printWiFiStatus() {
// print the SSID of the network you’re attached to:
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);
// print where to go in a browser:
Serial.print(“To see this page in action, open a browser to http://”);
Serial.println(ip);
}
Note that you need to change the text after the “=” sign in this code segment to the network name and password you want to create. If there are special characters like:!,@,#,$,%,^,&,*, etc., just add double quotes or single quotes and it will run normally!
char ssid[] = SECRET_SSID;// your network SSID (name)
char pass[] = SECRET_PASS;// your network password (use for WPA, or use as key for WEP)
Click the arrow to upload.
Result Acceptance!
When the burning is complete, a message like this will appear:
Then click Serial Monitor to see the output message:
Device disconnected from AP
This indicates that your device (Arduino Uno R4 WiFi) has disconnected from the access point (Access Point, AP). This may be due to a disconnection, poor connection quality, or the device disconnecting itself. You can press the reset button on the Arduino to try to reconnect.
Access Point Web Server
indicates that the Arduino Uno R4 WiFi is running in Access Point (AP) mode and is a Web server that can interact through a webpage.
Please upgrade the firmware
is used to check the firmware version of the WiFi module. If the firmware version is outdated, it will prompt you to upgrade the firmware. However, for the Arduino Uno R4 WiFi, it usually does not require manual firmware upgrades, as the correct and operational version is pre-burned at the factory.
Creating access point named: ” “
indicates that the Arduino Uno R4 WiFi has created an Access Point named ” “.
SSID: #####
displays the SSID (Service Set Identifier, WiFi name) of your WiFi network.
IP Address: 192.48.56.2
This is the IP address of the Access Point. In the program, the Access Point’s IP address is set to 192.48.56.2.
To see this page in action, open a browser to <http://192.48.56.2>
This is a prompt telling you to open a web browser and enter http://192.48.56.2 to use the webpage provided by the Arduino Uno R4 WiFi.
💡Note that the UNO R4 WiFi is running in Access Point (AP) mode. In this mode, the UNO R4 WiFi will establish its own WiFi network, and the devices that need to communicate must connect to the same WiFi to communicate with the Arduino.
So next, connect your computer to the network established by the UNO R4 WiFi! If you successfully connect to the webpage, you will see a page like this.
Next, you can remotely control the built-in LED of the Arduino!
By establishing a WebServer, we can remotely control the UNO R4 WiFi LED switch.
Before the actual operation, I want to help readers understand Access Point and Web Server. In the previous example, we learned that an Access Point can establish a point for users to connect, while a Web Server is a software or hardware device that provides web content.
💡Access Point and Web Server
In some cases, these two may be combined. For example, when a user connects to a wireless router (which has AP functionality), the router may internally run a Web Server, allowing users to configure the router settings through a webpage.
So the two are different concepts under Wi-Fi technology, playing different roles in wireless networks.
In the next implementation, we will use an external Wi-Fi router as a Web Server, connecting both the UNO R4 WiFi and the computer to that network for communication, similarly to control the LED switch.
Open the example program.
Again, through the example file, find WiFiWebServer in the WiFiS3 category.
After opening, a new example program file will be displayed.
Rewrite the program code.
You can compare the program content that controls the LED switch through the AP method above. We need to add the following content:
LED variable declaration.
int led = LED_BUILTIN;
LED initialization in the setup() function:
pinMode(led, OUTPUT);// set the LED pin mode
Set variable currentLine.
String currentLine = “”;
HTML button generation:
LED control in the loop() function:
if (currentLine.endsWith(“GET /H”)) {
digitalWrite(led, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith(“GET /L”)) {
digitalWrite(led, LOW); // GET /L turns the LED off
}
After modifying, click the upload button to burn the program code into the Arduino UNO R4 WiFi. After completion, you can click SerialPort to view the output message:
Copy the IP address and start the browser, paste the IP address into the web search bar to access that webpage. You will see the site output some simulated signals and two buttons to control the LED switch. Readers can try to see if they can control the LED light on the UNO R4 WiFi!
Conclusion
In this article, we used two common Wi-Fi technologies, Access Point and Web Server, to experience the new experience brought by the UNO R4 WiFi. During testing, I found that although the network connection is quick, it seems less stable than using the ESP32 development board’s Wi-Fi functionality, requiring constant monitoring for disconnections. However, aside from that, the major update of the UNO R4 WiFi with the WiFiS3 suite has truly opened up more creative possibilities for many Arduino enthusiasts!
================================
Leave a Comment
Your email address will not be published. Required fields are marked *