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
- MKR 1000 WiFi board
This example also works with a WiFi Shield 101 (retired) + Arduino Zero.
Walkthrough
Download the WiFi101OTA library via Library Manager
Select the WiFi101OTA sketch from the "Examples" menu and tune it to match your network name and password.
Upload the example using the "classic" serial port method
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"
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!Hooray! We just uploaded a new sketch over the air!
Open the serial port to check the new sketch is up and running
Circuit
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, sketches10
11 can be updated by selecting a network port from within12
13 the Arduino IDE: Tools -> Port -> Network Ports ...14
15 Circuit:16
17 * WiFi shield attached18
19 * SD shield attached20
21 created 13 July 201022
23 by dlf (Metodo2 srl)24
25 modified 31 May 201226
27 by Tom Igoe28
29 modified 16 January 201730
31 by Sandeep Mistry32
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 password43
44int status = WL_IDLE_STATUS;45
46void setup() {47
48 //Initialize serial:49
50 Serial.begin(9600);51
52 // setup SD card53
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 storage95
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 updates106
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.