The WiFiNINA library is designed for Arduino boards using a NINA W-10 series module. In this article you will find a series of examples that can be uploaded to your board.
You can also visit the WiFiNINA GitHub repository to learn more about this library.
Most examples in this article uses no external circuit, only the board itself is required.
Please note: these three boards use dedicated pins to communicate and select the WiFi module, therefore you have no restriction in the usage of the available digital pins connected to the header pins.
In this example, a simple web server lets you blink an LED via the web. This example uses the beginAP() function to set up an access point without relying on a local WiFI network. This example will print the IP address of your WiFi module to the Arduino Software (IDE) serial monitor. Once you know the IP address of our board, you can open that address in a web browser to turn on and off the LED on pin 9.
If the IP address of your shield is yourAddress:
The default address of the board in AP mode is 192.168.4.1. When you load this sketch, the WiFi module creates an Access Point with the name specified as SSID in arduino_secrets.h. Connect to it using the password specified as PASS.
1/*2
3 WiFi Web Server LED Blink4
5 A simple web server that lets you blink an LED via the web.6
7 This sketch will create a new access point (with no password).8
9 It will then launch a new server and print out the IP address10
11 to the Serial monitor. From there, you can open that address in a web browser12
13 to turn on and off the LED on pin 13.14
15 If the IP address of your board is yourAddress:16
17 http://yourAddress/H turns the LED on18
19 http://yourAddress/L turns it off20
21 created 25 Nov 201222
23 by Tom Igoe24
25 adapted to WiFi AP by Adafruit26
27 */28
29#include <SPI.h>30#include <WiFiNINA.h>31#include "arduino_secrets.h"32///////please enter your sensitive data in the Secret tab/arduino_secrets.h33char ssid[] = SECRET_SSID; // your network SSID (name)34char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)35int keyIndex = 0; // your network key Index number (needed only for WEP)36
37int led = LED_BUILTIN;38int status = WL_IDLE_STATUS;39
40WiFiServer server(80);41
42void setup() {43
44 //Initialize serial and wait for port to open:45
46 Serial.begin(9600);47
48 while (!Serial) {49
50 ; // wait for serial port to connect. Needed for native USB port only51
52 }53
54 Serial.println("Access Point Web Server");55
56 pinMode(led, OUTPUT); // set the LED pin mode57
58 // check for the WiFi module:59
60 if (WiFi.status() == WL_NO_MODULE) {61
62 Serial.println("Communication with WiFi module failed!");63
64 // don't continue65
66 while (true);67
68 }69
70 String fv = WiFi.firmwareVersion();71
72 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {73
74 Serial.println("Please upgrade the firmware");75
76 }77
78 // by default the local IP address of will be 192.168.4.179
80 // you can override it with the following:81
82 // WiFi.config(IPAddress(10, 0, 0, 1));83
84 // print the network name (SSID);85
86 Serial.print("Creating access point named: ");87
88 Serial.println(ssid);89
90 // Create open network. Change this line if you want to create an WEP network:91
92 status = WiFi.beginAP(ssid, pass);93
94 if (status != WL_AP_LISTENING) {95
96 Serial.println("Creating access point failed");97
98 // don't continue99
100 while (true);101
102 }103
104 // wait 10 seconds for connection:105
106 delay(10000);107
108 // start the web server on port 80109
110 server.begin();111
112 // you're connected now, so print out the status113
114 printWiFiStatus();115}116
117void loop() {118
119 // compare the previous status to the current status120
121 if (status != WiFi.status()) {122
123 // it has changed update the variable124
125 status = WiFi.status();126
127 if (status == WL_AP_CONNECTED) {128
129 // a device has connected to the AP130
131 Serial.println("Device connected to AP");132
133 } else {134
135 // a device has disconnected from the AP, and we are back in listening mode136
137 Serial.println("Device disconnected from AP");138
139 }140
141 }142
143
144
145 WiFiClient client = server.available(); // listen for incoming clients146
147 if (client) { // if you get a client,148
149 Serial.println("new client"); // print a message out the serial port150
151 String currentLine = ""; // make a String to hold incoming data from the client152
153 while (client.connected()) { // loop while the client's connected154
155 if (client.available()) { // if there's bytes to read from the client,156
157 char c = client.read(); // read a byte, then158
159 Serial.write(c); // print it out the serial monitor160
161 if (c == '\n') { // if the byte is a newline character162
163 // if the current line is blank, you got two newline characters in a row.164
165 // that's the end of the client HTTP request, so send a response:166
167 if (currentLine.length() == 0) {168
169 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)170
171 // and a content-type so the client knows what's coming, then a blank line:172
173 client.println("HTTP/1.1 200 OK");174
175 client.println("Content-type:text/html");176
177 client.println();178
179 // the content of the HTTP response follows the header:180
181 client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");182
183 client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");184
185 // The HTTP response ends with another blank line:186
187 client.println();188
189 // break out of the while loop:190
191 break;192
193 }194
195 else { // if you got a newline, then clear currentLine:196
197 currentLine = "";198
199 }200
201 }202
203 else if (c != '\r') { // if you got anything else but a carriage return character,204
205 currentLine += c; // add it to the end of the currentLine206
207 }208
209 // Check to see if the client request was "GET /H" or "GET /L":210
211 if (currentLine.endsWith("GET /H")) {212
213 digitalWrite(led, HIGH); // GET /H turns the LED on214
215 }216
217 if (currentLine.endsWith("GET /L")) {218
219 digitalWrite(led, LOW); // GET /L turns the LED off220
221 }222
223 }224
225 }226
227 // close the connection:228
229 client.stop();230
231 Serial.println("client disconnected");232
233 }234}235
236void printWiFiStatus() {237
238 // print the SSID of the network you're attached to:239
240 Serial.print("SSID: ");241
242 Serial.println(WiFi.SSID());243
244 // print your WiFi shield's IP address:245
246 IPAddress ip = WiFi.localIP();247
248 Serial.print("IP Address: ");249
250 Serial.println(ip);251
252 // print where to go in a browser:253
254 Serial.print("To see this page in action, open a browser to http://");255
256 Serial.println(ip);257
258}
This example shows you how to connect to an open (not encrypted) 802.11b/g network with one of the boards that support this library. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
1/*2
3 This example connects to an unencrypted Wifi network.4
5 Then it prints the MAC address of the board,6
7 the IP address obtained, and other network details.8
9 created 13 July 201010
11 by dlf (Metodo2 srl)12
13 modified 31 May 201214
15 by Tom Igoe16
17 */18#include <SPI.h>19#include <WiFiNINA.h>20
21#include "arduino_secrets.h"22///////please enter your sensitive data in the Secret tab/arduino_secrets.h23char ssid[] = SECRET_SSID; // your network SSID (name)24int status = WL_IDLE_STATUS; // the Wifi radio's status25
26void setup() {27
28 //Initialize serial and wait for port to open:29
30 Serial.begin(9600);31
32 while (!Serial) {33
34 ; // wait for serial port to connect. Needed for native USB port only35
36 }37
38 // check for the WiFi module:39
40 if (WiFi.status() == WL_NO_MODULE) {41
42 Serial.println("Communication with WiFi module failed!");43
44 // don't continue45
46 while (true);47
48 }49
50 String fv = WiFi.firmwareVersion();51
52 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {53
54 Serial.println("Please upgrade the firmware");55
56 }57
58 // attempt to connect to Wifi network:59
60 while (status != WL_CONNECTED) {61
62 Serial.print("Attempting to connect to open SSID: ");63
64 Serial.println(ssid);65
66 status = WiFi.begin(ssid);67
68 // wait 10 seconds for connection:69
70 delay(10000);71
72 }73
74 // you're connected now, so print out the data:75
76 Serial.print("You're connected to the network");77
78 printCurrentNet();79
80 printWifiData();81}82
83void loop() {84
85 // check the network connection once every 10 seconds:86
87 delay(10000);88
89 printCurrentNet();90}91
92void printWifiData() {93
94 // print your board's IP address:95
96 IPAddress ip = WiFi.localIP();97
98 Serial.print("IP Address: ");99
100 Serial.println(ip);101
102 Serial.println(ip);103
104 // print your MAC address:105
106 byte mac[6];107
108 WiFi.macAddress(mac);109
110 Serial.print("MAC address: ");111
112 printMacAddress(mac);113
114 // print your subnet mask:115
116 IPAddress subnet = WiFi.subnetMask();117
118 Serial.print("NetMask: ");119
120 Serial.println(subnet);121
122 // print your gateway address:123
124 IPAddress gateway = WiFi.gatewayIP();125
126 Serial.print("Gateway: ");127
128 Serial.println(gateway);129}130
131void printCurrentNet() {132
133 // print the SSID of the network you're attached to:134
135 Serial.print("SSID: ");136
137 Serial.println(WiFi.SSID());138
139 // print the MAC address of the router you're attached to:140
141 byte bssid[6];142
143 WiFi.BSSID(bssid);144
145 Serial.print("BSSID: ");146
147 printMacAddress(bssid);148
149 // print the received signal strength:150
151 long rssi = WiFi.RSSI();152
153 Serial.print("signal strength (RSSI):");154
155 Serial.println(rssi);156
157 // print the encryption type:158
159 byte encryption = WiFi.encryptionType();160
161 Serial.print("Encryption Type:");162
163 Serial.println(encryption, HEX);164}165
166void printMacAddress(byte mac[]) {167
168 for (int i = 5; i >= 0; i--) {169
170 if (mac[i] < 16) {171
172 Serial.print("0");173
174 }175
176 Serial.print(mac[i], HEX);177
178 if (i > 0) {179
180 Serial.print(":");181
182 }183
184 }185
186 Serial.println();187}
This example shows you how to connect to a WEP encrypted 802.11b/g network with one of the boards that support this library. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.
1/*2
3 This example connects to a WEP-encrypted Wifi network.4
5 Then it prints the MAC address of the Wifi module,6
7 the IP address obtained, and other network details.8
9 If you use 40-bit WEP, you need a key that is 10 characters long,10
11 and the characters must be hexadecimal (0-9 or A-F).12
13 e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work14
15 (too short) and ABBAISDEAF won't work (I and S are not16
17 hexadecimal characters).18
19 For 128-bit, you need a string that is 26 characters long.20
21 D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters,22
23 all in the 0-9, A-F range.24
25 created 13 July 201026
27 by dlf (Metodo2 srl)28
29 modified 31 May 201230
31 by Tom Igoe32
33 */34#include <SPI.h>35#include <WiFiNINA.h>36
37#include "arduino_secrets.h"38///////please enter your sensitive data in the Secret tab/arduino_secrets.h39char ssid[] = SECRET_SSID; // your network SSID (name)40char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)41int keyIndex = 0; // your network key Index number42int status = WL_IDLE_STATUS; // the Wifi radio's status43
44void setup() {45
46 //Initialize serial and wait for port to open:47
48 Serial.begin(9600);49
50 while (!Serial) {51
52 ; // wait for serial port to connect. Needed for native USB port only53
54 }55
56 // check for the WiFi module:57
58 if (WiFi.status() == WL_NO_MODULE) {59
60 Serial.println("Communication with WiFi module failed!");61
62 // don't continue63
64 while (true);65
66 }67
68 String fv = WiFi.firmwareVersion();69
70 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {71
72 Serial.println("Please upgrade the firmware");73
74 }75
76 // attempt to connect to Wifi network:77
78 while (status != WL_CONNECTED) {79
80 Serial.print("Attempting to connect to WEP network, SSID: ");81
82 Serial.println(ssid);83
84 status = WiFi.begin(ssid, keyIndex, pass);85
86 // wait 10 seconds for connection:87
88 delay(10000);89
90 }91
92 // once you are connected :93
94 Serial.print("You're connected to the network");95
96 printCurrentNet();97
98 printWifiData();99}100
101void loop() {102
103 // check the network connection once every 10 seconds:104
105 delay(10000);106
107 printCurrentNet();108}109
110void printWifiData() {111
112 // print your board's IP address:113
114 IPAddress ip = WiFi.localIP();115
116 Serial.print("IP Address: ");117
118 Serial.println(ip);119
120 Serial.println(ip);121
122 // print your MAC address:123
124 byte mac[6];125
126 WiFi.macAddress(mac);127
128 Serial.print("MAC address: ");129
130 printMacAddress(mac);131}132
133void printCurrentNet() {134
135 // print the SSID of the network you're attached to:136
137 Serial.print("SSID: ");138
139 Serial.println(WiFi.SSID());140
141 // print the MAC address of the router you're attached to:142
143 byte bssid[6];144
145 WiFi.BSSID(bssid);146
147 Serial.print("BSSID: ");148
149 printMacAddress(bssid);150
151 // print the received signal strength:152
153 long rssi = WiFi.RSSI();154
155 Serial.print("signal strength (RSSI):");156
157 Serial.println(rssi);158
159 // print the encryption type:160
161 byte encryption = WiFi.encryptionType();162
163 Serial.print("Encryption Type:");164
165 Serial.println(encryption, HEX);166
167 Serial.println();168}169
170void printMacAddress(byte mac[]) {171
172 for (int i = 5; i >= 0; i--) {173
174 if (mac[i] < 16) {175
176 Serial.print("0");177
178 }179
180 Serial.print(mac[i], HEX);181
182 if (i > 0) {183
184 Serial.print(":");185
186 }187
188 }189
190 Serial.println();191}
This example shows you how to connect to a WPA2 Personal encrypted 802.11b/g network with one of the boards that support this library. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The module will not connect to networks using WPA2 Enterprise encryption.
1/*2
3 This example connects to an unencrypted Wifi network.4
5 Then it prints the MAC address of the Wifi module,6
7 the IP address obtained, and other network details.8
9 created 13 July 201010
11 by dlf (Metodo2 srl)12
13 modified 31 May 201214
15 by Tom Igoe16
17 */18#include <SPI.h>19#include <WiFiNINA.h>20
21#include "arduino_secrets.h"22///////please enter your sensitive data in the Secret tab/arduino_secrets.h23char ssid[] = SECRET_SSID; // your network SSID (name)24char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)25int status = WL_IDLE_STATUS; // the Wifi radio's status26
27void setup() {28
29 //Initialize serial and wait for port to open:30
31 Serial.begin(9600);32
33 while (!Serial) {34
35 ; // wait for serial port to connect. Needed for native USB port only36
37 }38
39 // check for the WiFi module:40
41 if (WiFi.status() == WL_NO_MODULE) {42
43 Serial.println("Communication with WiFi module failed!");44
45 // don't continue46
47 while (true);48
49 }50
51 String fv = WiFi.firmwareVersion();52
53 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {54
55 Serial.println("Please upgrade the firmware");56
57 }58
59 // attempt to connect to Wifi network:60
61 while (status != WL_CONNECTED) {62
63 Serial.print("Attempting to connect to WPA SSID: ");64
65 Serial.println(ssid);66
67 // Connect to WPA/WPA2 network:68
69 status = WiFi.begin(ssid, pass);70
71 // wait 10 seconds for connection:72
73 delay(10000);74
75 }76
77 // you're connected now, so print out the data:78
79 Serial.print("You're connected to the network");80
81 printCurrentNet();82
83 printWifiData();84
85}86
87void loop() {88
89 // check the network connection once every 10 seconds:90
91 delay(10000);92
93 printCurrentNet();94}95
96void printWifiData() {97
98 // print your board's IP address:99
100 IPAddress ip = WiFi.localIP();101
102 Serial.print("IP Address: ");103
104 Serial.println(ip);105
106 Serial.println(ip);107
108 // print your MAC address:109
110 byte mac[6];111
112 WiFi.macAddress(mac);113
114 Serial.print("MAC address: ");115
116 printMacAddress(mac);117}118
119void printCurrentNet() {120
121 // print the SSID of the network you're attached to:122
123 Serial.print("SSID: ");124
125 Serial.println(WiFi.SSID());126
127 // print the MAC address of the router you're attached to:128
129 byte bssid[6];130
131 WiFi.BSSID(bssid);132
133 Serial.print("BSSID: ");134
135 printMacAddress(bssid);136
137 // print the received signal strength:138
139 long rssi = WiFi.RSSI();140
141 Serial.print("signal strength (RSSI):");142
143 Serial.println(rssi);144
145 // print the encryption type:146
147 byte encryption = WiFi.encryptionType();148
149 Serial.print("Encryption Type:");150
151 Serial.println(encryption, HEX);152
153 Serial.println();154}155
156void printMacAddress(byte mac[]) {157
158 for (int i = 5; i >= 0; i--) {159
160 if (mac[i] < 16) {161
162 Serial.print("0");163
164 }165
166 Serial.print(mac[i], HEX);167
168 if (i > 0) {169
170 Serial.print(":");171
172 }173
174 }175
176 Serial.println();177}
This example scans for 802.11b/g network with one of the boards that support this library. Your Arduino Software (IDE) serial monitor will print out information about the board and the networks it can see. It will not connect to a network.
Open your Arduino Software (IDE) serial monitor to view the networks the WiFi module can see. The shield may not see as many networks as your computer.
1/*2
3 This example prints the board's MAC address, and4
5 scans for available Wifi networks using the NINA module.6
7 Every ten seconds, it scans again. It doesn't actually8
9 connect to any network, so no encryption scheme is specified.10
11 Circuit:12
13 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)14
15 created 13 July 201016
17 by dlf (Metodo2 srl)18
19 modified 21 Junn 201220
21 by Tom Igoe and Jaymes Dec22
23 */24
25#include <SPI.h>26#include <WiFiNINA.h>27
28void setup() {29
30 //Initialize serial and wait for port to open:31
32 Serial.begin(9600);33
34 while (!Serial) {35
36 ; // wait for serial port to connect. Needed for native USB port only37
38 }39
40 // check for the WiFi module:41
42 if (WiFi.status() == WL_NO_MODULE) {43
44 Serial.println("Communication with WiFi module failed!");45
46 // don't continue47
48 while (true);49
50 }51
52 String fv = WiFi.firmwareVersion();53
54 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {55
56 Serial.println("Please upgrade the firmware");57
58 }59
60 // print your MAC address:61
62 byte mac[6];63
64 WiFi.macAddress(mac);65
66 Serial.print("MAC: ");67
68 printMacAddress(mac);69}70
71void loop() {72
73 // scan for existing networks:74
75 Serial.println("Scanning available networks...");76
77 listNetworks();78
79 delay(10000);80}81
82void listNetworks() {83
84 // scan for nearby networks:85
86 Serial.println("** Scan Networks **");87
88 int numSsid = WiFi.scanNetworks();89
90 if (numSsid == -1) {91
92 Serial.println("Couldn't get a wifi connection");93
94 while (true);95
96 }97
98 // print the list of networks seen:99
100 Serial.print("number of available networks:");101
102 Serial.println(numSsid);103
104 // print the network number and name for each network found:105
106 for (int thisNet = 0; thisNet < numSsid; thisNet++) {107
108 Serial.print(thisNet);109
110 Serial.print(") ");111
112 Serial.print(WiFi.SSID(thisNet));113
114 Serial.print("\tSignal: ");115
116 Serial.print(WiFi.RSSI(thisNet));117
118 Serial.print(" dBm");119
120 Serial.print("\tEncryption: ");121
122 printEncryptionType(WiFi.encryptionType(thisNet));123
124 }125}126
127void printEncryptionType(int thisType) {128
129 // read the encryption type and print out the name:130
131 switch (thisType) {132
133 case ENC_TYPE_WEP:134
135 Serial.println("WEP");136
137 break;138
139 case ENC_TYPE_TKIP:140
141 Serial.println("WPA");142
143 break;144
145 case ENC_TYPE_CCMP:146
147 Serial.println("WPA2");148
149 break;150
151 case ENC_TYPE_NONE:152
153 Serial.println("None");154
155 break;156
157 case ENC_TYPE_AUTO:158
159 Serial.println("Auto");160
161 break;162
163 case ENC_TYPE_UNKNOWN:164
165 default:166
167 Serial.println("Unknown");168
169 break;170
171 }172}173
174void printMacAddress(byte mac[]) {175
176 for (int i = 5; i >= 0; i--) {177
178 if (mac[i] < 16) {179
180 Serial.print("0");181
182 }183
184 Serial.print(mac[i], HEX);185
186 if (i > 0) {187
188 Serial.print(":");189
190 }191
192 }193
194 Serial.println();195}
This example scans for 802.11b/g network with one of the boards that support this library. Your Arduino Software (IDE) serial monitor will print out information about the board and the networks it can see, with the encryption type. It will not connect to a network.
Open your Arduino Software (IDE) serial monitor to view the networks the WiFi module can see. The shield may not see as many networks as your computer, but it will show also the encryption type.
1/*2
3 This example prints the board's MAC address, and4
5 scans for available WiFi networks using the NINA module.6
7 Every ten seconds, it scans again. It doesn't actually8
9 connect to any network, so no encryption scheme is specified.10
11 BSSID and WiFi channel are printed12
13 Circuit:14
15 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)16
17 This example is based on ScanNetworks18
19 created 1 Mar 201720
21 by Arturo Guadalupi22
23*/24
25#include <SPI.h>26#include <WiFiNINA.h>27
28void setup() {29
30 //Initialize serial and wait for port to open:31
32 Serial.begin(9600);33
34 while (!Serial) {35
36 ; // wait for serial port to connect. Needed for native USB port only37
38 }39
40 // check for the WiFi module:41
42 if (WiFi.status() == WL_NO_MODULE) {43
44 Serial.println("Communication with WiFi module failed!");45
46 // don't continue47
48 while (true);49
50 }51
52 String fv = WiFi.firmwareVersion();53
54 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {55
56 Serial.println("Please upgrade the firmware");57
58 }59
60 // print your MAC address:61
62 byte mac[6];63
64 WiFi.macAddress(mac);65
66 Serial.print("MAC: ");67
68 printMacAddress(mac);69
70 // scan for existing networks:71
72 Serial.println();73
74 Serial.println("Scanning available networks...");75
76 listNetworks();77}78
79void loop() {80
81 delay(10000);82
83 // scan for existing networks:84
85 Serial.println("Scanning available networks...");86
87 listNetworks();88}89
90void listNetworks() {91
92 // scan for nearby networks:93
94 Serial.println("** Scan Networks **");95
96 int numSsid = WiFi.scanNetworks();97
98 if (numSsid == -1)99
100 {101
102 Serial.println("Couldn't get a WiFi connection");103
104 while (true);105
106 }107
108 // print the list of networks seen:109
110 Serial.print("number of available networks: ");111
112 Serial.println(numSsid);113
114 // print the network number and name for each network found:115
116 for (int thisNet = 0; thisNet < numSsid; thisNet++) {117
118 Serial.print(thisNet + 1);119
120 Serial.print(") ");121
122 Serial.print("Signal: ");123
124 Serial.print(WiFi.RSSI(thisNet));125
126 Serial.print(" dBm");127
128 Serial.print("\tChannel: ");129
130 Serial.print(WiFi.channel(thisNet));131
132 byte bssid[6];133
134 Serial.print("\t\tBSSID: ");135
136 printMacAddress(WiFi.BSSID(thisNet, bssid));137
138 Serial.print("\tEncryption: ");139
140 printEncryptionType(WiFi.encryptionType(thisNet));141
142 Serial.print("\t\tSSID: ");143
144 Serial.println(WiFi.SSID(thisNet));145
146 Serial.flush();147
148 }149
150 Serial.println();151}152
153void printEncryptionType(int thisType) {154
155 // read the encryption type and print out the name:156
157 switch (thisType) {158
159 case ENC_TYPE_WEP:160
161 Serial.print("WEP");162
163 break;164
165 case ENC_TYPE_TKIP:166
167 Serial.print("WPA");168
169 break;170
171 case ENC_TYPE_CCMP:172
173 Serial.print("WPA2");174
175 break;176
177 case ENC_TYPE_NONE:178
179 Serial.print("None");180
181 break;182
183 case ENC_TYPE_AUTO:184
185 Serial.print("Auto");186
187 break;188
189 case ENC_TYPE_UNKNOWN:190
191 default:192
193 Serial.print("Unknown");194
195 break;196
197 }198}199
200void print2Digits(byte thisByte) {201
202 if (thisByte < 0xF) {203
204 Serial.print("0");205
206 }207
208 Serial.print(thisByte, HEX);209}210
211void printMacAddress(byte mac[]) {212
213 for (int i = 5; i >= 0; i--) {214
215 if (mac[i] < 16) {216
217 Serial.print("0");218
219 }220
221 Serial.print(mac[i], HEX);222
223 if (i > 0) {224
225 Serial.print(":");226
227 }228
229 }230
231 Serial.println();232}
In this example, a simple web server lets you blink an LED via the web. This example will print the IP address of your WiFi module (once connected) to the Arduino Software (IDE) serial monitor. Once you know the IP address of our board, you can open that address in a web browser to turn on and off the LED on pin 9.
If you do not have an LED, you can use the built-in LED instead (just remember to change out "9" to "LED_BUILTIN")
If the IP address of your shield is yourAddress:
This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.
WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.
1/*2
3 WiFi Web Server LED Blink4
5 A simple web server that lets you blink an LED via the web.6
7 This sketch will print the IP address of your WiFi module (once connected)8
9 to the Serial monitor. From there, you can open that address in a web browser10
11 to turn on and off the LED on pin 9.12
13 If the IP address of your board is yourAddress:14
15 http://yourAddress/H turns the LED on16
17 http://yourAddress/L turns it off18
19 This example is written for a network using WPA encryption. For20
21 WEP or WPA, change the Wifi.begin() call accordingly.22
23 Circuit:24
25 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)26
27 * LED attached to pin 928
29 created 25 Nov 201230
31 by Tom Igoe32
33 */34#include <SPI.h>35#include <WiFiNINA.h>36
37#include "arduino_secrets.h"38///////please enter your sensitive data in the Secret tab/arduino_secrets.h39char ssid[] = SECRET_SSID; // your network SSID (name)40char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)41int keyIndex = 0; // your network key Index number (needed only for WEP)42
43int status = WL_IDLE_STATUS;44
45WiFiServer server(80);46
47void setup() {48
49 Serial.begin(9600); // initialize serial communication50
51 pinMode(9, OUTPUT); // set the LED pin mode52
53 // check for the WiFi module:54
55 if (WiFi.status() == WL_NO_MODULE) {56
57 Serial.println("Communication with WiFi module failed!");58
59 // don't continue60
61 while (true);62
63 }64
65 String fv = WiFi.firmwareVersion();66
67 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {68
69 Serial.println("Please upgrade the firmware");70
71 }72
73 // attempt to connect to Wifi network:74
75 while (status != WL_CONNECTED) {76
77 Serial.print("Attempting to connect to Network named: ");78
79 Serial.println(ssid); // print the network name (SSID);80
81 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:82
83 status = WiFi.begin(ssid, pass);84
85 // wait 10 seconds for connection:86
87 delay(10000);88
89 }90
91 server.begin(); // start the web server on port 8092
93 printWifiStatus(); // you're connected now, so print out the status94}95
96void loop() {97
98 WiFiClient client = server.available(); // listen for incoming clients99
100 if (client) { // if you get a client,101
102 Serial.println("new client"); // print a message out the serial port103
104 String currentLine = ""; // make a String to hold incoming data from the client105
106 while (client.connected()) { // loop while the client's connected107
108 if (client.available()) { // if there's bytes to read from the client,109
110 char c = client.read(); // read a byte, then111
112 Serial.write(c); // print it out the serial monitor113
114 if (c == '\n') { // if the byte is a newline character115
116 // if the current line is blank, you got two newline characters in a row.117
118 // that's the end of the client HTTP request, so send a response:119
120 if (currentLine.length() == 0) {121
122 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)123
124 // and a content-type so the client knows what's coming, then a blank line:125
126 client.println("HTTP/1.1 200 OK");127
128 client.println("Content-type:text/html");129
130 client.println();131
132 // the content of the HTTP response follows the header:133
134 client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");135
136 client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");137
138 // The HTTP response ends with another blank line:139
140 client.println();141
142 // break out of the while loop:143
144 break;145
146 } else { // if you got a newline, then clear currentLine:147
148 currentLine = "";149
150 }151
152 } else if (c != '\r') { // if you got anything else but a carriage return character,153
154 currentLine += c; // add it to the end of the currentLine155
156 }157
158 // Check to see if the client request was "GET /H" or "GET /L":159
160 if (currentLine.endsWith("GET /H")) {161
162 digitalWrite(9, HIGH); // GET /H turns the LED on163
164 }165
166 if (currentLine.endsWith("GET /L")) {167
168 digitalWrite(9, LOW); // GET /L turns the LED off169
170 }171
172 }173
174 }175
176 // close the connection:177
178 client.stop();179
180 Serial.println("client disconnected");181
182 }183}184
185void printWifiStatus() {186
187 // print the SSID of the network you're attached to:188
189 Serial.print("SSID: ");190
191 Serial.println(WiFi.SSID());192
193 // print your board's IP address:194
195 IPAddress ip = WiFi.localIP();196
197 Serial.print("IP Address: ");198
199 Serial.println(ip);200
201 // print the received signal strength:202
203 long rssi = WiFi.RSSI();204
205 Serial.print("signal strength (RSSI):");206
207 Serial.print(rssi);208
209 Serial.println(" dBm");210
211 // print where to go in a browser:212
213 Serial.print("To see this page in action, open a browser to http://");214
215 Serial.println(ip);216}
In this example, you will use your board's wifi capabilities to query a Network Time Protocol (NTP) server. In this way, your board can get the time from the Internet.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
1/*2
3 Udp NTP Client4
5 Get the time from a Network Time Protocol (NTP) time server6
7 Demonstrates use of UDP sendPacket and ReceivePacket8
9 For more on NTP time servers and the messages needed to communicate with them,10
11 see http://en.wikipedia.org/wiki/Network_Time_Protocol12
13 created 4 Sep 201014
15 by Michael Margolis16
17 modified 9 Apr 201218
19 by Tom Igoe20
21 This code is in the public domain.22
23 */24
25#include <SPI.h>26#include <WiFiNINA.h>27#include <WiFiUdp.h>28
29int status = WL_IDLE_STATUS;30#include "arduino_secrets.h"31///////please enter your sensitive data in the Secret tab/arduino_secrets.h32char ssid[] = SECRET_SSID; // your network SSID (name)33char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)34int keyIndex = 0; // your network key Index number (needed only for WEP)35
36unsigned int localPort = 2390; // local port to listen for UDP packets37
38IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server39
40const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message41
42byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets43
44// A UDP instance to let us send and receive packets over UDP45
46WiFiUDP Udp;47
48void setup() {49
50 // Open serial communications and wait for port to open:51
52 Serial.begin(9600);53
54 while (!Serial) {55
56 ; // wait for serial port to connect. Needed for native USB port only57
58 }59
60 // check for the WiFi module:61
62 if (WiFi.status() == WL_NO_MODULE) {63
64 Serial.println("Communication with WiFi module failed!");65
66 // don't continue67
68 while (true);69
70 }71
72 String fv = WiFi.firmwareVersion();73
74 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {75
76 Serial.println("Please upgrade the firmware");77
78 }79
80 // attempt to connect to Wifi network:81
82 while (status != WL_CONNECTED) {83
84 Serial.print("Attempting to connect to SSID: ");85
86 Serial.println(ssid);87
88 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:89
90 status = WiFi.begin(ssid, pass);91
92 // wait 10 seconds for connection:93
94 delay(10000);95
96 }97
98 Serial.println("Connected to wifi");99
100 printWifiStatus();101
102 Serial.println("\nStarting connection to server...");103
104 Udp.begin(localPort);105}106
107void loop() {108
109 sendNTPpacket(timeServer); // send an NTP packet to a time server110
111 // wait to see if a reply is available112
113 delay(1000);114
115 if (Udp.parsePacket()) {116
117 Serial.println("packet received");118
119 // We've received a packet, read the data from it120
121 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer122
123 //the timestamp starts at byte 40 of the received packet and is four bytes,124
125 // or two words, long. First, esxtract the two words:126
127 unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);128
129 unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);130
131 // combine the four bytes (two words) into a long integer132
133 // this is NTP time (seconds since Jan 1 1900):134
135 unsigned long secsSince1900 = highWord << 16 | lowWord;136
137 Serial.print("Seconds since Jan 1 1900 = ");138
139 Serial.println(secsSince1900);140
141 // now convert NTP time into everyday time:142
143 Serial.print("Unix time = ");144
145 // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:146
147 const unsigned long seventyYears = 2208988800UL;148
149 // subtract seventy years:150
151 unsigned long epoch = secsSince1900 - seventyYears;152
153 // print Unix time:154
155 Serial.println(epoch);156
157 // print the hour, minute and second:158
159 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)160
161 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)162
163 Serial.print(':');164
165 if (((epoch % 3600) / 60) < 10) {166
167 // In the first 10 minutes of each hour, we'll want a leading '0'168
169 Serial.print('0');170
171 }172
173 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)174
175 Serial.print(':');176
177 if ((epoch % 60) < 10) {178
179 // In the first 10 seconds of each minute, we'll want a leading '0'180
181 Serial.print('0');182
183 }184
185 Serial.println(epoch % 60); // print the second186
187 }188
189 // wait ten seconds before asking for the time again190
191 delay(10000);192}193
194// send an NTP request to the time server at the given address195unsigned long sendNTPpacket(IPAddress& address) {196
197 //Serial.println("1");198
199 // set all bytes in the buffer to 0200
201 memset(packetBuffer, 0, NTP_PACKET_SIZE);202
203 // Initialize values needed to form NTP request204
205 // (see URL above for details on the packets)206
207 //Serial.println("2");208
209 packetBuffer[0] = 0b11100011; // LI, Version, Mode210
211 packetBuffer[1] = 0; // Stratum, or type of clock212
213 packetBuffer[2] = 6; // Polling Interval214
215 packetBuffer[3] = 0xEC; // Peer Clock Precision216
217 // 8 bytes of zero for Root Delay & Root Dispersion218
219 packetBuffer[12] = 49;220
221 packetBuffer[13] = 0x4E;222
223 packetBuffer[14] = 49;224
225 packetBuffer[15] = 52;226
227 //Serial.println("3");228
229 // all NTP fields have been given values, now230
231 // you can send a packet requesting a timestamp:232
233 Udp.beginPacket(address, 123); //NTP requests are to port 123234
235 //Serial.println("4");236
237 Udp.write(packetBuffer, NTP_PACKET_SIZE);238
239 //Serial.println("5");240
241 Udp.endPacket();242
243 //Serial.println("6");244}245
246void printWifiStatus() {247
248 // print the SSID of the network you're attached to:249
250 Serial.print("SSID: ");251
252 Serial.println(WiFi.SSID());253
254 // print your board's IP address:255
256 IPAddress ip = WiFi.localIP();257
258 Serial.print("IP Address: ");259
260 Serial.println(ip);261
262 // print the received signal strength:263
264 long rssi = WiFi.RSSI();265
266 Serial.print("signal strength (RSSI):");267
268 Serial.print(rssi);269
270 Serial.println(" dBm");271}
A simple server that distributes any incoming messages to all connected clients. To use, open a terminal window, telnet to your WiFi module's IP address, and type away. Any incoming text will be sent to all connected clients (including the one typing). Additionally, you will be able to see the client's input in your Arduino Software (IDE) serial monitor as well.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.
WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.
1/*2
3 Chat Server4
5 A simple server that distributes any incoming messages to all6
7 connected clients. To use telnet to your device's IP address and type.8
9 You can see the client's input in the serial monitor as well.10
11 This example is written for a network using WPA encryption. For12
13 WEP or WPA, change the Wifi.begin() call accordingly.14
15 Circuit:16
17 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)18
19 created 18 Dec 200920
21 by David A. Mellis22
23 modified 31 May 201224
25 by Tom Igoe26
27 */28
29#include <SPI.h>30#include <WiFiNINA.h>31
32#include "arduino_secrets.h"33///////please enter your sensitive data in the Secret tab/arduino_secrets.h34char ssid[] = SECRET_SSID; // your network SSID (name)35char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)36
37int keyIndex = 0; // your network key Index number (needed only for WEP)38
39int status = WL_IDLE_STATUS;40
41WiFiServer server(23);42
43boolean alreadyConnected = false; // whether or not the client was connected previously44
45void setup() {46
47 //Initialize serial and wait for port to open:48
49 Serial.begin(9600);50
51 while (!Serial) {52
53 ; // wait for serial port to connect. Needed for native USB port only54
55 }56
57 // check for the WiFi module:58
59 if (WiFi.status() == WL_NO_MODULE) {60
61 Serial.println("Communication with WiFi module failed!");62
63 // don't continue64
65 while (true);66
67 }68
69 String fv = WiFi.firmwareVersion();70
71 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {72
73 Serial.println("Please upgrade the firmware");74
75 }76
77 // attempt to connect to Wifi network:78
79 while (status != WL_CONNECTED) {80
81 Serial.print("Attempting to connect to SSID: ");82
83 Serial.println(ssid);84
85 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:86
87 status = WiFi.begin(ssid, pass);88
89 // wait 10 seconds for connection:90
91 delay(10000);92
93 }94
95 // start the server:96
97 server.begin();98
99 // you're connected now, so print out the status:100
101 printWifiStatus();102}103
104void loop() {105
106 // wait for a new client:107
108 WiFiClient client = server.available();109
110 // when the client sends the first byte, say hello:111
112 if (client) {113
114 if (!alreadyConnected) {115
116 // clead out the input buffer:117
118 client.flush();119
120 Serial.println("We have a new client");121
122 client.println("Hello, client!");123
124 alreadyConnected = true;125
126 }127
128 if (client.available() > 0) {129
130 // read the bytes incoming from the client:131
132 char thisChar = client.read();133
134 // echo the bytes back to the client:135
136 server.write(thisChar);137
138 // echo the bytes to the server as well:139
140 Serial.write(thisChar);141
142 }143
144 }145}146
147void printWifiStatus() {148
149 // print the SSID of the network you're attached to:150
151 Serial.print("SSID: ");152
153 Serial.println(WiFi.SSID());154
155 // print your board's IP address:156
157 IPAddress ip = WiFi.localIP();158
159 Serial.print("IP Address: ");160
161 Serial.println(ip);162
163 // print the received signal strength:164
165 long rssi = WiFi.RSSI();166
167 Serial.print("signal strength (RSSI):");168
169 Serial.print(rssi);170
171 Serial.println(" dBm");172}
This example connects to a encrypted WiFi network (WPA/WPA2) then it prints the MAC address of the WiFi module, the IP address obtained, and other network details. After this initial phase, the loop continuously pings a given host, specified by IP Address or name.
You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The module will not connect to networks using WPA2 Enterprise encryption.
1/*2
3 This example connects to a encrypted WiFi network (WPA/WPA2).4
5 Then it prints the MAC address of the board,6
7 the IP address obtained, and other network details.8
9 Then it continuously pings given host specified by IP Address or name.10
11 Circuit:12
13 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)14
15 created 13 July 201016
17 by dlf (Metodo2 srl)18
19 modified 09 June 201620
21 by Petar Georgiev22
23*/24#include <SPI.h>25#include <WiFiNINA.h>26
27#include "arduino_secrets.h"28///////please enter your sensitive data in the Secret tab/arduino_secrets.h29char ssid[] = SECRET_SSID; // your network SSID (name)30char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)31int status = WL_IDLE_STATUS; // the WiFi radio's status32
33// Specify IP address or hostname34
35String hostName = "www.google.com";36int pingResult;37
38void setup() {39
40 // Initialize serial and wait for port to open:41
42 Serial.begin(9600);43
44 while (!Serial) {45
46 ; // wait for serial port to connect. Needed for native USB port only47
48 }49
50 // check for the WiFi module:51
52 if (WiFi.status() == WL_NO_MODULE) {53
54 Serial.println("Communication with WiFi module failed!");55
56 // don't continue57
58 while (true);59
60 }61
62 String fv = WiFi.firmwareVersion();63
64 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {65
66 Serial.println("Please upgrade the firmware");67
68 }69
70 // attempt to connect to WiFi network:71
72 while ( status != WL_CONNECTED) {73
74 Serial.print("Attempting to connect to WPA SSID: ");75
76 Serial.println(ssid);77
78 // Connect to WPA/WPA2 network:79
80 status = WiFi.begin(ssid, pass);81
82 // wait 5 seconds for connection:83
84 delay(5000);85
86 }