Guide to WiFi101

Find examples & utilities for using the WiFi101 library, designed for the MKR 1000 WiFi and WiFi 101 Shield (retired).

This article was revised on 2021/11/29 by Karl Söderby & Benjamin Dannegård.

The WiFi101 library is designed for Arduino products using a WINC 1500 module. In this article you will find a series of examples that can be uploaded to your board.

You can also visit the WiFi101 GitHub repository to learn more about this library.

Hardware Required

This library is also compatible with the Arduino WiFi Shield 101(retired). This shield can be mounted on top of a board, such as the Arduino UNO.

Circuit

  • If you have a Wi-Fi Shield 101, mount it on top of your Arduino UNO.
  • If you have a MKR 1000 WiFi, no additional circuit is needed!

Digital pin 7 is used as a handshake pin between the WiFi Shield 101 and the board, and should not be used.

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.

WiFi101 Firmware

Check Firmware Version

You can check the firmware of your board/shield by using the sketch below.

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.

1/*
2
3 * This example check if the firmware loaded on the WiFi101
4
5 * shield is updated.
6
7 *
8
9 * Circuit:
10
11 * - WiFi101 Shield attached
12
13 *
14
15 * Created 29 July 2015 by Cristian Maglie
16
17 * This code is in the public domain.
18
19 */
20#include <SPI.h>
21#include <WiFi101.h>
22#include <driver/source/nmasic.h>
23
24void setup() {
25
26 // Initialize serial
27
28 Serial.begin(9600);
29
30 while (!Serial) {
31
32 ; // wait for serial port to connect. Needed for native USB port only
33
34 }
35
36 // Print a welcome message
37
38 Serial.println("WiFi101 firmware check.");
39
40 Serial.println();
41
42 // Check for the presence of the shield
43
44 Serial.print("WiFi101 shield: ");
45
46 if (WiFi.status() == WL_NO_SHIELD) {
47
48 Serial.println("NOT PRESENT");
49
50 return; // don't continue
51
52 }
53
54 Serial.println("DETECTED");
55
56 // Print firmware version on the shield
57
58 String fv = WiFi.firmwareVersion();
59
60 String latestFv;
61
62 Serial.print("Firmware version installed: ");
63
64 Serial.println(fv);
65
66 if (REV(GET_CHIPID()) >= REV_3A0) {
67
68 // model B
69
70 latestFv = WIFI_FIRMWARE_LATEST_MODEL_B;
71
72 } else {
73
74 // model A
75
76 latestFv = WIFI_FIRMWARE_LATEST_MODEL_A;
77
78 }
79
80 // Print required firmware version
81
82 Serial.print("Latest firmware version available : ");
83
84 Serial.println(latestFv);
85
86 // Check if the latest version is installed
87
88 Serial.println();
89
90 if (fv >= latestFv) {
91
92 Serial.println("Check result: PASSED");
93
94 } else {
95
96 Serial.println("Check result: NOT PASSED");
97
98 Serial.println(" - The firmware version on the shield do not match the");
99
100 Serial.println(" version required by the library, you may experience");
101
102 Serial.println(" issues or failures.");
103
104 }
105}
106
107void loop() {
108
109 // do nothing
110}

Update Firmware / Load Certificates

The 19.6.1 firmware is only available for model B of the WINC1500, this is used in the MKR1000 board. Unfortunately, the WiFi101 shield uses model A, which Atmel has stopped supporting, so there is no 19.6.1 firmware release for it, 19.4.4 will be the latest firmware version that is compatible.

To simplify the process, we have prepared a specific sketch - this FirmwareUpdater - that you must load on the host board (either the one with the shield plugged in, or the MKR1000 itself) and an easy to use plug-in available in Arduino Software (IDE) 1.6.10 onwards.

The

FirmwareUpdater.ino
sketch is available in Examples > WiFi101

Select the "FirmwareUpdater" example.
Select the "FirmwareUpdater" example.

When you load the sketch on the board, it prepares the communication between the plug-in and the WiFi chip. It opens up the communication through the serial port to the WiFi module hosted on the board. It is necessary to perform all the procedures managed by the Firmware Upgrader Plugin. Everything will be managed by the plug-in, but it is important to upload this sketch first.

Upload the sketch and keep the board (either the one with the shield plugged in, or the MKR1000 itself) connected to the computer.

Once done, open the plug-in that is available in the tools menu.

Navigate to the firmware updater tool.
Navigate to the firmware updater tool.

Select your board and firmware.
Select your board and firmware.

Your board should be in the list of the available serial ports. If not, please check that it is properly configured in the Tools menu.

To update the firmware you should choose the right typer of board. You can find your model looking at the WiFi module: the first line in the sticker or the last line of the silk print on the right side of the PCB shows the microcontroller model. It can be either MR210PA or MR510PB and the last letter shows yor model accordingly.

Find the model.
Find the model.

Choose in the dropdown list the model corresponding to your unit and proceed clicking on the Update Firmware button. A bar at the bottom will show you the progress of the procedure that includes erasing, writing and verifying of the firmware. At the end you get a clear notice of the successful operation.

Firmware has been updated.
Firmware has been updated.

Certificate Uploading

With the same procedure, you may load root certificates on the WiFi module to access securely specific websites. Your board must be running the FirmwareUpdater sketch to work .The root certificates are issued by a limited number of certification authorities, but it is difficult to know which site is using which authority. To ease your life, we allow you to specify directly the URL to which you need to connect securely, leaving to us the task to download the root certificate. The list you are building is not saved from one session to the next one. It might happen that a few websites share the same root certificate. You don't have to worry about this as we take care of it. The space available on your WiFi module to store the certificates is limited to around 10 certificates that, being issued by a limited number of authorities, should be more than enough for the average projects.

The procedure starts connecting your board (either the one with the shield plugged in, or the MKR1000 itself) to your computer and selecting it from the Tools menu of the Arduino Software (IDE). Load the FirmwareUpdater on the board and launch the WiFi 101 Firmware Updater from Tools and go to the third section of the interface.

Adding SSL root certificates.
Adding SSL root certificates.

There you find on the left an empty list and on the right the buttons to add or remove the URL from which you want to download the root certificates. The URL should be exactly the one to which you need to connect. Add all the websites' URLs needed and then proceed with the uploading process. Please remember that you erase all the existing certificates when you load a new set. Press the ''Upload Certificates to WiFi module" and wait for the confirmation message.

Certificates loaded successfully.
Certificates loaded successfully.

Examples

Below are a set of examples to test out the WiFi101 Library.

Wifi101 Connect No Encryption

This example shows you how to connect to an open (not encrypted) 802.11b/g network with the Arduino WiFi shield 101 or a MKR1000. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.

1/*
2 This example connects to an unencrypted WiFi network.
3
4 Then it prints the MAC address of the WiFi shield,
5
6 the IP address obtained, and other network details.
7
8 Circuit:
9
10 * WiFi shield attached
11
12 created 13 July 2010
13
14 by dlf (Metodo2 srl)
15
16 modified 31 May 2012
17
18 by Tom Igoe
19
20 */
21#include <SPI.h>
22#include <WiFi101.h>
23#include "arduino_secrets.h"
24///////please enter your sensitive data in the Secret tab/arduino_secrets.h
25char ssid[] = SECRET_SSID; // your network SSID (name)
26int status = WL_IDLE_STATUS; // the WiFi radio's status
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 only
37
38 }
39
40 // check for the presence of the shield:
41
42 if (WiFi.status() == WL_NO_SHIELD) {
43
44 Serial.println("WiFi shield not present");
45
46 // don't continue:
47
48 while (true);
49
50 }
51
52 // attempt to connect to WiFi network:
53
54 while ( status != WL_CONNECTED) {
55
56 Serial.print("Attempting to connect to open SSID: ");
57
58 Serial.println(ssid);
59
60 status = WiFi.begin(ssid);
61
62 // wait 10 seconds for connection:
63
64 delay(10000);
65
66 }
67
68 // you're connected now, so print out the data:
69
70 Serial.print("You're connected to the network");
71
72 printCurrentNet();
73
74 printWiFiData();
75}
76
77void loop() {
78
79 // check the network connection once every 10 seconds:
80
81 delay(10000);
82
83 printCurrentNet();
84}
85
86void printWiFiData() {
87
88 // print your WiFi shield's IP address:
89
90 IPAddress ip = WiFi.localIP();
91
92 Serial.print("IP Address: ");
93
94 Serial.println(ip);
95
96 Serial.println(ip);
97
98 // print your MAC address:
99
100 byte mac[6];
101
102 WiFi.macAddress(mac);
103
104 Serial.print("MAC address: ");
105
106 printMacAddress(mac);
107
108 // print your subnet mask:
109
110 IPAddress subnet = WiFi.subnetMask();
111
112 Serial.print("NetMask: ");
113
114 Serial.println(subnet);
115
116 // print your gateway address:
117
118 IPAddress gateway = WiFi.gatewayIP();
119
120 Serial.print("Gateway: ");
121
122 Serial.println(gateway);
123}
124
125void printCurrentNet() {
126
127 // print the SSID of the network you're attached to:
128
129 Serial.print("SSID: ");
130
131 Serial.println(WiFi.SSID());
132
133 // print the MAC address of the router you're attached to:
134
135 byte bssid[6];
136
137 WiFi.BSSID(bssid);
138
139 Serial.print("BSSID: ");
140
141 printMacAddress(bssid);
142
143 // print the received signal strength:
144
145 long rssi = WiFi.RSSI();
146
147 Serial.print("signal strength (RSSI):");
148
149 Serial.println(rssi);
150
151 // print the encryption type:
152
153 byte encryption = WiFi.encryptionType();
154
155 Serial.print("Encryption Type:");
156
157 Serial.println(encryption, HEX);
158}
159
160void printMacAddress(byte mac[]) {
161
162 for (int i = 5; i >= 0; i--) {
163
164 if (mac[i] < 16) {
165
166 Serial.print("0");
167
168 }
169
170 Serial.print(mac[i], HEX);
171
172 if (i > 0) {
173
174 Serial.print(":");
175
176 }
177
178 }
179
180 Serial.println();
181}

Wifi101 Connect With WEP

This example shows you how to connect to a WEP encrypted 802.11b/g network with the Arduino WiFi shield 101 or a MKR1000 board. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.

1/*
2
3 This example connects to a WEP-encrypted WiFi network.
4
5 Then it prints the MAC address of the WiFi shield,
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 work
14
15 (too short) and ABBAISDEAF won't work (I and S are not
16
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 Circuit:
26
27 * WiFi shield attached
28
29 created 13 July 2010
30
31 by dlf (Metodo2 srl)
32
33 modified 31 May 2012
34
35 by Tom Igoe
36
37 */
38#include <SPI.h>
39#include <WiFi101.h>
40
41#include "arduino_secrets.h"
42///////please enter your sensitive data in the Secret tab/arduino_secrets.h
43char ssid[] = SECRET_SSID; // your network SSID (name)
44char key[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
45int keyIndex = 0; // your network key Index number
46int status = WL_IDLE_STATUS; // the WiFi radio's status
47
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 only
57
58 }
59
60 // check for the presence of the shield:
61
62 if (WiFi.status() == WL_NO_SHIELD) {
63
64 Serial.println("WiFi shield not present");
65
66 // don't continue:
67
68 while (true);
69
70 }
71
72 // attempt to connect to WiFi network:
73
74 while ( status != WL_CONNECTED) {
75
76 Serial.print("Attempting to connect to WEP network, SSID: ");
77
78 Serial.println(ssid);
79
80 status = WiFi.begin(ssid, keyIndex, key);
81
82 // wait 10 seconds for connection:
83
84 delay(10000);
85
86 }
87
88 // once you are connected :
89
90 Serial.print("You're connected to the network");
91
92 printCurrentNet();
93
94 printWiFiData();
95}
96
97void loop() {
98
99 // check the network connection once every 10 seconds:
100
101 delay(10000);
102
103 printCurrentNet();
104}
105
106void printWiFiData() {
107
108 // print your WiFi shield's IP address:
109
110 IPAddress ip = WiFi.localIP();
111
112 Serial.print("IP Address: ");
113
114 Serial.println(ip);
115
116 Serial.println(ip);
117
118 // print your MAC address:
119
120 byte mac[6];
121
122 WiFi.macAddress(mac);
123
124 Serial.print("MAC address: ");
125
126 printMacAddress(mac);
127}
128
129void printCurrentNet() {
130
131 // print the SSID of the network you're attached to:
132
133 Serial.print("SSID: ");
134
135 Serial.println(WiFi.SSID());
136
137 // print the MAC address of the router you're attached to:
138
139 byte bssid[6];
140
141 WiFi.BSSID(bssid);
142
143 Serial.print("BSSID: ");
144
145 printMacAddress(bssid);
146
147 // print the received signal strength:
148
149 long rssi = WiFi.RSSI();
150
151 Serial.print("signal strength (RSSI):");
152
153 Serial.println(rssi);
154
155 // print the encryption type:
156
157 byte encryption = WiFi.encryptionType();
158
159 Serial.print("Encryption Type:");
160
161 Serial.println(encryption, HEX);
162
163 Serial.println();
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}

Wifi101 Connect With WPA

This example shows you how to connect to a WPA2 Personal encrypted 802.11b/g network with the Arduino WiFi Shield 101 or a MKR1000 board. Your Arduino Software (IDE) serial monitor will provide information about the connection once it has connected.

1/*
2
3 This example connects to an unencrypted WiFi network.
4
5 Then it prints the MAC address of the WiFi shield,
6
7 the IP address obtained, and other network details.
8
9 Circuit:
10
11 * WiFi shield attached
12
13 created 13 July 2010
14
15 by dlf (Metodo2 srl)
16
17 modified 31 May 2012
18
19 by Tom Igoe
20
21 */
22#include <SPI.h>
23#include <WiFi101.h>
24
25#include "arduino_secrets.h"
26///////please enter your sensitive data in the Secret tab/arduino_secrets.h
27char ssid[] = SECRET_SSID; // your network SSID (name)
28char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
29int status = WL_IDLE_STATUS; // the WiFi radio's status
30
31void setup() {
32
33 //Initialize serial and wait for port to open:
34
35 Serial.begin(9600);
36
37 while (!Serial) {
38
39 ; // wait for serial port to connect. Needed for native USB port only
40
41 }
42
43 // check for the presence of the shield:
44
45 if (WiFi.status() == WL_NO_SHIELD) {
46
47 Serial.println("WiFi shield not present");
48
49 // don't continue:
50
51 while (true);
52
53 }
54
55 // attempt to connect to WiFi network:
56
57 while ( status != WL_CONNECTED) {
58
59 Serial.print("Attempting to connect to WPA SSID: ");
60
61 Serial.println(ssid);
62
63 // Connect to WPA/WPA2 network:
64
65 status = WiFi.begin(ssid, pass);
66
67 // wait 10 seconds for connection:
68
69 delay(10000);
70
71 }
72
73 // you're connected now, so print out the data:
74
75 Serial.print("You're connected to the network");
76
77 printCurrentNet();
78
79 printWiFiData();
80
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 WiFi shield'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}
115
116void printCurrentNet() {
117
118 // print the SSID of the network you're attached to:
119
120 Serial.print("SSID: ");
121
122 Serial.println(WiFi.SSID());
123
124 // print the MAC address of the router you're attached to:
125
126 byte bssid[6];
127
128 WiFi.BSSID(bssid);
129
130 Serial.print("BSSID: ");
131
132 printMacAddress(bssid);
133
134 // print the received signal strength:
135
136 long rssi = WiFi.RSSI();
137
138 Serial.print("signal strength (RSSI):");
139
140 Serial.println(rssi);
141
142 // print the encryption type:
143
144 byte encryption = WiFi.encryptionType();
145
146 Serial.print("Encryption Type:");
147
148 Serial.println(encryption, HEX);
149
150 Serial.println();
151}
152
153void printMacAddress(byte mac[]) {
154
155 for (int i = 5; i >= 0; i--) {
156
157 if (mac[i] < 16) {
158
159 Serial.print("0");
160
161 }
162
163 Serial.print(mac[i], HEX);
164
165 if (i > 0) {
166
167 Serial.print(":");
168
169 }
170
171 }
172
173 Serial.println();
174}

Wifi101 Scan Networks

This example scans for 802.11b/g networks with the Arduino WiFi Shield 101 or a MKR1000 board. 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.

1/*
2
3 This example prints the WiFi shield's MAC address, and
4
5 scans for available WiFi networks using the WiFi shield.
6
7 Every ten seconds, it scans again. It doesn't actually
8
9 connect to any network, so no encryption scheme is specified.
10
11 Circuit:
12
13 * WiFi shield attached
14
15 created 13 July 2010
16
17 by dlf (Metodo2 srl)
18
19 modified 21 Junn 2012
20
21 by Tom Igoe and Jaymes Dec
22
23 */
24
25#include <SPI.h>
26#include <WiFi101.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 only
37
38 }
39
40 // check for the presence of the shield:
41
42 if (WiFi.status() == WL_NO_SHIELD) {
43
44 Serial.println("WiFi shield not present");
45
46 // don't continue:
47
48 while (true);
49
50 }
51
52 // Print WiFi MAC address:
53
54 printMacAddress();
55
56 // scan for existing networks:
57
58 Serial.println("Scanning available networks...");
59
60 listNetworks();
61}
62
63void loop() {
64
65 delay(10000);
66
67 // scan for existing networks:
68
69 Serial.println("Scanning available networks...");
70
71 listNetworks();
72}
73
74void printMacAddress() {
75
76 // the MAC address of your WiFi shield
77
78 byte mac[6];
79
80 // print your MAC address:
81
82 WiFi.macAddress(mac);
83
84 Serial.print("MAC: ");
85
86 printMacAddress(mac);
87}
88
89void listNetworks() {
90
91 // scan for nearby networks:
92
93 Serial.println("** Scan Networks **");
94
95 int numSsid = WiFi.scanNetworks();
96
97 if (numSsid == -1)
98
99 {
100
101 Serial.println("Couldn't get a wifi connection");
102
103 while (true);
104
105 }
106
107 // print the list of networks seen:
108
109 Serial.print("number of available networks:");
110
111 Serial.println(numSsid);
112
113 // print the network number and name for each network found:
114
115 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
116
117 Serial.print(thisNet);
118
119 Serial.print(") ");
120
121 Serial.print(WiFi.SSID(thisNet));
122
123 Serial.print("\tSignal: ");
124
125 Serial.print(WiFi.RSSI(thisNet));
126
127 Serial.print(" dBm");
128
129 Serial.print("\tEncryption: ");
130
131 printEncryptionType(WiFi.encryptionType(thisNet));
132
133 Serial.flush();
134
135 }
136}
137
138void printEncryptionType(int thisType) {
139
140 // read the encryption type and print out the name:
141
142 switch (thisType) {
143
144 case ENC_TYPE_WEP:
145
146 Serial.println("WEP");
147
148 break;
149
150 case ENC_TYPE_TKIP:
151
152 Serial.println("WPA");
153
154 break;
155
156 case ENC_TYPE_CCMP:
157
158 Serial.println("WPA2");
159
160 break;
161
162 case ENC_TYPE_NONE:
163
164 Serial.println("None");
165
166 break;
167
168 case ENC_TYPE_AUTO:
169
170 Serial.println("Auto");
171
172 break;
173
174 }
175}
176
177void printMacAddress(byte mac[]) {
178
179 for (int i = 5; i >= 0; i--) {
180
181 if (mac[i] < 16) {
182
183 Serial.print("0");
184
185 }
186
187 Serial.print(mac[i], HEX);
188
189 if (i > 0) {
190
191 Serial.print(":");
192
193 }
194
195 }
196
197 Serial.println();
198}

Wifi101 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 Shield 101 or MKR1000 board (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 the IP address of your shield/board 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.

1Access Point Web Server
2
3Creating access point named: MKR1000-network
4
5SSID: MKR1000-network
6
7IP Address: 192.168.1.1
8
9signal strength (RSSI):-100 dBm
10
11To see this page in action, open a browser to http://192.168.1.1

Every connection is reported on the Serial Monitor as well and contains information that is related to the connecting client. In the following example, the client is Chrome on OSX:

1new client
2
3GET / HTTP/1.1
4
5Host: 192.168.1.1
6
7Connection: keep-alive
8
9Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
10
11Upgrade-Insecure-Requests: 1
12
13User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
14
15Accept-Encoding: gzip, deflate, sdch
16
17Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2
18
19client disconnected
20
21new client
22
23GET /favicon.ico HTTP/1.1
24
25Host: 192.168.1.1
26
27Connection: keep-alive
28
29User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
30
31Accept: */*
32
33Referer: http://192.168.1.1/
34
35Accept-Encoding: gzip, deflate, sdch
36
37Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2
38
39client disconnected

The complete sketch of this tutorial is below.

1/*
2
3 WiFi Web Server LED Blink
4
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 Shield (once connected)
8
9 to the Serial monitor. From there, you can open that address in a web browser
10
11 to turn on and off the LED on pin 9.
12
13 If the IP address of your shield is yourAddress:
14
15 http://yourAddress/H turns the LED on
16
17 http://yourAddress/L turns it off
18
19 This example is written for a network using WPA encryption. For
20
21 WEP or WPA, change the WiFi.begin() call accordingly.
22
23 Circuit:
24
25 * WiFi shield attached
26
27 * LED attached to pin 9
28
29 created 25 Nov 2012
30
31 by Tom Igoe
32
33 */
34#include <SPI.h>
35#include <WiFi101.h>
36
37#include "arduino_secrets.h"
38///////please enter your sensitive data in the Secret tab/arduino_secrets.h
39char 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 communication
50
51 pinMode(9, OUTPUT); // set the LED pin mode
52
53 // check for the presence of the shield:
54
55 if (WiFi.status() == WL_NO_SHIELD) {
56
57 Serial.println("WiFi shield not present");
58
59 while (true); // don't continue
60
61 }
62
63 // attempt to connect to WiFi network:
64
65 while ( status != WL_CONNECTED) {
66
67 Serial.print("Attempting to connect to Network named: ");
68
69 Serial.println(ssid); // print the network name (SSID);
70
71 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
72
73 status = WiFi.begin(ssid, pass);
74
75 // wait 10 seconds for connection:
76
77 delay(10000);
78
79 }
80
81 server.begin(); // start the web server on port 80
82
83 printWiFiStatus(); // you're connected now, so print out the status
84}
85
86void loop() {
87
88 WiFiClient client = server.available(); // listen for incoming clients
89
90 if (client) { // if you get a client,
91
92 Serial.println("new client"); // print a message out the serial port
93
94 String currentLine = ""; // make a String to hold incoming data from the client
95
96 while (client.connected()) { // loop while the client's connected
97
98 if (client.available()) { // if there's bytes to read from the client,
99
100 char c = client.read(); // read a byte, then
101
102 Serial.write(c); // print it out the serial monitor
103
104 if (c == '\n') { // if the byte is a newline character
105
106 // if the current line is blank, you got two newline characters in a row.
107
108 // that's the end of the client HTTP request, so send a response:
109
110 if (currentLine.length() == 0) {
111
112 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
113
114 // and a content-type so the client knows what's coming, then a blank line:
115
116 client.println("HTTP/1.1 200 OK");
117
118 client.println("Content-type:text/html");
119
120 client.println();
121
122 // the content of the HTTP response follows the header:
123
124 client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
125
126 client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
127
128 // The HTTP response ends with another blank line:
129
130 client.println();
131
132 // break out of the while loop:
133
134 break;
135
136 }
137
138 else { // if you got a newline, then clear currentLine:
139
140 currentLine = "";
141
142 }
143
144 }
145
146 else if (c != '\r') { // if you got anything else but a carriage return character,
147
148 currentLine += c; // add it to the end of the currentLine
149
150 }
151
152 // Check to see if the client request was "GET /H" or "GET /L":
153
154 if (currentLine.endsWith("GET /H")) {
155
156 digitalWrite(9, HIGH); // GET /H turns the LED on
157
158 }
159
160 if (currentLine.endsWith("GET /L")) {
161
162 digitalWrite(9, LOW); // GET /L turns the LED off
163
164 }
165
166 }
167
168 }
169
170 // close the connection:
171
172 client.stop();
173
174 Serial.println("client disconnected");
175
176 }
177}
178
179void printWiFiStatus() {
180
181 // print the SSID of the network you're attached to:
182
183 Serial.print("SSID: ");
184
185 Serial.println(WiFi.SSID());
186
187 // print your WiFi shield's IP address:
188
189 IPAddress ip = WiFi.localIP();
190
191 Serial.print("IP Address: ");
192
193 Serial.println(ip);
194
195 // print the received signal strength:
196
197 long rssi = WiFi.RSSI();
198
199 Serial.print("signal strength (RSSI):");
200
201 Serial.print(rssi);
202
203 Serial.println(" dBm");
204
205 // print where to go in a browser:
206
207 Serial.print("To see this page in action, open a browser to http://");
208
209 Serial.println(ip);
210}

Wifi101 Udp NTP Client

In this example, you will use your WiFi Shield 101 with your Arduino Zero board, or a MKR1000 board, to query a Network Time Protocol (NTP) server. In this way, your board can get the time from the Internet.

1/*
2
3 Udp NTP Client
4
5 Get the time from a Network Time Protocol (NTP) time server
6
7 Demonstrates use of UDP sendPacket and ReceivePacket
8
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_Protocol
12
13 created 4 Sep 2010
14
15 by Michael Margolis
16
17 modified 9 Apr 2012
18
19 by Tom Igoe
20
21 This code is in the public domain.
22
23 */
24
25#include <SPI.h>
26#include <WiFi101.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.h
32char 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 packets
37
38IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
39
40const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
41
42byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
43
44// A UDP instance to let us send and receive packets over UDP
45
46WiFiUDP Udp;
47
48void setup()
49{
50
51 // Open serial communications and wait for port to open:
52
53 Serial.begin(9600);
54
55 while (!Serial) {
56
57 ; // wait for serial port to connect. Needed for native USB port only
58
59 }
60
61 // check for the presence of the shield:
62
63 if (WiFi.status() == WL_NO_SHIELD) {
64
65 Serial.println("WiFi shield not present");
66
67 // don't continue:
68
69 while (true);
70
71 }
72
73 // attempt to connect to WiFi network:
74
75 while ( status != WL_CONNECTED) {
76
77 Serial.print("Attempting to connect to SSID: ");
78
79 Serial.println(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 Serial.println("Connected to wifi");
92
93 printWiFiStatus();
94
95 Serial.println("\nStarting connection to server...");
96
97 Udp.begin(localPort);
98}
99
100void loop()
101{
102
103 sendNTPpacket(timeServer); // send an NTP packet to a time server
104
105 // wait to see if a reply is available
106
107 delay(1000);
108
109 if ( Udp.parsePacket() ) {
110
111 Serial.println("packet received");
112
113 // We've received a packet, read the data from it
114
115 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
116
117 //the timestamp starts at byte 40 of the received packet and is four bytes,
118
119 // or two words, long. First, esxtract the two words:
120
121 unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
122
123 unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
124
125 // combine the four bytes (two words) into a long integer
126
127 // this is NTP time (seconds since Jan 1 1900):
128
129 unsigned long secsSince1900 = highWord << 16 | lowWord;
130
131 Serial.print("Seconds since Jan 1 1900 = " );
132
133 Serial.println(secsSince1900);
134
135 // now convert NTP time into everyday time:
136
137 Serial.print("Unix time = ");
138
139 // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
140
141 const unsigned long seventyYears = 2208988800UL;
142
143 // subtract seventy years:
144
145 unsigned long epoch = secsSince1900 - seventyYears;
146
147 // print Unix time:
148
149 Serial.println(epoch);
150
151 // print the hour, minute and second:
152
153 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
154
155 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
156
157 Serial.print(':');
158
159 if ( ((epoch % 3600) / 60) < 10 ) {
160
161 // In the first 10 minutes of each hour, we'll want a leading '0'
162
163 Serial.print('0');
164
165 }
166
167 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
168
169 Serial.print(':');
170
171 if ( (epoch % 60) < 10 ) {
172
173 // In the first 10 seconds of each minute, we'll want a leading '0'
174
175 Serial.print('0');
176
177 }
178
179 Serial.println(epoch % 60); // print the second
180
181 }
182
183 // wait ten seconds before asking for the time again
184
185 delay(10000);
186}
187
188// send an NTP request to the time server at the given address
189unsigned long sendNTPpacket(IPAddress& address)
190{
191
192 //Serial.println("1");
193
194 // set all bytes in the buffer to 0
195
196 memset(packetBuffer, 0, NTP_PACKET_SIZE);
197
198 // Initialize values needed to form NTP request
199
200 // (see URL above for details on the packets)
201
202 //Serial.println("2");
203
204 packetBuffer[0] = 0b11100011; // LI, Version, Mode
205
206 packetBuffer[1] = 0; // Stratum, or type of clock
207
208 packetBuffer[2] = 6; // Polling Interval
209
210 packetBuffer[3] = 0xEC; // Peer Clock Precision
211
212 // 8 bytes of zero for Root Delay & Root Dispersion
213
214 packetBuffer[12] = 49;
215
216 packetBuffer[13] = 0x4E;
217
218 packetBuffer[14] = 49;
219
220 packetBuffer[15] = 52;
221
222 //Serial.println("3");
223
224 // all NTP fields have been given values, now
225
226 // you can send a packet requesting a timestamp:
227
228 Udp.beginPacket(address, 123); //NTP requests are to port 123
229
230 //Serial.println("4");
231
232 Udp.write(packetBuffer, NTP_PACKET_SIZE);
233
234 //Serial.println("5");
235
236 Udp.endPacket();
237
238 //Serial.println("6");
239}
240
241void printWiFiStatus() {
242
243 // print the SSID of the network you're attached to:
244
245 Serial.print("SSID: ");
246
247 Serial.println(WiFi.SSID());
248
249 // print your WiFi shield's IP address:
250
251 IPAddress ip = WiFi.localIP();
252
253 Serial.print("IP Address: ");
254
255 Serial.println(ip);
256
257 // print the received signal strength:
258
259 long rssi = WiFi.RSSI();
260
261 Serial.print("signal strength (RSSI):");
262
263 Serial.print(rssi);
264
265 Serial.println(" dBm");
266}

Wifi101 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 shield's or MKR1000'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.

1/*
2
3 Chat Server
4
5 A simple server that distributes any incoming messages to all
6
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. For
12
13 WEP or WPA, change the WiFi.begin() call accordingly.
14
15 Circuit:
16
17 * WiFi shield attached
18
19 created 18 Dec 2009
20
21 by David A. Mellis
22
23 modified 31 May 2012
24
25 by Tom Igoe
26
27 */
28
29#include <SPI.h>
30#include <WiFi101.h>
31
32#include "arduino_secrets.h"
33///////please enter your sensitive data in the Secret tab/arduino_secrets.h
34char 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
43bool alreadyConnected = false; // whether or not the client was connected previously
44
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 only
54
55 }
56
57 // check for the presence of the shield:
58
59 if (WiFi.status() == WL_NO_SHIELD) {
60
61 Serial.println("WiFi shield not present");
62
63 // don't continue:
64
65 while (true);
66
67 }
68
69 // attempt to connect to WiFi network:
70
71 while ( status != WL_CONNECTED) {
72
73 Serial.print("Attempting to connect to SSID: ");
74
75 Serial.println(ssid);
76
77 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
78
79 status = WiFi.begin(ssid, pass);
80
81 // wait 10 seconds for connection:
82
83 delay(10000);
84
85 }
86
87 // start the server:
88
89 server.begin();
90
91 // you're connected now, so print out the status:
92
93 printWiFiStatus();
94}
95
96void loop() {
97
98 // wait for a new client:
99
100 WiFiClient client = server.available();
101
102 // when the client sends the first byte, say hello:
103
104 if (client) {
105
106 if (!alreadyConnected) {
107
108 // clead out the input buffer:
109
110 client.flush();
111
112 Serial.println("We have a new client");
113
114 client.println("Hello, client!");
115
116 alreadyConnected = true;
117
118 }
119
120 if (client.available() > 0) {
121
122 // read the bytes incoming from the client:
123
124 char thisChar = client.read();
125
126 // echo the bytes back to the client:
127
128 server.write(thisChar);
129
130 // echo the bytes to the server as well:
131
132 Serial.write(thisChar);
133
134 }
135
136 }
137}
138
139void printWiFiStatus() {
140
141 // print the SSID of the network you're attached to:
142
143 Serial.print("SSID: ");
144
145 Serial.println(WiFi.SSID());
146
147 // print your WiFi shield's IP address:
148
149 IPAddress ip = WiFi.localIP();
150
151 Serial.print("IP Address: ");
152
153 Serial.println(ip);
154
155 // print the received signal strength:
156
157 long rssi = WiFi.RSSI();
158
159 Serial.print("signal strength (RSSI):");
160
161 Serial.print(rssi);
162
163 Serial.println(" dBm");
164}

Wifi101 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 LAN using an Arduino Wifi 101 Shield and Zero Board or the MKR1000 board.

1/*
2
3 WiFi UDP Send and Receive String
4
5 This sketch wait an UDP packet on localPort using a WiFi shield.
6
7 When a packet is received an Acknowledge packet is sent to the client on port remotePort
8
9 Circuit:
10
11 * WiFi shield attached
12
13 created 30 December 2012
14
15 by dlf (Metodo2 srl)
16
17 */
18
19#include <SPI.h>
20#include <WiFi101.h>
21#include <WiFiUdp.h>
22
23int status = WL_IDLE_STATUS;
24#include "arduino_secrets.h"
25///////please enter your sensitive data in the Secret tab/arduino_secrets.h
26char ssid[] = SECRET_SSID; // your network SSID (name)
27char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
28int keyIndex = 0; // your network key Index number (needed only for WEP)
29
30unsigned int localPort = 2390; // local port to listen on
31
32char packetBuffer[255]; //buffer to hold incoming packet
33char ReplyBuffer[] = "acknowledged"; // a string to send back
34
35WiFiUDP Udp;
36
37void setup() {
38
39 //Initialize serial and wait for port to open:
40
41 Serial.begin(9600);
42
43 while (!Serial) {
44
45 ; // wait for serial port to connect. Needed for native USB port only
46
47 }
48
49 // check for the presence of the shield:
50
51 if (WiFi.status() == WL_NO_SHIELD) {
52
53 Serial.println("WiFi shield not present");
54
55 // don't continue:
56
57 while (true);
58
59 }
60
61 // attempt to connect to WiFi network:
62
63 while ( status != WL_CONNECTED) {
64
65 Serial.print("Attempting to connect to SSID: ");
66
67 Serial.println(ssid);
68
69 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
70
71 status = WiFi.begin(ssid, pass);
72
73 // wait 10 seconds for connection:
74
75 delay(10000);
76
77 }
78
79 Serial.println("Connected to wifi");
80
81 printWiFiStatus();
82
83 Serial.println("\nStarting connection to server...");
84
85 // if you get a connection, report back via serial:
86
87 Udp.begin(localPort);
88}
89
90void loop() {
91
92 // if there's data available, read a packet
93
94 int packetSize = Udp.parsePacket();
95
96 if (packetSize)
97
98 {
99
100 Serial.print("Received packet of size ");
101
102 Serial.println(packetSize);
103
104 Serial.print("From ");
105
106 IPAddress remoteIp = Udp.remoteIP();
107
108 Serial.print(remoteIp);
109
110 Serial.print(", port ");
111
112 Serial.println(Udp.remotePort());
113
114 // read the packet into packetBufffer
115
116 int len = Udp.read(packetBuffer, 255);
117
118 if (len > 0) packetBuffer[len] = 0;
119
120 Serial.println("Contents:");
121
122 Serial.println(packetBuffer);
123
124 // send a reply, to the IP address and port that sent us the packet we received
125
126 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
127
128 Udp.write(ReplyBuffer);
129
130 Udp.endPacket();
131
132 }
133}
134
135void printWiFiStatus() {
136
137 // print the SSID of the network you're attached to:
138
139 Serial.print("SSID: ");
140
141 Serial.println(WiFi.SSID());
142
143 // print your WiFi shield's IP address:
144
145 IPAddress ip = WiFi.localIP();
146
147 Serial.print("IP Address: ");
148
149 Serial.println(ip);
150
151 // print the received signal strength:
152
153 long rssi = WiFi.RSSI();
154
155 Serial.print("signal strength (RSSI):");
156
157 Serial.print(rssi);
158
159 Serial.println(" dBm");
160}

Wifi101 WiFi Web Client

This example shows you how to make a HTTP request using a WiFi Shield 101 or a MKR1000 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 client
4
5 This sketch connects to a website (http://www.google.com)
6
7 using a WiFi shield.
8
9 This example is written for a network using WPA encryption. For
10
11 WEP or WPA, change the WiFi.begin() call accordingly.
12
13 This example is written for a network using WPA encryption. For
14
15 WEP or WPA, change the WiFi.begin() call accordingly.
16
17 Circuit:
18
19 * WiFi shield attached
20
21 created 13 July 2010
22
23 by dlf (Metodo2 srl)
24
25 modified 31 May 2012
26
27 by Tom Igoe
28
29 */
30
31#include <SPI.h>
32#include <WiFi101.h>
33#include "arduino_secrets.h"
34///////please enter your sensitive data in the Secret tab/arduino_secrets.h
35char ssid[] = SECRET_SSID; // your network SSID (name)
36char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
37int keyIndex = 0; // your network key Index number (needed only for WEP)
38
39int status = WL_IDLE_STATUS;
40// if you don't want to use DNS (and reduce your sketch size)
41// use the numeric IP instead of the name for the server:
42//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
43char server[] = "www.google.com"; // name address for Google (using DNS)
44
45// Initialize the Ethernet client library
46// with the IP address and port of the server
47// that you want to connect to (port 80 is default for HTTP):
48
49WiFiClient client;
50
51void setup() {
52
53 //Initialize serial and wait for port to open:
54
55 Serial.begin(9600);
56
57 while (!Serial) {
58
59 ; // wait for serial port to connect. Needed for native USB port only
60
61 }
62
63 // check for the presence of the shield:
64
65 if (WiFi.status() == WL_NO_SHIELD) {
66
67 Serial.println("WiFi shield not present");
68
69 // don't continue:
70
71 while (true);
72
73 }
74
75 // attempt to connect to WiFi network:
76
77 while (status != WL_CONNECTED) {
78
79 Serial.print("Attempting to connect to SSID: ");
80
81 Serial.println(ssid);
82
83 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
84
85 status = WiFi.begin(ssid, pass);
86
87 // wait 10 seconds for connection:
88
89 delay(10000);
90
91 }
92
93 Serial.println("Connected to wifi");
94
95 printWiFiStatus();
96
97 Serial.println("\nStarting connection to server...");
98
99 // if you get a connection, report back via serial:
100
101 if (client.connect(server, 80)) {
102
103 Serial.println("connected to server");
104
105 // Make a HTTP request:
106
107 client.println("GET /search?q=arduino HTTP/1.1");
108
109 client.println("Host: www.google.com");
110
111 client.println("Connection: close");
112
113 client.println();
114
115 }
116}
117
118void loop() {
119
120 // if there are incoming bytes available
121
122 // from the server, read them and print them:
123
124 while (client.available()) {
125
126 char c = client.read();
127
128 Serial.write(c);
129
130 }
131
132 // if the server's disconnected, stop the client:
133
134 if (!client.connected()) {
135
136 Serial.println();
137
138 Serial.println("disconnecting from server.");
139
140 client.stop();
141
142 // do nothing forevermore:
143
144 while (true);
145
146 }
147}
148
149void printWiFiStatus() {
150
151 // print the SSID of the network you're attached to:
152
153 Serial.print("SSID: ");
154
155 Serial.println(WiFi.SSID());
156
157 // print your WiFi shield's IP address:
158
159 IPAddress ip = WiFi.localIP();
160
161 Serial.print("IP Address: ");
162
163 Serial.println(ip);
164
165 // print the received signal strength:
166
167 long rssi = WiFi.RSSI();
168
169 Serial.print("signal strength (RSSI):");
170
171 Serial.print(rssi);
172
173 Serial.println(" dBm");
174}

Wifi101 WiFi Web Client Repeating

This example shows you how to make repeated HTTP requests using a WiFi Shield 101 or a MKR1000 board. It connects to http://www.arduino.cc/latest.txt. The content of the page is viewable through your Arduino Software (IDE) Serial Monitor window.

This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.

1/*
2
3 Repeating WiFi Web Client
4
5 This sketch connects to a a web server and makes a request
6
7 using an Arduino WiFi shield.
8
9 Circuit:
10
11 * WiFi shield attached to pins SPI pins and pin 7
12
13 created 23 April 2012
14
15 modified 31 May 2012
16
17 by Tom Igoe
18
19 modified 13 Jan 2014
20
21 by Federico Vanzati
22
23 http://arduino.cc/en/Tutorial/WiFiWebClientRepeating
24
25 This code is in the public domain.
26
27 */
28
29#include <SPI.h>
30#include <WiFi101.h>
31
32#include "arduino_secrets.h"
33///////please enter your sensitive data in the Secret tab/arduino_secrets.h
34char ssid[] = SECRET_SSID; // your network SSID (name)
35char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
36int keyIndex = 0; // your network key Index number (needed only for WEP)
37
38int status = WL_IDLE_STATUS;
39
40// Initialize the WiFi client library
41
42WiFiClient client;
43
44// server address:
45char server[] = "example.org";
46//IPAddress server(64,131,82,241);
47
48unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
49
50const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
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 only
61
62 }
63
64 // check for the presence of the shield:
65
66 if (WiFi.status() == WL_NO_SHIELD) {
67
68 Serial.println("WiFi shield not present");
69
70 // don't continue:
71
72 while (true);
73
74 }
75
76 // attempt to connect to WiFi network:
77
78 while ( status != WL_CONNECTED) {
79
80 Serial.print("Attempting to connect to SSID: ");
81
82 Serial.println(ssid);
83
84 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
85
86 status = WiFi.begin(ssid, pass);
87
88 // wait 10 seconds for connection:
89
90 delay(10000);
91
92 }
93
94 // you're connected now, so print out the status:
95
96 printWiFiStatus();
97}
98
99void loop() {
100
101 // if there's incoming data from the net connection.
102
103 // send it out the serial port. This is for debugging
104
105 // purposes only:
106
107 while (client.available()) {
108
109 char c = client.read();
110
111 Serial.write(c);
112
113 }
114
115 // if ten seconds have passed since your last connection,
116
117 // then connect again and send data:
118
119 if (millis() - lastConnectionTime > postingInterval) {
120
121 httpRequest();
122
123 }
124
125}
126
127// this method makes a HTTP connection to the server:
128void httpRequest() {
129
130 // close any connection before send a new request.
131
132 // This will free the socket on the WiFi shield
133
134 client.stop();
135
136 // if there's a successful connection:
137
138 if (client.connect(server, 80)) {
139
140 Serial.println("connecting...");
141
142 // send the HTTP PUT request:
143
144 client.println("GET / HTTP/1.1");
145
146 client.println("Host: example.org");
147
148 client.println("User-Agent: ArduinoWiFi/1.1");
149
150 client.println("Connection: close");
151
152 client.println();
153
154 // note the time that the connection was made:
155
156 lastConnectionTime = millis();
157
158 }
159
160 else {
161
162 // if you couldn't make a connection:
163
164 Serial.println("connection failed");
165
166 }
167}
168
169void printWiFiStatus() {
170
171 // print the SSID of the network you're attached to:
172
173 Serial.print("SSID: ");
174
175 Serial.println(WiFi.SSID());
176
177 // print your WiFi shield's IP address:
178
179 IPAddress ip = WiFi.localIP();
180
181 Serial.print("IP Address: ");
182
183 Serial.println(ip);
184
185 // print the received signal strength:
186
187 long rssi = WiFi.RSSI();
188
189 Serial.print("signal strength (RSSI):");
190
191 Serial.print(rssi);
192
193 Serial.println(" dBm");
194}

Wifi101 Simple Web Server WiFi

In this example, you will use your WiFi Shield 101 and your Arduino Zero, or a MKR1000 board, to create a simple Web server. Using the WiFi 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 shield's or MKR1000'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.

1/*
2
3 WiFi Web Server
4
5 A simple web server that shows the value of the analog input pins.
6
7 using a WiFi shield.
8
9 This example is written for a network using WPA encryption. For
10
11 WEP or WPA, change the WiFi.begin() call accordingly.
12
13 Circuit:
14
15 * WiFi shield attached
16
17 * Analog inputs attached to pins A0 through A5 (optional)
18
19 created 13 July 2010
20
21 by dlf (Metodo2 srl)
22
23 modified 31 May 2012
24
25 by Tom Igoe
26
27 */
28
29#include <SPI.h>
30#include <WiFi101.h>
31
32#include "arduino_secrets.h"
33///////please enter your sensitive data in the Secret tab/arduino_secrets.h
34char ssid[] = SECRET_SSID; // your network SSID (name)
35char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
36int keyIndex = 0; // your network key Index number (needed only for WEP)
37
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 only
51
52