WiFiNINA Library Examples
The WiFi library for boards with a NINA WiFi module. Works with the MKR WiFi 1010, MKR Vidor 4000, Uno WiFi Rev2, Nano 33 IoT and the Nano RP2040 Connect.
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.
Hardware Required
- Arduino MKR WiFi 1010
- Arduino MKR VIDOR 4000
- Arduino UNO WiFi Rev.2
- Arduino Nano 33 IoT
- Arduino Nano RP2040 Connect
Circuit
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.
Examples
WiFiNINA AP Simple Web Server
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:
- http://yourAddress/H turns the LED on
- http://yourAddress/L turns it off
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}WiFiNINA Connect No Encryption
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}WifiNINA Connect With WEP
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}WifiNINA Connect With WPA
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}WifiNINA Scan Networks
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}WifiNINA Scan Networks Advanced
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}WifiNINA Simple Web Server WiFi
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:
- http://yourAddress/H turns the LED on
- http://yourAddress/L turns it off
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}WifiNINA Udp NTP Client
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}WifiNINA WiFi Chat Server
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}WifiNINA WiFi Ping
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  }87
88  // you're connected now, so print out the data:89
90  Serial.println("You're connected to the network");91
92  printCurrentNet();93
94  printWiFiData();95}96
97void loop() {98
99  Serial.print("Pinging ");100
101  Serial.print(hostName);102
103  Serial.print(": ");104
105  pingResult = WiFi.ping(hostName);106
107  if (pingResult >= 0) {108
109    Serial.print("SUCCESS! RTT = ");110
111    Serial.print(pingResult);112
113    Serial.println(" ms");114
115  } else {116
117    Serial.print("FAILED! Error code: ");118
119    Serial.println(pingResult);120
121  }122
123  delay(5000);124}125
126void printWiFiData() {127
128  // print your board's IP address:129
130  IPAddress ip = WiFi.localIP();131
132  Serial.print("IP address : ");133
134  Serial.println(ip);135
136  Serial.print("Subnet mask: ");137
138  Serial.println((IPAddress)WiFi.subnetMask());139
140  Serial.print("Gateway IP : ");141
142  Serial.println((IPAddress)WiFi.gatewayIP());143
144  // print your MAC address:145
146  byte mac[6];147
148  WiFi.macAddress(mac);149
150  Serial.print("MAC address: ");151
152  printMacAddress(mac);153}154
155void printCurrentNet() {156
157  // print the SSID of the network you're attached to:158
159  Serial.print("SSID: ");160
161  Serial.println(WiFi.SSID());162
163  // print the MAC address of the router you're attached to:164
165  byte bssid[6];166
167  WiFi.BSSID(bssid);168
169  Serial.print("BSSID: ");170
171  printMacAddress(bssid);172
173  // print the received signal strength:174
175  long rssi = WiFi.RSSI();176
177  Serial.print("signal strength (RSSI): ");178
179  Serial.println(rssi);180
181  // print the encryption type:182
183  byte encryption = WiFi.encryptionType();184
185  Serial.print("Encryption Type: ");186
187  Serial.println(encryption, HEX);188
189  Serial.println();190}191
192void printMacAddress(byte mac[]) {193
194  for (int i = 5; i >= 0; i--) {195
196    if (mac[i] < 16) {197
198      Serial.print("0");199
200    }201
202    Serial.print(mac[i], HEX);203
204    if (i > 0) {205
206      Serial.print(":");207
208    }209
210  }211
212  Serial.println();213}WifiNINA WiFi SSL Client
This example creates a client object that connects and transfers data using always SSL.
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
3This example creates a client object that connects and transfers4
5data using always SSL.6
7It is compatible with the methods normally related to plain8
9connections, like client.connect(host, port).10
11Written by Arturo Guadalupi12
13last revision November 201514
15*/16
17#include <SPI.h>18#include <WiFiNINA.h>19
20#include "arduino_secrets.h"21///////please enter your sensitive data in the Secret tab/arduino_secrets.h22char ssid[] = SECRET_SSID;        // your network SSID (name)23char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)24int keyIndex = 0;            // your network key Index number (needed only for WEP)25
26int status = WL_IDLE_STATUS;27// if you don't want to use DNS (and reduce your sketch size)28// use the numeric IP instead of the name for the server:29//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)30char server[] = "www.google.com";    // name address for Google (using DNS)31
32// Initialize the Ethernet client library33// with the IP address and port of the server34// that you want to connect to (port 80 is default for HTTP):35
36WiFiSSLClient client;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 SSID: ");75
76    Serial.println(ssid);77
78    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:79
80    status = WiFi.begin(ssid, pass);81
82    // wait 10 seconds for connection:83
84    delay(10000);85
86  }87
88  Serial.println("Connected to wifi");89
90  printWiFiStatus();91
92  Serial.println("\nStarting connection to server...");93
94  // if you get a connection, report back via serial:95
96  if (client.connect(server, 443)) {97
98    Serial.println("connected to server");99
100    // Make a HTTP request:101
102    client.println("GET /search?q=arduino HTTP/1.1");103
104    client.println("Host: www.google.com");105
106    client.println("Connection: close");107
108    client.println();109
110  }111}112
113void loop() {114
115  // if there are incoming bytes available116
117  // from the server, read them and print them:118
119  while (client.available()) {120
121    char c = client.read();122
123    Serial.write(c);124
125  }126
127  // if the server's disconnected, stop the client:128
129  if (!client.connected()) {130
131    Serial.println();132
133    Serial.println("disconnecting from server.");134
135    client.stop();136
137    // do nothing forevermore:138
139    while (true);140
141  }142}143
144void printWiFiStatus() {145
146  // print the SSID of the network you're attached to:147
148  Serial.print("SSID: ");149
150  Serial.println(WiFi.SSID());151
152  // print your board's IP address:153
154  IPAddress ip = WiFi.localIP();155
156  Serial.print("IP Address: ");157
158  Serial.println(ip);159
160  // print the received signal strength:161
162  long rssi = WiFi.RSSI();163
164  Serial.print("signal strength (RSSI):");165
166  Serial.print(rssi);167
168  Serial.println(" dBm");169}WifiNINA WiFi Udp Send Receive String
This tutorial waits for a UDP packet on a local port. When a valid packet is received, an acknowledge packet is sent back to the client on a specified outgoing port. It relies on a WiFi connection made to your WiFi equipped board.
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  WiFi UDP Send and Receive String4
5 This sketch wait an UDP packet on localPort using the WiFi module.6
7 When a packet is received an Acknowledge packet is sent to the client on port remotePort8
9 created 30 December 201210
11 by dlf (Metodo2 srl)12
13 */14
15#include <SPI.h>16#include <WiFiNINA.h>17#include <WiFiUdp.h>18
19int status = WL_IDLE_STATUS;20#include "arduino_secrets.h"21///////please enter your sensitive data in the Secret tab/arduino_secrets.h22char ssid[] = SECRET_SSID;        // your network SSID (name)23char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)24int keyIndex = 0;            // your network key Index number (needed only for WEP)25
26unsigned int localPort = 2390;      // local port to listen on27
28char packetBuffer[256]; //buffer to hold incoming packet29char  ReplyBuffer[] = "acknowledged";       // a string to send back30
31WiFiUDP Udp;32
33void setup() {34
35  //Initialize serial and wait for port to open:36
37  Serial.begin(9600);38
39  while (!Serial) {40
41    ; // wait for serial port to connect. Needed for native USB port only42
43  }44
45  // check for the WiFi module:46
47  if (WiFi.status() == WL_NO_MODULE) {48
49    Serial.println("Communication with WiFi module failed!");50
51    // don't continue52
53    while (true);54
55  }56
57  String fv = WiFi.firmwareVersion();58
59  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {60
61    Serial.println("Please upgrade the firmware");62
63  }64
65  // attempt to connect to Wifi network:66
67  while (status != WL_CONNECTED) {68
69    Serial.print("Attempting to connect to SSID: ");70
71    Serial.println(ssid);72
73    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:74
75    status = WiFi.begin(ssid, pass);76
77    // wait 10 seconds for connection:78
79    delay(10000);80
81  }82
83  Serial.println("Connected to wifi");84
85  printWifiStatus();86
87  Serial.println("\nStarting connection to server...");88
89  // if you get a connection, report back via serial:90
91  Udp.begin(localPort);92}93
94void loop() {95
96  // if there's data available, read a packet97
98  int packetSize = Udp.parsePacket();99
100  if (packetSize) {101
102    Serial.print("Received packet of size ");103
104    Serial.println(packetSize);105
106    Serial.print("From ");107
108    IPAddress remoteIp = Udp.remoteIP();109
110    Serial.print(remoteIp);111
112    Serial.print(", port ");113
114    Serial.println(Udp.remotePort());115
116    // read the packet into packetBufffer117
118    int len = Udp.read(packetBuffer, 255);119
120    if (len > 0) {121
122      packetBuffer[len] = 0;123
124    }125
126    Serial.println("Contents:");127
128    Serial.println(packetBuffer);129
130    // send a reply, to the IP address and port that sent us the packet we received131
132    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());133
134    Udp.write(ReplyBuffer);135
136    Udp.endPacket();137
138  }139}140
141void printWifiStatus() {142
143  // print the SSID of the network you're attached to:144
145  Serial.print("SSID: ");146
147  Serial.println(WiFi.SSID());148
149  // print your board's IP address:150
151  IPAddress ip = WiFi.localIP();152
153  Serial.print("IP Address: ");154
155  Serial.println(ip);156
157  // print the received signal strength:158
159  long rssi = WiFi.RSSI();160
161  Serial.print("signal strength (RSSI):");162
163  Serial.print(rssi);164
165  Serial.println(" dBm");166}WifiNINA WiFi Web Client
This example shows you how to make a HTTP request using a WiFi module equipped board. It returns a Google search for the term "Arduino". The results of this search are viewable as HTML through your Arduino Software (IDE) serial window.
This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.
1/*2
3  Web client4
5 This sketch connects to a website (http://www.google.com)6
7 using the WiFi module.8
9 This example is written for a network using WPA encryption. For10
11 WEP or WPA, change the Wifi.begin() call accordingly.12
13 This example is written for a network using WPA encryption. For14
15 WEP or WPA, change the Wifi.begin() call accordingly.16
17 Circuit:18
19 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)20
21 created 13 July 201022
23 by dlf (Metodo2 srl)24
25 modified 31 May 201226
27 by Tom Igoe28
29 */30
31#include <SPI.h>32#include <WiFiNINA.h>33
34#include "arduino_secrets.h"35///////please enter your sensitive data in the Secret tab/arduino_secrets.h36char ssid[] = SECRET_SSID;        // your network SSID (name)37char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)38int keyIndex = 0;            // your network key Index number (needed only for WEP)39
40int status = WL_IDLE_STATUS;41// if you don't want to use DNS (and reduce your sketch size)42// use the numeric IP instead of the name for the server:43//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)44char server[] = "www.google.com";    // name address for Google (using DNS)45
46// Initialize the Ethernet client library47// with the IP address and port of the server48// that you want to connect to (port 80 is default for HTTP):49
50WiFiClient client;51
52void setup() {53
54  //Initialize serial and wait for port to open:55
56  Serial.begin(9600);57
58  while (!Serial) {59
60    ; // wait for serial port to connect. Needed for native USB port only61
62  }63
64  // check for the WiFi module:65
66  if (WiFi.status() == WL_NO_MODULE) {67
68    Serial.println("Communication with WiFi module failed!");69
70    // don't continue71
72    while (true);73
74  }75
76  String fv = WiFi.firmwareVersion();77
78  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {79
80    Serial.println("Please upgrade the firmware");81
82  }83
84  // attempt to connect to Wifi network:85
86  while (status != WL_CONNECTED) {87
88    Serial.print("Attempting to connect to SSID: ");89
90    Serial.println(ssid);91
92    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:93
94    status = WiFi.begin(ssid, pass);95
96    // wait 10 seconds for connection:97
98    delay(10000);99
100  }101
102  Serial.println("Connected to wifi");103
104  printWifiStatus();105
106  Serial.println("\nStarting connection to server...");107
108  // if you get a connection, report back via serial:109
110  if (client.connect(server, 80)) {111
112    Serial.println("connected to server");113
114    // Make a HTTP request:115
116    client.println("GET /search?q=arduino HTTP/1.1");117
118    client.println("Host: www.google.com");119
120    client.println("Connection: close");121
122    client.println();123
124  }125}126
127void loop() {128
129  // if there are incoming bytes available130
131  // from the server, read them and print them:132
133  while (client.available()) {134
135    char c = client.read();136
137    Serial.write(c);138
139  }140
141  // if the server's disconnected, stop the client:142
143  if (!client.connected()) {144
145    Serial.println();146
147    Serial.println("disconnecting from server.");148
149    client.stop();150
151    // do nothing forevermore:152
153    while (true);154
155  }156}157
158void printWifiStatus() {159
160  // print the SSID of the network you're attached to:161
162  Serial.print("SSID: ");163
164  Serial.println(WiFi.SSID());165
166  // print your board's IP address:167
168  IPAddress ip = WiFi.localIP();169
170  Serial.print("IP Address: ");171
172  Serial.println(ip);173
174  // print the received signal strength:175
176  long rssi = WiFi.RSSI();177
178  Serial.print("signal strength (RSSI):");179
180  Serial.print(rssi);181
182  Serial.println(" dBm");183}WifiNINA WiFi Web Client Repeating
This example shows you how to make repeated HTTP requests using a WiFi equipped board. It connects to http://example.org. The content of the page is viewable through your Arduino Software (IDE) Serial Monitor window.
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  Repeating Wifi Web Client4
5 This sketch connects to a a web server and makes a request6
7 using a WiFi equipped Arduino board.8
9 created 23 April 201210
11 modified 31 May 201212
13 by Tom Igoe14
15 modified 13 Jan 201416
17 by Federico Vanzati18
19 https://docs.arduino.cc/library-examples/wifi-library/WiFiWebClientRepeating20
21 This code is in the public domain.22
23 */24
25#include <SPI.h>26#include <WiFiNINA.h>27
28#include "arduino_secrets.h"29///////please enter your sensitive data in the Secret tab/arduino_secrets.h30char 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
36// Initialize the Wifi client library37
38WiFiClient client;39
40// server address:41char server[] = "example.org";42//IPAddress server(64,131,82,241);43
44unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds45
46const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds47
48void setup() {49
50  //Initialize serial 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  // you're connected now, so print out the status:99
100  printWifiStatus();101}102
103void loop() {104
105  // if there's incoming data from the net connection.106
107  // send it out the serial port.  This is for debugging108
109  // purposes only:110
111  while (client.available()) {112
113    char c = client.read();114
115    Serial.write(c);116
117  }118
119  // if ten seconds have passed since your last connection,120
121  // then connect again and send data:122
123  if (millis() - lastConnectionTime > postingInterval) {124
125    httpRequest();126
127  }128
129}130
131// this method makes a HTTP connection to the server:132void httpRequest() {133
134  // close any connection before send a new request.135
136  // This will free the socket on the Nina module137
138  client.stop();139
140  // if there's a successful connection:141
142  if (client.connect(server, 80)) {143
144    Serial.println("connecting...");145
146    // send the HTTP PUT request:147
148    client.println("GET / HTTP/1.1");149
150    client.println("Host: example.org");151
152    client.println("User-Agent: ArduinoWiFi/1.1");153
154    client.println("Connection: close");155
156    client.println();157
158    // note the time that the connection was made:159
160    lastConnectionTime = millis();161
162  } else {163
164    // if you couldn't make a connection:165
166    Serial.println("connection failed");167
168  }169}170
171void printWifiStatus() {172
173  // print the SSID of the network you're attached to:174
175  Serial.print("SSID: ");176
177  Serial.println(WiFi.SSID());178
179  // print your board's IP address:180
181  IPAddress ip = WiFi.localIP();182
183  Serial.print("IP Address: ");184
185  Serial.println(ip);186
187  // print the received signal strength:188
189  long rssi = WiFi.RSSI();190
191  Serial.print("signal strength (RSSI):");192
193  Serial.print(rssi);194
195  Serial.println(" dBm");196}WifiNINA WiFi Web Server
In this example, you will use the WiFi module of your board to create a simple Web server. Using the WiFi 1010 library, your device will be able to answer a HTTP request received from the WiFI connection. After opening a browser and navigating to your WiFi's IP address, your board will respond with just enough HTML for a browser to display the input values from all six analog pins.
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  WiFi Web Server3
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. For7 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 201013 by dlf (Metodo2 srl)14 modified 31 May 201215 by Tom Igoe16
17 */18
19#include <SPI.h>20#include <WiFiNINA.h>21
22
23#include "arduino_secrets.h" 24///////please enter your sensitive data in the Secret tab/arduino_secrets.h25char ssid[] = SECRET_SSID;        // your network SSID (name)26char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)27int keyIndex = 0;                 // your network key Index number (needed only for WEP)28
29int status = WL_IDLE_STATUS;30
31WiFiServer server(80);32
33void setup() {34  //Initialize serial and wait for port to open:35  Serial.begin(9600);36  while (!Serial) {37    ; // wait for serial port to connect. Needed for native USB port only38  }39
40  // check for the WiFi module:41  if (WiFi.status() == WL_NO_MODULE) {42    Serial.println("Communication with WiFi module failed!");43    // don't continue44    while (true);45  }46
47  String fv = WiFi.firmwareVersion();48  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {49    Serial.println("Please upgrade the firmware");50  }51
52  // attempt to connect to Wifi network:53  while (status != WL_CONNECTED) {54    Serial.print("Attempting to connect to SSID: ");55    Serial.println(ssid);56    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:57    status = WiFi.begin(ssid, pass);58
59    // wait 10 seconds for connection:60    delay(10000);61  }62  server.begin();63  // you're connected now, so print out the status:64  printWifiStatus();65}66
67
68void loop() {69  // listen for incoming clients70  WiFiClient client = server.available();71  if (client) {72    Serial.println("new client");73    // an http request ends with a blank line74    boolean currentLineIsBlank = true;75    while (client.connected()) {76      if (client.available()) {77        char c = client.read();78        Serial.write(c);79        // if you've gotten to the end of the line (received a newline80        // character) and the line is blank, the http request has ended,81        // so you can send a reply82        if (c == '\n' && currentLineIsBlank) {83          // send a standard http response header84          client.println("HTTP/1.1 200 OK");85          client.println("Content-Type: text/html");86          client.println("Connection: close");  // the connection will be closed after completion of the response87          client.println("Refresh: 5");  // refresh the page automatically every 5 sec88          client.println();89          client.println("<!DOCTYPE HTML>");90          client.println("<html>");91          // output the value of each analog input pin92          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {93            int sensorReading = analogRead(analogChannel);94            client.print("analog input ");95            client.print(analogChannel);96            client.print(" is ");97            client.print(sensorReading);98            client.println("<br />");99          }100          client.println("</html>");101          break;102        }103        if (c == '\n') {104          // you're starting a new line105          currentLineIsBlank = true;106        } else if (c != '\r') {107          // you've gotten a character on the current line108          currentLineIsBlank = false;109        }110      }111    }112    // give the web browser time to receive the data113    delay(1);114
115    // close the connection:116    client.stop();117    Serial.println("client disconnected");118  }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}NINA Firmware
Firmware Version
It is important to check firmware version against WiFiNNINA library version so that they are both aligned. Having different versions of library and firmware may create compatibility issues.
Code
When you load the sketch on the board, it will wait for a serial monitor console to be opened on your computer, then it prints out the result of the check between the expected firmware and the one available. If everything is fine, then you can proceed with your tasks, otherwise you should update the firmware following this update procedure.
The sketch can be found in the snippet below:
1/*2
3 * This example check if the firmware loaded on the NINA module4
5 * is updated.6
7 *8
9 * Circuit:10
11 * - Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)12
13 *14
15 * Created 17 October 2018 by Riccardo Rosario Rizzo16
17 * This code is in the public domain.18
19 */20#include <SPI.h>21#include <WiFiNINA.h>22
23void setup() {24
25  // Initialize serial26
27  Serial.begin(9600);28
29  while (!Serial) {30
31    ; // wait for serial port to connect. Needed for native USB port only32
33  }34
35  // Print a welcome message36
37  Serial.println("WiFiNINA firmware check.");38
39  Serial.println();40
41  // check for the WiFi module:42
43  if (WiFi.status() == WL_NO_MODULE) {44
45    Serial.println("Communication with WiFi module failed!");46
47    // don't continue48
49    while (true);50
51  }52
53  // Print firmware version on the module54
55  String fv = WiFi.firmwareVersion();56
57  String latestFv;58
59  Serial.print("Firmware version installed: ");60
61  Serial.println(fv);62
63  latestFv = WIFI_FIRMWARE_LATEST_VERSION;64
65  // Print required firmware version66
67  Serial.print("Latest firmware version available : ");68
69  Serial.println(latestFv);70
71  // Check if the latest version is installed72
73  Serial.println();74
75  if (fv >= latestFv) {76
77    Serial.println("Check result: PASSED");78
79  } else {80
81    Serial.println("Check result: NOT PASSED");82
83    Serial.println(" - The firmware version on the module do not match the");84
85    Serial.println("   version required by the library, you may experience");86
87    Serial.println("   issues or failures.");88
89  }90}91
92void loop() {93
94  // do nothing95}Firmware Update
To update the firmware on your NINA module, you can visit the Firmware/Certificate uploader for u-Blox NINA modules tutorial for instructions.
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.
 
  