This tutorial refers to a product that has reached its end-of-life status.

WiFi101 OTA with MKR 1000 WiFi

This example shows how to use the WiFi101OTA library to update your sketch over the air.

This example shows how to use the WiFi101OTA library to update your sketch over the air. No extra hardware is required since the update is applied directly in the upper half of the internal flash. This means that the biggest possible size of the compiled sketch is 120 KB.

Hardware Required

This example also works with a WiFi Shield 101 (retired) + Arduino Zero.

Walkthrough

Download the WiFi101OTA library via Library Manager

Step 1.
Step 1.

Step 2
Step 2

Select the WiFi101OTA sketch from the "Examples" menu and tune it to match your network name and password.

Step 3.
Step 3.

Upload the example using the "classic" serial port method

Step 4.
Step 4.

Your MKR1000 will connect to the Wi-Fi and expose itself as a Network port with the name and password you declared in the sketch with

WiFiOTA.begin
(the defaults name is "Arduino" while the password is "password").

At this point you are already able to update the sketch over the air! Open another sketch and make sure to add

WiFiOTA.begin()
in
setup()
and
WiFiOTA.poll()
in your
loop()
. If you forget about this you will lose the ability to upload over the air again (but you can still upload via serial, of course)

We are now ready to upload our new sketch wirelessly! Select the correct Network port in the Ports menu and press "Upload"

Step 5.
Step 5.

You will be prompted to enter the password you configured in the

begin
. Remember to choose a strong password to avoid unauthorised access to your board!

Step 6.
Step 6.

Hooray! We just uploaded a new sketch over the air!

Step 7.
Step 7.

Open the serial port to check the new sketch is up and running

Step 8.
Step 8.

Circuit

The circuit for this example.
The circuit for this example.

Code

First time you upload this code, you have to do it by means of the USB cable. After the first upload you will see your board listed under the Tools->port menu in the network's port sub menu.

1/*
2
3 This example connects to an WPA 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 It then polls for sketch updates over WiFi, sketches
10
11 can be updated by selecting a network port from within
12
13 the Arduino IDE: Tools -> Port -> Network Ports ...
14
15 Circuit:
16
17 * WiFi shield attached
18
19 * SD 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 modified 16 January 2017
30
31 by Sandeep Mistry
32
33 */
34
35#include <SPI.h>
36#include <SD.h>
37#include <WiFi101.h>
38#include <WiFi101OTA.h>
39#include <SDU.h>
40
41char ssid[] = "yourNetwork"; // your network SSID (name)
42char pass[] = "secretPassword"; // your network password
43
44int status = WL_IDLE_STATUS;
45
46void setup() {
47
48 //Initialize serial:
49
50 Serial.begin(9600);
51
52 // setup SD card
53
54 Serial.print("Initializing SD card...");
55
56 if (!SD.begin(SDCARD_SS_PIN)) {
57
58 Serial.println("initialization failed!");
59
60 // don't continue:
61
62 while (true);
63
64 }
65
66 Serial.println("initialization done.");
67
68 // check for the presence of the shield:
69
70 if (WiFi.status() == WL_NO_SHIELD) {
71
72 Serial.println("WiFi shield not present");
73
74 // don't continue:
75
76 while (true);
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 }
93
94 // start the WiFi OTA library with SD based storage
95
96 WiFiOTA.begin("Arduino", "password", SDStorage);
97
98 // you're connected now, so print out the status:
99
100 printWifiStatus();
101}
102
103void loop() {
104
105 // check for WiFi OTA updates
106
107 WiFiOTA.poll();
108
109 // add your normal loop code below ...
110}
111
112void printWifiStatus() {
113
114 // print the SSID of the network you're attached to:
115
116 Serial.print("SSID: ");
117
118 Serial.println(WiFi.SSID());
119
120 // print your WiFi shield's IP address:
121
122 IPAddress ip = WiFi.localIP();
123
124 Serial.print("IP Address: ");
125
126 Serial.println(ip);
127
128 // print the received signal strength:
129
130 long rssi = WiFi.RSSI();
131
132 Serial.print("signal strength (RSSI):");
133
134 Serial.print(rssi);
135
136 Serial.println(" dBm");
137}

Last revision 2017/03/24 by AG

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.