1/*
2  Usage
3  This example demonstrates how to use the SAMD SNU library to update a
4  sketch on an Arduino MKRWiFi1010 board using the board and nothing else.
5  It prints out the date and time the sketch was compiled at
6  to both Serial and Serial1.
7
8   Arduino MKRWiFi1010 board
9
10  Steps to update sketch via NINA WiFi/BT module:
11
12  1) Upload this sketch or another sketch that includes the SNU library
13    via #include <SNU.h>
14
15  2) Update the sketch as desired. For this example the sketch prints out
16    the compiled date and time so no updates are needed.
17
18  3) In the IDE select: Sketch -> Export compiled Binary
19
20  4) Use WiFiStorage.download(url, "UPDATE.BIN") function to download the
21    new binary from a remote webserver.
22
23  5) Reboot the board; the update will be applied seamlessly
24
25  created 14 December 2018
26  by Martino Facchin
27*/
28
29/*
30  Include the SNU library
31
32  This will add some code to the sketch before setup() is called
33  to check if the WiFi module is present and UPDATE.bin exists.
34
35  If UPDATE.bin is present, the file is used to update the sketch
36  running on the board. After this UPDATE.bin is deleted from NINA memory.
37*/
38#include <SNU.h>
39#include <WiFiNINA.h>
40
41#include "arduino_secrets.h"
42///////please enter your sensitive data in the Secret tab/arduino_secrets.h
43/////// Wifi Settings ///////
44char ssid[] = SECRET_SSID;      // your network SSID (name)
45char pass[] = SECRET_PASS;   // your network password
46char url[] = SECRET_OTA_URL;
47
48int status = WL_IDLE_STATUS;
49
50String message;
51
52void setup() {
53  Serial.begin(9600);
54  Serial1.begin(9600);
55
56  // check for the presence of the shield:
57  if (WiFi.status() == WL_NO_SHIELD) {
58    Serial.println("WiFi shield not present");
59    // don't continue:
60    while (true);
61  }
62
63  // attempt to connect to Wifi network:
64  while ( status != WL_CONNECTED) {
65    Serial.print("Attempting to connect to SSID: ");
66    Serial.println(ssid);
67    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
68    status = WiFi.begin(ssid, pass);
69  }
70
71  // wait a bit
72  delay(1000);
73
74  message += "Sketch compile date and time: ";
75  message += __DATE__;
76  message += " ";
77  message += __TIME__;
78
79  // print out the sketch compile date and time on the serial port
80  Serial.println(message);
81  Serial1.println(message);
82
83  Serial.println("Type \"download\" in the Serial Monitor to start downloading the update");
84}
85
86void loop() {
87  String command = Serial.readStringUntil('\n');
88
89  if (command.indexOf("download") >= 0) {
90
91    Serial.println("Downloading update file");
92    WiFiStorage.download(url, "UPDATE.BIN");
93
94    WiFiStorageFile update = WiFiStorage.open("/fs/UPDATE.BIN");
95    if (update.available()) {
96      Serial.println("Download complete, please restart or type \"restart\" to apply the update");
97      Serial.println("Filesize: " + String(update.available()));
98    } else {
99      Serial.println("Download failed, please retry :(");
100    }
101  }
102
103  if (command.indexOf("restart") >= 0) {
104      NVIC_SystemReset();
105  }
106}
107