UNO R4 WiFi Network Examples

Discover examples compatible with the WiFi library included in the UNO R4 Board Package.

The Arduino UNO R4 WiFi has a built in ESP32-S3 module that enables you to connect to Wi-Fi® networks, and perform network operations. Protocols including HTTPS, MQTT, UDP are tested and supported, and in this article, you will find a number of examples that will get you started.

Wi-Fi® support is enabled via the built-in

WiFiS3
library that is shipped with the Arduino UNO R4 Board Package. Installing the Board Package automatically installs the
WiFiS3
library.

The easiest way to connect your board to the Internet is via the Arduino Cloud platform. Here you can configure, program, monitor and synchronize your devices without having to write any networking code.

Hardware & Software Needed

Examples

Examples listed in this section have been tested and verified to work. Most examples require you to input the

SSID
and
PASSWORD
for your local Wi-Fi® network. As a standard practice, in all of our examples, we store this in a separate header file (called
arduino_secrets.h
).

You will need to create this file, or remove the

#include "arduino_secrets.h"
file at the top of each example. The file should contain:

1//arduino_secrets.h header file
2#define SECRET_SSID "yournetwork"
3#define SECRET_PASS "yourpassword"

Storing your network & password in a separate file minimizes the risk of you accidentally sharing your Wi-Fi® credentials.

Access Point

Code Source on Github
1/*
2 WiFi Web Server LED Blink
3
4 A simple web server that lets you blink an LED via the web.
5 This sketch will create a new access point (with no password).
6 It will then launch a new server and print out the IP address
7 to the Serial Monitor. From there, you can open that address in a web browser
8 to turn on and off the LED on pin 13.
9
10 If the IP address of your board is yourAddress:
11 http://yourAddress/H turns the LED on
12 http://yourAddress/L turns it off
13
14 created 25 Nov 2012
15 by Tom Igoe
16 adapted to WiFi AP by Adafruit
17
18 Find the full UNO R4 WiFi Network documentation here:
19 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#access-point
20 */
21
22#include "WiFiS3.h"
23
24#include "arduino_secrets.h"
25
26///////please enter your sensitive data in the Secret tab/arduino_secrets.h
27char ssid[] = SECRET_SSID; // your network SSID (name)
28char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
29int keyIndex = 0; // your network key index number (needed only for WEP)
30
31int led = LED_BUILTIN;
32int status = WL_IDLE_STATUS;
33WiFiServer server(80);
34
35void setup() {
36 //Initialize serial and wait for port to open:
37 Serial.begin(9600);
38 while (!Serial) {
39 ; // wait for serial port to connect. Needed for native USB port only
40 }
41 Serial.println("Access Point Web Server");
42
43 pinMode(led, OUTPUT); // set the LED pin mode
44
45 // check for the WiFi module:
46 if (WiFi.status() == WL_NO_MODULE) {
47 Serial.println("Communication with WiFi module failed!");
48 // don't continue
49 while (true);
50 }
51
52 String fv = WiFi.firmwareVersion();
53 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
54 Serial.println("Please upgrade the firmware");
55 }
56
57 // by default the local IP address will be 192.168.4.1
58 // you can override it with the following:
59 WiFi.config(IPAddress(192,48,56,2));
60
61 // print the network name (SSID);
62 Serial.print("Creating access point named: ");
63 Serial.println(ssid);
64
65 // Create open network. Change this line if you want to create an WEP network:
66 status = WiFi.beginAP(ssid, pass);
67 if (status != WL_AP_LISTENING) {
68 Serial.println("Creating access point failed");
69 // don't continue
70 while (true);
71 }
72
73 // wait 10 seconds for connection:
74 delay(10000);
75
76 // start the web server on port 80
77 server.begin();
78
79 // you're connected now, so print out the status
80 printWiFiStatus();
81}
82
83
84void loop() {
85
86 // compare the previous status to the current status
87 if (status != WiFi.status()) {
88 // it has changed update the variable
89 status = WiFi.status();
90
91 if (status == WL_AP_CONNECTED) {
92 // a device has connected to the AP
93 Serial.println("Device connected to AP");
94 } else {
95 // a device has disconnected from the AP, and we are back in listening mode
96 Serial.println("Device disconnected from AP");
97 }
98 }
99
100 WiFiClient client = server.available(); // listen for incoming clients
101
102 if (client) { // if you get a client,
103 Serial.println("new client"); // print a message out the serial port
104 String currentLine = ""; // make a String to hold incoming data from the client
105 while (client.connected()) { // loop while the client's connected
106 delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
107 if (client.available()) { // if there's bytes to read from the client,
108 char c = client.read(); // read a byte, then
109 Serial.write(c); // print it out to the serial monitor
110 if (c == '\n') { // if the byte is a newline character
111
112 // if the current line is blank, you got two newline characters in a row.
113 // that's the end of the client HTTP request, so send a response:
114 if (currentLine.length() == 0) {
115 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
116 // and a content-type so the client knows what's coming, then a blank line:
117 client.println("HTTP/1.1 200 OK");
118 client.println("Content-type:text/html");
119 client.println();
120
121 // the content of the HTTP response follows the header:
122 client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
123 client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
124
125 // The HTTP response ends with another blank line:
126 client.println();
127 // break out of the while loop:
128 break;
129 }
130 else { // if you got a newline, then clear currentLine:
131 currentLine = "";
132 }
133 }
134 else if (c != '\r') { // if you got anything else but a carriage return character,
135 currentLine += c; // add it to the end of the currentLine
136 }
137
138 // Check to see if the client request was "GET /H" or "GET /L":
139 if (currentLine.endsWith("GET /H")) {
140 digitalWrite(led, HIGH); // GET /H turns the LED on
141 }
142 if (currentLine.endsWith("GET /L")) {
143 digitalWrite(led, LOW); // GET /L turns the LED off
144 }
145 }
146 }
147 // close the connection:
148 client.stop();
149 Serial.println("client disconnected");
150 }
151}
152
153void printWiFiStatus() {
154 // print the SSID of the network you're attached to:
155 Serial.print("SSID: ");
156 Serial.println(WiFi.SSID());
157
158 // print your WiFi shield's IP address:
159 IPAddress ip = WiFi.localIP();
160 Serial.print("IP Address: ");
161 Serial.println(ip);
162
163 // print where to go in a browser:
164 Serial.print("To see this page in action, open a browser to http://");
165 Serial.println(ip);
166
167}
168

Connect With WPA

Code Source on Github
1/*
2 This example connects to an unencrypted WiFi network.
3 Then it prints the MAC address of the WiFi module,
4 the IP address obtained, and other network details.
5
6 created 13 July 2010
7 by dlf (Metodo2 srl)
8 modified 31 May 2012
9 by Tom Igoe
10
11 Find the full UNO R4 WiFi Network documentation here:
12 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#connect-with-wpa
13 */
14#include <WiFiS3.h>
15
16#include "arduino_secrets.h"
17///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18char ssid[] = SECRET_SSID; // your network SSID (name)
19char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20int status = WL_IDLE_STATUS; // the WiFi radio's status
21
22void setup() {
23 //Initialize serial and wait for port to open:
24 Serial.begin(9600);
25 while (!Serial) {
26 ; // wait for serial port to connect. Needed for native USB port only
27 }
28
29 // check for the WiFi module:
30 if (WiFi.status() == WL_NO_MODULE) {
31 Serial.println("Communication with WiFi module failed!");
32 // don't continue
33 while (true);
34 }
35
36 String fv = WiFi.firmwareVersion();
37 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
38 Serial.println("Please upgrade the firmware");
39 }
40
41 // attempt to connect to WiFi network:
42 while (status != WL_CONNECTED) {
43 Serial.print("Attempting to connect to WPA SSID: ");
44 Serial.println(ssid);
45 // Connect to WPA/WPA2 network:
46 status = WiFi.begin(ssid, pass);
47
48 // wait 10 seconds for connection:
49 delay(10000);
50 }
51
52 // you're connected now, so print out the data:
53 Serial.print("You're connected to the network");
54 printCurrentNet();
55 printWifiData();
56
57}
58
59void loop() {
60 // check the network connection once every 10 seconds:
61 delay(10000);
62 printCurrentNet();
63}
64
65void printWifiData() {
66 // print your board's IP address:
67 IPAddress ip = WiFi.localIP();
68 Serial.print("IP Address: ");
69
70 Serial.println(ip);
71
72 // print your MAC address:
73 byte mac[6];
74 WiFi.macAddress(mac);
75 Serial.print("MAC address: ");
76 printMacAddress(mac);
77}
78
79void printCurrentNet() {
80 // print the SSID of the network you're attached to:
81 Serial.print("SSID: ");
82 Serial.println(WiFi.SSID());
83
84 // print the MAC address of the router you're attached to:
85 byte bssid[6];
86 WiFi.BSSID(bssid);
87 Serial.print("BSSID: ");
88 printMacAddress(bssid);
89
90 // print the received signal strength:
91 long rssi = WiFi.RSSI();
92 Serial.print("signal strength (RSSI):");
93 Serial.println(rssi);
94
95 // print the encryption type:
96 byte encryption = WiFi.encryptionType();
97 Serial.print("Encryption Type:");
98 Serial.println(encryption, HEX);
99 Serial.println();
100}
101
102void printMacAddress(byte mac[]) {
103 for (int i = 0; i < 6; i++) {
104 if (i > 0) {
105 Serial.print(":");
106 }
107 if (mac[i] < 16) {
108 Serial.print("0");
109 }
110 Serial.print(mac[i], HEX);
111 }
112 Serial.println();
113}
114

Scan Networks

Code Source on Github
1/*
2 This example prints the board's MAC address, and
3 scans for available WiFi networks using the NINA module.
4 Every ten seconds, it scans again. It doesn't actually
5 connect to any network, so no encryption scheme is specified.
6
7 Circuit:
8 * Uno R4 WiFi
9
10 created 13 July 2010
11 by dlf (Metodo2 srl)
12 modified 21 Junn 2012
13 by Tom Igoe and Jaymes Dec
14
15 Find the full UNO R4 WiFi Network documentation here:
16 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#scan-networks
17 */
18
19
20
21#include <WiFiS3.h>
22
23void setup() {
24 //Initialize serial and wait for port to open:
25 Serial.begin(9600);
26 while (!Serial) {
27 ; // wait for serial port to connect. Needed for native USB port only
28 }
29
30 // check for the WiFi module:
31 if (WiFi.status() == WL_NO_MODULE) {
32 Serial.println("Communication with WiFi module failed!");
33 // don't continue
34 while (true);
35 }
36
37 String fv = WiFi.firmwareVersion();
38 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
39 Serial.println("Please upgrade the firmware");
40 }
41}
42
43void loop() {
44 byte mac[6];
45 // scan for existing networks:
46 Serial.println("Scanning available networks...");
47 listNetworks();
48 WiFi.macAddress(mac);
49 Serial.println();
50 Serial.print("Your MAC Address is: ");
51 printMacAddress(mac);
52 delay(10000);
53}
54
55void listNetworks() {
56 // scan for nearby networks:
57 Serial.println("** Scan Networks **");
58 int numSsid = WiFi.scanNetworks();
59 if (numSsid == -1) {
60 Serial.println("Couldn't get a WiFi connection");
61 while (true);
62 }
63
64 // print the list of networks seen:
65 Serial.print("number of available networks:");
66 Serial.println(numSsid);
67
68 // print the network number and name for each network found:
69 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
70 Serial.print(thisNet);
71 Serial.print(") ");
72 Serial.print(WiFi.SSID(thisNet));
73 Serial.print(" Signal: ");
74 Serial.print(WiFi.RSSI(thisNet));
75 Serial.print(" dBm");
76 Serial.print(" Encryption: ");
77 printEncryptionType(WiFi.encryptionType(thisNet));
78 }
79}
80
81void printEncryptionType(int thisType) {
82 // read the encryption type and print out the name:
83 switch (thisType) {
84 case ENC_TYPE_WEP:
85 Serial.println("WEP");
86 break;
87 case ENC_TYPE_WPA:
88 Serial.println("WPA");
89 break;
90 case ENC_TYPE_WPA2:
91 Serial.println("WPA2");
92 break;
93 case ENC_TYPE_WPA3:
94 Serial.println("WPA3");
95 break;
96 case ENC_TYPE_NONE:
97 Serial.println("None");
98 break;
99 case ENC_TYPE_AUTO:
100 Serial.println("Auto");
101 break;
102 case ENC_TYPE_UNKNOWN:
103 default:
104 Serial.println("Unknown");
105 break;
106 }
107}
108
109
110void printMacAddress(byte mac[]) {
111 for (int i = 0; i < 6; i++) {
112 if (i > 0) {
113 Serial.print(":");
114 }
115 if (mac[i] < 16) {
116 Serial.print("0");
117 }
118 Serial.print(mac[i], HEX);
119 }
120 Serial.println();
121}
122

Scan Networks (Advanced)

Code Source on Github
1/*
2 This example prints the board's MAC address, and
3 scans for available WiFi networks using the NINA module.
4 Every ten seconds, it scans again. It doesn't actually
5 connect to any network, so no encryption scheme is specified.
6 BSSID and WiFi channel are printed
7
8 Circuit:
9 * Uno R4 WiFi
10
11 This example is based on ScanNetworks
12
13 created 1 Mar 2017
14 by Arturo Guadalupi
15
16 Find the full UNO R4 WiFi Network documentation here:
17 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#scan-networks-advanced
18*/
19
20
21
22#include <WiFiS3.h>
23
24void setup() {
25 //Initialize serial and wait for port to open:
26 Serial.begin(9600);
27 while (!Serial) {
28 ; // wait for serial port to connect. Needed for native USB port only
29 }
30
31 // check for the WiFi module:
32 if (WiFi.status() == WL_NO_MODULE) {
33 Serial.println("Communication with WiFi module failed!");
34 // don't continue
35 while (true);
36 }
37
38 String fv = WiFi.firmwareVersion();
39 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
40 Serial.println("Please upgrade the firmware");
41 }
42
43 // scan for existing networks:
44 Serial.println();
45 Serial.println("Scanning available networks...");
46 listNetworks();
47
48 // print your MAC address:
49 byte mac[6];
50 WiFi.macAddress(mac);
51 Serial.print("MAC: ");
52 printMacAddress(mac);
53}
54
55void loop() {
56 delay(10000);
57 // scan for existing networks:
58 Serial.println("Scanning available networks...");
59 listNetworks();
60}
61
62void listNetworks() {
63 // scan for nearby networks:
64 Serial.println("** Scan Networks **");
65 int numSsid = WiFi.scanNetworks();
66 if (numSsid == -1)
67 {
68 Serial.println("Couldn't get a WiFi connection");
69 while (true);
70 }
71
72 // print the list of networks seen:
73 Serial.print("number of available networks: ");
74 Serial.println(numSsid);
75
76 // print the network number and name for each network found:
77 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
78 Serial.print(thisNet + 1);
79 Serial.print(") ");
80 Serial.print("Signal: ");
81 Serial.print(WiFi.RSSI(thisNet));
82 Serial.print(" dBm");
83 Serial.print("\tChannel: ");
84 Serial.print(WiFi.channel(thisNet));
85 byte bssid[6];
86 Serial.print("\t\tBSSID: ");
87 printMacAddress(WiFi.BSSID(thisNet, bssid));
88 Serial.print("\tEncryption: ");
89 printEncryptionType(WiFi.encryptionType(thisNet));
90 Serial.print("\t\tSSID: ");
91 Serial.println(WiFi.SSID(thisNet));
92 Serial.flush();
93 }
94 Serial.println();
95}
96
97void printEncryptionType(int thisType) {
98 // read the encryption type and print out the name:
99 switch (thisType) {
100 case ENC_TYPE_WEP:
101 Serial.print("WEP");
102 break;
103 case ENC_TYPE_WPA:
104 Serial.print("WPA");
105 break;
106 case ENC_TYPE_WPA2:
107 Serial.print("WPA2");
108 break;
109 case ENC_TYPE_WPA3:
110 Serial.print("WPA3");
111 break;
112 case ENC_TYPE_NONE:
113 Serial.print("None");
114 break;
115 case ENC_TYPE_AUTO:
116 Serial.print("Auto");
117 break;
118 case ENC_TYPE_UNKNOWN:
119 default:
120 Serial.print("Unknown");
121 break;
122 }
123}
124
125void print2Digits(byte thisByte) {
126 if (thisByte < 0xF) {
127 Serial.print("0");
128 }
129 Serial.print(thisByte, HEX);
130}
131
132void printMacAddress(byte mac[]) {
133 for (int i = 0; i < 6; i++) {
134 if (i > 0) {
135 Serial.print(":");
136 }
137 if (mac[i] < 16) {
138 Serial.print("0");
139 }
140 Serial.print(mac[i], HEX);
141 }
142 Serial.println();
143}
144

Simple Webserver

Code Source on Github
1/*
2 WiFi Web Server LED Blink
3
4 A simple web server that lets you blink an LED via the web.
5 This sketch will print the IP address of your WiFi module (once connected)
6 to the Serial Monitor. From there, you can open that address in a web browser
7 to turn on and off the LED_BUILTIN.
8
9 If the IP address of your board is yourAddress:
10 http://yourAddress/H turns the LED on
11 http://yourAddress/L turns it off
12
13 This example is written for a network using WPA encryption. For
14 WEP or WPA, change the WiFi.begin() call accordingly.
15
16 Circuit:
17 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and Uno WiFi Rev.2)
18 * LED attached to pin 9
19
20 created 25 Nov 2012
21 by Tom Igoe
22
23 Find the full UNO R4 WiFi Network documentation here:
24 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#simple-webserver
25 */
26
27#include "WiFiS3.h"
28
29#include "arduino_secrets.h"
30///////please enter your sensitive data in the Secret tab/arduino_secrets.h
31char ssid[] = SECRET_SSID; // your network SSID (name)
32char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
33int keyIndex = 0; // your network key index number (needed only for WEP)
34
35int led = LED_BUILTIN;
36int status = WL_IDLE_STATUS;
37WiFiServer server(80);
38
39void setup() {
40 Serial.begin(9600); // initialize serial communication
41 pinMode(led, OUTPUT); // set the LED pin mode
42
43 // check for the WiFi module:
44 if (WiFi.status() == WL_NO_MODULE) {
45 Serial.println("Communication with WiFi module failed!");
46 // don't continue
47 while (true);
48 }
49
50 String fv = WiFi.firmwareVersion();
51 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
52 Serial.println("Please upgrade the firmware");
53 }
54
55 // attempt to connect to WiFi network:
56 while (status != WL_CONNECTED) {
57 Serial.print("Attempting to connect to Network named: ");
58 Serial.println(ssid); // print the network name (SSID);
59
60 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
61 status = WiFi.begin(ssid, pass);
62 // wait 10 seconds for connection:
63 delay(10000);
64 }
65 server.begin(); // start the web server on port 80
66 printWifiStatus(); // you're connected now, so print out the status
67}
68
69
70void loop() {
71 WiFiClient client = server.available(); // listen for incoming clients
72
73 if (client) { // if you get a client,
74 Serial.println("new client"); // print a message out the serial port
75 String currentLine = ""; // make a String to hold incoming data from the client
76 while (client.connected()) { // loop while the client's connected
77 if (client.available()) { // if there's bytes to read from the client,
78 char c = client.read(); // read a byte, then
79 Serial.write(c); // print it out to the serial monitor
80 if (c == '\n') { // if the byte is a newline character
81
82 // if the current line is blank, you got two newline characters in a row.
83 // that's the end of the client HTTP request, so send a response:
84 if (currentLine.length() == 0) {
85 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
86 // and a content-type so the client knows what's coming, then a blank line:
87 client.println("HTTP/1.1 200 OK");
88 client.println("Content-type:text/html");
89 client.println();
90
91 // the content of the HTTP response follows the header:
92 client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
93 client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
94
95 // The HTTP response ends with another blank line:
96 client.println();
97 // break out of the while loop:
98 break;
99 } else { // if you got a newline, then clear currentLine:
100 currentLine = "";
101 }
102 } else if (c != '\r') { // if you got anything else but a carriage return character,
103 currentLine += c; // add it to the end of the currentLine
104 }
105
106 // Check to see if the client request was "GET /H" or "GET /L":
107 if (currentLine.endsWith("GET /H")) {
108 digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
109 }
110 if (currentLine.endsWith("GET /L")) {
111 digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
112 }
113 }
114
115 }
116 // close the connection:
117 client.stop();
118 Serial.println("client disconnected");
119 }
120}
121
122void printWifiStatus() {
123 // print the SSID of the network you're attached to:
124 Serial.print("SSID: ");
125 Serial.println(WiFi.SSID());
126
127 // print your board's IP address:
128 IPAddress ip = WiFi.localIP();
129 Serial.print("IP Address: ");
130 Serial.println(ip);
131
132 // print the received signal strength:
133 long rssi = WiFi.RSSI();
134 Serial.print("signal strength (RSSI):");
135 Serial.print(rssi);
136 Serial.println(" dBm");
137 // print where to go in a browser:
138 Serial.print("To see this page in action, open a browser to http://");
139 Serial.println(ip);
140}
141

Wi-Fi® Chat Server

Code Source on Github
1/*
2 Chat Server
3
4 A simple server that distributes any incoming messages to all
5 connected clients. To use, telnet to your device's IP address and type.
6 You can see the client's input in the serial monitor as well.
7
8 This example is written for a network using WPA encryption. For
9 WEP or WPA, change the WiFi.begin() call accordingly.
10
11
12 Circuit:
13 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and Uno WiFi Rev.2)
14
15 created 18 Dec 2009
16 by David A. Mellis
17 modified 31 May 2012
18 by Tom Igoe
19
20 Find the full UNO R4 WiFi Network documentation here:
21 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-chat-server
22 */
23
24
25#include <WiFiS3.h>
26
27#include "arduino_secrets.h"
28///////please enter your sensitive data in the Secret tab/arduino_secrets.h
29char ssid[] = SECRET_SSID; // your network SSID (name)
30char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
31
32int keyIndex = 0; // your network key index number (needed only for WEP)
33
34int status = WL_IDLE_STATUS;
35
36WiFiServer server(23);
37
38boolean alreadyConnected = false; // whether or not the client was connected previously
39
40void setup() {
41 //Initialize serial and wait for port to open:
42 Serial.begin(9600);
43 while (!Serial) {
44 ; // wait for serial port to connect. Needed for native USB port only
45 }
46
47 // check for the WiFi module:
48 if (WiFi.status() == WL_NO_MODULE) {
49 Serial.println("Communication with WiFi module failed!");
50 // don't continue
51 while (true);
52 }
53
54 String fv = WiFi.firmwareVersion();
55 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
56 Serial.println("Please upgrade the firmware");
57 }
58
59 // attempt to connect to WiFi network:
60 while (status != WL_CONNECTED) {
61 Serial.print("Attempting to connect to SSID: ");
62 Serial.println(ssid);
63 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
64 status = WiFi.begin(ssid, pass);
65
66 // wait 10 seconds for connection:
67 delay(10000);
68 }
69
70 // start the server:
71 server.begin();
72 // you're connected now, so print out the status:
73 printWifiStatus();
74}
75
76
77void loop() {
78
79 // wait for a new client:
80 WiFiClient client = server.available();
81
82
83 // when the client sends the first byte, say hello:
84 if (client) {
85 if (!alreadyConnected) {
86 // clear out the input buffer:
87 client.flush();
88 Serial.println("We have a new client");
89 client.println("Hello, client!");
90 alreadyConnected = true;
91 }
92
93 if (client.available() > 0) {
94 // read the bytes incoming from the client:
95 char thisChar = client.read();
96 // echo the bytes back to the client:
97 server.write(thisChar);
98 // echo the bytes to the server as well:
99 Serial.write(thisChar);
100 }
101 }
102}
103
104
105void printWifiStatus() {
106 // print the SSID of the network you're attached to:
107 Serial.print("SSID: ");
108 Serial.println(WiFi.SSID());
109
110 // print your board's IP address:
111 IPAddress ip = WiFi.localIP();
112 Serial.print("IP Address: ");
113 Serial.println(ip);
114
115 // print the received signal strength:
116 long rssi = WiFi.RSSI();
117 Serial.print("signal strength (RSSI):");
118 Serial.print(rssi);
119 Serial.println(" dBm");
120}
121

Wi-Fi® UDP NTP Client

Code Source on Github
1/*
2 Udp NTP Client
3
4 Get the time from a Network Time Protocol (NTP) time server
5 Demonstrates use of UDP sendPacket and ReceivePacket
6 For more on NTP time servers and the messages needed to communicate with them,
7 see http://en.wikipedia.org/wiki/Network_Time_Protocol
8
9 created 4 Sep 2010
10 by Michael Margolis
11 modified 9 Apr 2012
12 by Tom Igoe
13 modified May, 4th 2023
14 by Daniele Aimo
15
16 This code is in the public domain.
17
18 Find the full UNO R4 WiFi Network documentation here:
19 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-udp-ntp-client
20 */
21
22
23#include <WiFiS3.h>
24
25int status = WL_IDLE_STATUS;
26#include "arduino_secrets.h"
27///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28char ssid[] = SECRET_SSID; // your network SSID (name)
29char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30int keyIndex = 0; // your network key index number (needed only for WEP)
31
32unsigned int localPort = 2390; // local port to listen for UDP packets
33
34IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
35
36const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
37
38byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
39
40// A UDP instance to let us send and receive packets over UDP
41WiFiUDP Udp;
42
43void setup() {
44 // Open serial communications and wait for port to open:
45 Serial.begin(9600);
46 while (!Serial) {
47 ; // wait for serial port to connect. Needed for native USB port only
48 }
49
50 // check for the WiFi module:
51 if (WiFi.status() == WL_NO_MODULE) {
52 Serial.println("Communication with WiFi module failed!");
53 // don't continue
54 while (true);
55 }
56
57 String fv = WiFi.firmwareVersion();
58 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
59 Serial.println("Please upgrade the firmware");
60 }
61
62 // attempt to connect to WiFi network:
63 while (status != WL_CONNECTED) {
64 Serial.print("Attempting to connect to SSID: ");
65 Serial.println(ssid);
66 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
67 status = WiFi.begin(ssid, pass);
68
69 // wait 10 seconds for connection:
70 delay(10000);
71 }
72
73 Serial.println("Connected to WiFi");
74 printWifiStatus();
75
76 Serial.println("\nStarting connection to server...");
77 Udp.begin(localPort);
78}
79
80void loop() {
81 sendNTPpacket(timeServer); // send an NTP packet to a time server
82 // wait to see if a reply is available
83 delay(1000);
84 if (Udp.parsePacket()) {
85 Serial.println("packet received");
86 // We've received a packet, read the data from it
87 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
88
89 //the timestamp starts at byte 40 of the received packet and is four bytes,
90 // or two words, long. First, extract the two words:
91
92 unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
93 unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
94 // combine the four bytes (two words) into a long integer
95 // this is NTP time (seconds since Jan 1 1900):
96 unsigned long secsSince1900 = highWord << 16 | lowWord;
97 Serial.print("Seconds since Jan 1 1900 = ");
98 Serial.println(secsSince1900);
99
100 // now convert NTP time into everyday time:
101 Serial.print("Unix time = ");
102 // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
103 const unsigned long seventyYears = 2208988800UL;
104 // subtract seventy years:
105 unsigned long epoch = secsSince1900 - seventyYears;
106 // print Unix time:
107 Serial.println(epoch);
108
109
110 // print the hour, minute and second:
111 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
112 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
113 Serial.print(':');
114 if (((epoch % 3600) / 60) < 10) {
115 // In the first 10 minutes of each hour, we'll want a leading '0'
116 Serial.print('0');
117 }
118 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
119 Serial.print(':');
120 if ((epoch % 60) < 10) {
121 // In the first 10 seconds of each minute, we'll want a leading '0'
122 Serial.print('0');
123 }
124 Serial.println(epoch % 60); // print the second
125 }
126 // wait ten seconds before asking for the time again
127 delay(10000);
128}
129
130// send an NTP request to the time server at the given address
131unsigned long sendNTPpacket(IPAddress& address) {
132 //Serial.println("1");
133 // set all bytes in the buffer to 0
134 memset(packetBuffer, 0, NTP_PACKET_SIZE);
135 // Initialize values needed to form NTP request
136 // (see URL above for details on the packets)
137 //Serial.println("2");
138 packetBuffer[0] = 0b11100011; // LI, Version, Mode
139 packetBuffer[1] = 0; // Stratum, or type of clock
140 packetBuffer[2] = 6; // Polling Interval
141 packetBuffer[3] = 0xEC; // Peer Clock Precision
142 // 8 bytes of zero for Root Delay & Root Dispersion
143 packetBuffer[12] = 49;
144 packetBuffer[13] = 0x4E;
145 packetBuffer[14] = 49;
146 packetBuffer[15] = 52;
147
148 //Serial.println("3");
149
150 // all NTP fields have been given values, now
151 // you can send a packet requesting a timestamp:
152 Udp.beginPacket(address, 123); //NTP requests are to port 123
153 //Serial.println("4");
154 Udp.write(packetBuffer, NTP_PACKET_SIZE);
155 //Serial.println("5");
156 Udp.endPacket();
157 //Serial.println("6");
158}
159
160
161void printWifiStatus() {
162 // print the SSID of the network you're attached to:
163 Serial.print("SSID: ");
164 Serial.println(WiFi.SSID());
165
166 // print your board's IP address:
167 IPAddress ip = WiFi.localIP();
168 Serial.print("IP Address: ");
169 Serial.println(ip);
170
171 // print the received signal strength:
172 long rssi = WiFi.RSSI();
173 Serial.print("signal strength (RSSI):");
174 Serial.print(rssi);
175 Serial.println(" dBm");
176}
177

Wi-Fi® UDP Send Receive String

Code Source on Github
1/*
2 WiFi UDP Send and Receive String
3
4 This sketch waits for a UDP packet on localPort using the WiFi module.
5 When a packet is received an Acknowledge packet is sent to the client on port remotePort
6
7 created 30 December 2012
8 by dlf (Metodo2 srl)
9
10 Find the full UNO R4 WiFi Network documentation here:
11 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-udp-send-receive-string
12 */
13
14
15#include <WiFiS3.h>
16
17int status = WL_IDLE_STATUS;
18#include "arduino_secrets.h"
19///////please enter your sensitive data in the Secret tab/arduino_secrets.h
20char ssid[] = SECRET_SSID; // your network SSID (name)
21char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
22int keyIndex = 0; // your network key index number (needed only for WEP)
23
24unsigned int localPort = 2390; // local port to listen on
25
26char packetBuffer[256]; //buffer to hold incoming packet
27char ReplyBuffer[] = "acknowledged\n"; // a string to send back
28
29WiFiUDP Udp;
30
31void setup() {
32 //Initialize serial and wait for port to open:
33 Serial.begin(9600);
34 while (!Serial) {
35 ; // wait for serial port to connect. Needed for native USB port only
36 }
37
38 // check for the WiFi module:
39 if (WiFi.status() == WL_NO_MODULE) {
40 Serial.println("Communication with WiFi module failed!");
41 // don't continue
42 while (true);
43 }
44
45 String fv = WiFi.firmwareVersion();
46 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
47 Serial.println("Please upgrade the firmware");
48 }
49
50 // attempt to connect to WiFi network:
51 while (status != WL_CONNECTED) {
52 Serial.print("Attempting to connect to SSID: ");
53 Serial.println(ssid);
54 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
55 status = WiFi.begin(ssid, pass);
56
57 // wait 10 seconds for connection:
58 delay(10000);
59 }
60 Serial.println("Connected to WiFi");
61 printWifiStatus();
62
63 Serial.println("\nStarting connection to server...");
64 // if you get a connection, report back via serial:
65 Udp.begin(localPort);
66}
67
68void loop() {
69
70 // if there's data available, read a packet
71 int packetSize = Udp.parsePacket();
72 if (packetSize) {
73 Serial.print("Received packet of size ");
74 Serial.println(packetSize);
75 Serial.print("From ");
76 IPAddress remoteIp = Udp.remoteIP();
77 Serial.print(remoteIp);
78 Serial.print(", port ");
79 Serial.println(Udp.remotePort());
80
81 // read the packet into packetBuffer
82 int len = Udp.read(packetBuffer, 255);
83 if (len > 0) {
84 packetBuffer[len] = 0;
85 }
86 Serial.println("Contents:");
87 Serial.println(packetBuffer);
88
89 // send a reply, to the IP address and port that sent us the packet we received
90 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
91 Udp.write(ReplyBuffer);
92 Udp.endPacket();
93 }
94}
95
96
97void printWifiStatus() {
98 // print the SSID of the network you're attached to:
99 Serial.print("SSID: ");
100 Serial.println(WiFi.SSID());
101
102 // print your board's IP address:
103 IPAddress ip = WiFi.localIP();
104 Serial.print("IP Address: ");
105 Serial.println(ip);
106
107 // print the received signal strength:
108 long rssi = WiFi.RSSI();
109 Serial.print("signal strength (RSSI):");
110 Serial.print(rssi);
111 Serial.println(" dBm");
112}
113

Wi-Fi® Web Client

Code Source on Github
1/*
2 Web client
3
4 This sketch connects to a website (http://www.google.com)
5 using the WiFi module.
6
7 This example is written for a network using WPA encryption. For
8 WEP or WPA, change the WiFi.begin() call accordingly.
9
10 This example is written for a network using WPA encryption. For
11 WEP or WPA, change the WiFi.begin() call accordingly.
12
13
14 created 13 July 2010
15 by dlf (Metodo2 srl)
16 modified 31 May 2012
17 by Tom Igoe
18
19 Find the full UNO R4 WiFi Network documentation here:
20 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-client
21 */
22
23
24#include "WiFiS3.h"
25
26
27#include "arduino_secrets.h"
28
29///////please enter your sensitive data in the Secret tab/arduino_secrets.h
30char ssid[] = SECRET_SSID; // your network SSID (name)
31char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
32int keyIndex = 0; // your network key index number (needed only for WEP)
33
34int status = WL_IDLE_STATUS;
35// if you don't want to use DNS (and reduce your sketch size)
36// use the numeric IP instead of the name for the server:
37//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
38char server[] = "www.google.com"; // name address for Google (using DNS)
39
40// Initialize the Ethernet client library
41// with the IP address and port of the server
42// that you want to connect to (port 80 is default for HTTP):
43WiFiClient client;
44
45/* -------------------------------------------------------------------------- */
46void setup() {
47/* -------------------------------------------------------------------------- */
48 //Initialize serial and wait for port to open:
49 Serial.begin(9600);
50 while (!Serial) {
51 ; // wait for serial port to connect. Needed for native USB port only
52 }
53
54 // check for the WiFi module:
55 if (WiFi.status() == WL_NO_MODULE) {
56 Serial.println("Communication with WiFi module failed!");
57 // don't continue
58 while (true);
59 }
60
61 String fv = WiFi.firmwareVersion();
62 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
63 Serial.println("Please upgrade the firmware");
64 }
65
66 // attempt to connect to WiFi network:
67 while (status != WL_CONNECTED) {
68 Serial.print("Attempting to connect to SSID: ");
69 Serial.println(ssid);
70 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
71 status = WiFi.begin(ssid, pass);
72
73 // wait 10 seconds for connection:
74 delay(10000);
75 }
76
77 printWifiStatus();
78
79 Serial.println("\nStarting connection to server...");
80 // if you get a connection, report back via serial:
81 if (client.connect(server, 80)) {
82 Serial.println("connected to server");
83 // Make a HTTP request:
84 client.println("GET /search?q=arduino HTTP/1.1");
85 client.println("Host: www.google.com");
86 client.println("Connection: close");
87 client.println();
88 }
89}
90
91/* just wrap the received data up to 80 columns in the serial print*/
92/* -------------------------------------------------------------------------- */
93void read_response() {
94/* -------------------------------------------------------------------------- */
95 uint32_t received_data_num = 0;
96 while (client.available()) {
97 /* actual data reception */
98 char c = client.read();
99 /* print data to serial port */
100 Serial.print(c);
101 /* wrap data to 80 columns*/
102 received_data_num++;
103 if(received_data_num % 80 == 0) {
104 Serial.println();
105 }
106 }
107}
108
109/* -------------------------------------------------------------------------- */
110void loop() {
111/* -------------------------------------------------------------------------- */
112 read_response();
113
114 // if the server's disconnected, stop the client:
115 if (!client.connected()) {
116 Serial.println();
117 Serial.println("disconnecting from server.");
118 client.stop();
119
120 // do nothing forevermore:
121 while (true);
122 }
123}
124
125/* -------------------------------------------------------------------------- */
126void printWifiStatus() {
127/* -------------------------------------------------------------------------- */
128 // print the SSID of the network you're attached to:
129 Serial.print("SSID: ");
130 Serial.println(WiFi.SSID());
131
132 // print your board's IP address:
133 IPAddress ip = WiFi.localIP();
134 Serial.print("IP Address: ");
135 Serial.println(ip);
136
137 // print the received signal strength:
138 long rssi = WiFi.RSSI();
139 Serial.print("signal strength (RSSI):");
140 Serial.print(rssi);
141 Serial.println(" dBm");
142}
143

Wi-Fi® Web Client Repeating

Code Source on Github
1/*
2 Repeating WiFi Web Client
3
4 This sketch connects to a a web server and makes a request
5 using a WiFi equipped Arduino board.
6
7 created 23 April 2012
8 modified 31 May 2012
9 by Tom Igoe
10 modified 13 Jan 2014
11 by Federico Vanzati
12
13 This code is in the public domain.
14
15 Find the full UNO R4 WiFi Network documentation here:
16 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-client-repeating
17 */
18
19#include "WiFiS3.h"
20
21
22#include "arduino_secrets.h"
23///////please enter your sensitive data in the Secret tab/arduino_secrets.h
24char ssid[] = SECRET_SSID; // your network SSID (name)
25char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
26int keyIndex = 0; // your network key index number (needed only for WEP)
27
28int status = WL_IDLE_STATUS;
29
30// Initialize the WiFi client library
31WiFiClient client;
32
33// server address:
34char server[] = "example.org";
35//IPAddress server(64,131,82,241);
36
37unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
38const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
39
40/* -------------------------------------------------------------------------- */
41void setup() {
42/* -------------------------------------------------------------------------- */
43 //Initialize serial and wait for port to open:
44 Serial.begin(9600);
45 while (!Serial) {
46 ; // wait for serial port to connect. Needed for native USB port only
47 }
48
49 // check for the WiFi module:
50 if (WiFi.status() == WL_NO_MODULE) {
51 Serial.println("Communication with WiFi module failed!");
52 // don't continue
53 while (true);
54 }
55
56 String fv = WiFi.firmwareVersion();
57 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
58 Serial.println("Please upgrade the firmware");
59 }
60
61 // attempt to connect to WiFi network:
62 while (status != WL_CONNECTED) {
63 Serial.print("Attempting to connect to SSID: ");
64 Serial.println(ssid);
65 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
66 status = WiFi.begin(ssid, pass);
67
68 // wait 10 seconds for connection:
69 //delay(10000);
70 }
71 // you're connected now, so print out the status:
72 printWifiStatus();
73}
74
75/* just wrap the received data up to 80 columns in the serial print*/
76/* -------------------------------------------------------------------------- */
77void read_request() {
78/* -------------------------------------------------------------------------- */
79 uint32_t received_data_num = 0;
80
81 while (client.available()) {
82 /* actual data reception */
83 char c = client.read();
84 /* print data to serial port */
85 Serial.print(c);
86 /* wrap data to 80 columns*/
87 received_data_num++;
88 if(received_data_num % 80 == 0) {
89
90 }
91
92 }
93}
94
95/* -------------------------------------------------------------------------- */
96void loop() {
97/* -------------------------------------------------------------------------- */
98 // if there's incoming data from the net connection.
99 // send it out the serial port. This is for debugging
100 // purposes only:
101 read_request();
102
103 // if ten seconds have passed since your last connection,
104 // then connect again and send data:
105 if (millis() - lastConnectionTime > postingInterval) {
106 httpRequest();
107 }
108
109}
110
111// this method makes a HTTP connection to the server:
112/* -------------------------------------------------------------------------- */
113void httpRequest() {
114/* -------------------------------------------------------------------------- */
115 // close any connection before send a new request.
116 // This will free the socket on the NINA module
117 client.stop();
118
119 // if there's a successful connection:
120 if (client.connect(server, 80)) {
121 Serial.println("connecting...");
122 // send the HTTP GET request:
123 client.println("GET / HTTP/1.1");
124 client.println("Host: example.org");
125 client.println("User-Agent: ArduinoWiFi/1.1");
126 client.println("Connection: close");
127 client.println();
128 // note the time that the connection was made:
129 lastConnectionTime = millis();
130 } else {
131 // if you couldn't make a connection:
132 Serial.println("connection failed");
133 }
134}
135
136/* -------------------------------------------------------------------------- */
137void printWifiStatus() {
138/* -------------------------------------------------------------------------- */
139 // print the SSID of the network you're attached to:
140 Serial.print("SSID: ");
141 Serial.println(WiFi.SSID());
142
143 // print your board's IP address:
144 IPAddress ip = WiFi.localIP();
145 Serial.print("IP Address: ");
146 Serial.println(ip);
147
148 // print the received signal strength:
149 long rssi = WiFi.RSSI();
150 Serial.print("signal strength (RSSI):");
151 Serial.print(rssi);
152 Serial.println(" dBm");
153}
154

Wi-Fi® Web Client SSL

Code Source on Github
1/*
2 TLS WiFi Web client
3
4 Board CA Root certificate bundle is embedded inside WiFi firmware:
5 https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/main/certificates/cacrt_all.pem
6
7 Find the full UNO R4 WiFi Network documentation here:
8 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-client-ssl
9*/
10
11#include "WiFiS3.h"
12#include "WiFiSSLClient.h"
13#include "IPAddress.h"
14
15#include "arduino_secrets.h"
16
17///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18char ssid[] = SECRET_SSID; // your network SSID (name)
19char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20
21int status = WL_IDLE_STATUS;
22// if you don't want to use DNS (and reduce your sketch size)
23// use the numeric IP instead of the name for the server:
24//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
25char server[] = "www.google.com"; // name address for Google (using DNS)
26
27// Initialize the Ethernet client library
28// with the IP address and port of the server
29// that you want to connect to (port 80 is default for HTTP):
30WiFiSSLClient client;
31
32/* -------------------------------------------------------------------------- */
33void setup() {
34/* -------------------------------------------------------------------------- */
35 //Initialize serial and wait for port to open:
36 Serial.begin(115200);
37 while (!Serial) {
38 ; // wait for serial port to connect. Needed for native USB port only
39 }
40
41 // check for the WiFi module:
42 if (WiFi.status() == WL_NO_MODULE) {
43 Serial.println("Communication with WiFi module failed!");
44 // don't continue
45 while (true);
46 }
47
48 String fv = WiFi.firmwareVersion();
49 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
50 Serial.println("Please upgrade the firmware");
51 }
52
53 // attempt to connect to WiFi network:
54 while (status != WL_CONNECTED) {
55 Serial.print("Attempting to connect to SSID: ");
56 Serial.println(ssid);
57 // Connect to WPA/WPA2 network.
58 status = WiFi.begin(ssid, pass);
59
60 // wait 10 seconds for connection:
61 delay(10000);
62 }
63
64 printWifiStatus();
65
66 Serial.println("\nStarting connection to server...");
67 // if you get a connection, report back via serial:
68
69 if (client.connect(server, 443)) {
70 Serial.println("connected to server");
71 // Make a HTTP request:
72 client.println("GET / HTTP/1.1");
73 client.println("Host: www.google.com");
74 client.println("Connection: close");
75 client.println();
76 }
77}
78
79/* just wrap the received data up to 80 columns in the serial print*/
80/* -------------------------------------------------------------------------- */
81void read_response() {
82/* -------------------------------------------------------------------------- */
83 uint32_t received_data_num = 0;
84 while (client.available()) {
85 /* actual data reception */
86 char c = client.read();
87 /* print data to serial port */
88 Serial.print(c);
89 /* wrap data to 80 columns*/
90 received_data_num++;
91 if(received_data_num % 80 == 0) {
92 Serial.println();
93 }
94 }
95}
96
97/* -------------------------------------------------------------------------- */
98void loop() {
99/* -------------------------------------------------------------------------- */
100 read_response();
101
102 // if the server's disconnected, stop the client:
103 if (!client.connected()) {
104 Serial.println();
105 Serial.println("disconnecting from server.");
106 client.stop();
107
108 // do nothing forevermore:
109 while (true);
110 }
111}
112
113/* -------------------------------------------------------------------------- */
114void printWifiStatus() {
115/* -------------------------------------------------------------------------- */
116 // print the SSID of the network you're attached to:
117 Serial.print("SSID: ");
118 Serial.println(WiFi.SSID());
119
120 // print your board's IP address:
121 IPAddress ip = WiFi.localIP();
122 Serial.print("IP Address: ");
123 Serial.println(ip);
124
125 // print the received signal strength:
126 long rssi = WiFi.RSSI();
127 Serial.print("signal strength (RSSI):");
128 Serial.print(rssi);
129 Serial.println(" dBm");
130}
131

Wi-Fi® Web Server

Code Source on Github
1/*
2 WiFi Web Server
3
4 A simple web server that shows the value of the analog input pins.
5
6 This example is written for a network using WPA encryption. For
7 WEP or WPA, change the WiFi.begin() call accordingly.
8
9 Circuit:
10 * Analog inputs attached to pins A0 through A5 (optional)
11
12 created 13 July 2010
13 by dlf (Metodo2 srl)
14 modified 31 May 2012
15 by Tom Igoe
16
17
18 Find the full UNO R4 WiFi Network documentation here:
19 https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-server
20 */
21
22#include "WiFiS3.h"
23
24
25
26#include "arduino_secrets.h"
27///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28char ssid[] = SECRET_SSID; // your network SSID (name)
29char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30int keyIndex = 0; // your network key index number (needed only for WEP)
31
32int status = WL_IDLE_STATUS;
33
34WiFiServer server(80);
35
36void setup() {
37 //Initialize serial and wait for port to open:
38 Serial.begin(9600);
39 while (!Serial) {
40 ; // wait for serial port to connect. Needed for native USB port only
41 }
42
43 // check for the WiFi module:
44 if (WiFi.status() == WL_NO_MODULE) {
45 Serial.println("Communication with WiFi module failed!");
46 // don't continue
47 while (true);
48 }
49
50 String fv = WiFi.firmwareVersion();
51 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
52 Serial.println("Please upgrade the firmware");
53 }
54
55 // attempt to connect to WiFi network:
56 while (status != WL_CONNECTED) {
57 Serial.print("Attempting to connect to SSID: ");
58 Serial.println(ssid);
59 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
60 status = WiFi.begin(ssid, pass);
61
62 // wait 10 seconds for connection:
63 delay(10000);
64 }
65 server.begin();
66 // you're connected now, so print out the status:
67 printWifiStatus();
68}
69
70
71void loop() {
72 // listen for incoming clients
73 WiFiClient client = server.available();
74 if (client) {
75 Serial.println("new client");
76 // an HTTP request ends with a blank line
77 boolean currentLineIsBlank = true;
78 while (client.connected()) {
79 if (client.available()) {
80 char c = client.read();
81 Serial.write(c);
82 // if you've gotten to the end of the line (received a newline
83 // character) and the line is blank, the HTTP request has ended,
84 // so you can send a reply
85 if (c == '\n' && currentLineIsBlank) {
86 // send a standard HTTP response header
87 client.println("HTTP/1.1 200 OK");
88 client.println("Content-Type: text/html");
89 client.println("Connection: close"); // the connection will be closed after completion of the response
90 client.println("Refresh: 5"); // refresh the page automatically every 5 sec
91 client.println();
92 client.println("<!DOCTYPE HTML>");
93 client.println("<html>");
94 // output the value of each analog input pin
95 for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
96 int sensorReading = analogRead(analogChannel);
97 client.print("analog input ");
98 client.print(analogChannel);
99 client.print(" is ");
100 client.print(sensorReading);
101 client.println("<br />");
102 }
103 client.println("</html>");
104 break;
105 }
106 if (c == '\n') {
107 // you're starting a new line
108 currentLineIsBlank = true;
109 } else if (c != '\r') {
110 // you've gotten a character on the current line
111 currentLineIsBlank = false;
112 }
113 }
114 }
115 // give the web browser time to receive the data
116 delay(1);
117
118 // close the connection:
119 client.stop();
120 Serial.println("client disconnected");
121 }
122}
123
124
125void printWifiStatus() {
126 // print the SSID of the network you're attached to:
127 Serial.print("SSID: ");
128 Serial.println(WiFi.SSID());
129
130 // print your board's IP address:
131 IPAddress ip = WiFi.localIP();
132 Serial.print("IP Address: ");
133 Serial.println(ip);
134
135 // print the received signal strength:
136 long rssi = WiFi.RSSI();
137 Serial.print("signal strength (RSSI):");
138 Serial.print(rssi);
139 Serial.println(" dBm");
140}
141

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.