|
| 1 | +/* |
| 2 | + OTANonBlocking |
| 3 | +
|
| 4 | + This sketch demonstrates how to make an OTA Update on the UNO R4 WiFi. |
| 5 | + Upload the sketch and wait for the invasion! |
| 6 | +
|
| 7 | + It requires at least version 0.5.0 of USB Wifi bridge firmware |
| 8 | +
|
| 9 | +*/ |
| 10 | + |
| 11 | + |
| 12 | +#include "WiFiS3.h" |
| 13 | +#include "OTAUpdate.h" |
| 14 | +#include "root_ca.h" |
| 15 | +#include "arduino_secrets.h" |
| 16 | + |
| 17 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 18 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 19 | + |
| 20 | +int status = WL_IDLE_STATUS; |
| 21 | + |
| 22 | +OTAUpdate ota; |
| 23 | +static char const OTA_FILE_LOCATION[] = "https://downloads.arduino.cc/ota/UNOR4WIFI_Animation.ota"; |
| 24 | + |
| 25 | +/* -------------------------------------------------------------------------- */ |
| 26 | +void setup() { |
| 27 | +/* -------------------------------------------------------------------------- */ |
| 28 | + //Initialize serial and wait for port to open: |
| 29 | + Serial.begin(115200); |
| 30 | + while (!Serial) { |
| 31 | + ; // wait for serial port to connect. Needed for native USB port only |
| 32 | + } |
| 33 | + |
| 34 | + // check for the Wi-Fi module: |
| 35 | + if (WiFi.status() == WL_NO_MODULE) { |
| 36 | + Serial.println("Communication with Wi-Fi module failed!"); |
| 37 | + // don't continue |
| 38 | + while (true); |
| 39 | + } |
| 40 | + |
| 41 | + String fv = WiFi.firmwareVersion(); |
| 42 | + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { |
| 43 | + Serial.println("Please upgrade the firmware"); |
| 44 | + } |
| 45 | + |
| 46 | + // attempt to connect to Wi-Fi network: |
| 47 | + while (status != WL_CONNECTED) { |
| 48 | + Serial.print("Attempting to connect to SSID: "); |
| 49 | + Serial.println(ssid); |
| 50 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 51 | + status = WiFi.begin(ssid, pass); |
| 52 | + |
| 53 | + // wait 1 seconds for connection: |
| 54 | + delay(1000); |
| 55 | + } |
| 56 | + |
| 57 | + printWiFiStatus(); |
| 58 | + |
| 59 | + Serial.println("ota.begin()"); |
| 60 | + int ret = ota.begin("/update.bin"); |
| 61 | + if(ret != OTAUpdate::OTA_ERROR_NONE) { |
| 62 | + Serial.println("ota.begin() error: "); |
| 63 | + Serial.println((int)ret); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + Serial.println("ota.setCACert()"); |
| 68 | + ret = ota.setCACert(root_ca); |
| 69 | + if(ret != OTAUpdate::OTA_ERROR_NONE) { |
| 70 | + Serial.println("ota.setCACert() error: "); |
| 71 | + Serial.println((int)ret); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + Serial.println("ota.startDownload()"); |
| 76 | + int ota_size = ota.startDownload(OTA_FILE_LOCATION, "/update.bin"); |
| 77 | + if(ota_size <= 0) { |
| 78 | + Serial.println("ota.startDownload() error: "); |
| 79 | + Serial.println(ota_size); |
| 80 | + Serial.println("Make sure your WiFi firmware version is at least 0.5.0"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + Serial.println("ota.downloadProgress()"); |
| 85 | + while((ret = ota.downloadProgress()) < ota_size) { |
| 86 | + Serial.print("Progress "); |
| 87 | + Serial.print(ret); |
| 88 | + Serial.print("/"); |
| 89 | + Serial.println(ota_size); |
| 90 | + delay(100); |
| 91 | + } |
| 92 | + |
| 93 | + if(ret < 0){ |
| 94 | + Serial.println("ota.downloadProgress() error: "); |
| 95 | + Serial.println((int)ret); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + Serial.println("ota.verify()"); |
| 100 | + ret = ota.verify(); |
| 101 | + if(ret != OTAUpdate::OTA_ERROR_NONE) { |
| 102 | + Serial.println("ota.verify() error: "); |
| 103 | + Serial.println((int)ret); |
| 104 | + return; |
| 105 | + } |
| 106 | +\ |
| 107 | + Serial.println("ota.update()"); |
| 108 | + ret = ota.update("/update.bin"); |
| 109 | + if(ret != OTAUpdate::OTA_ERROR_NONE) { |
| 110 | + Serial.println("ota.update() error: "); |
| 111 | + Serial.println((int)ret); |
| 112 | + return; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/* -------------------------------------------------------------------------- */ |
| 117 | +void loop() { |
| 118 | +/* -------------------------------------------------------------------------- */ |
| 119 | + delay(1000); |
| 120 | +} |
| 121 | + |
| 122 | +/* -------------------------------------------------------------------------- */ |
| 123 | +void printWiFiStatus() { |
| 124 | +/* -------------------------------------------------------------------------- */ |
| 125 | + // print the SSID of the network you're attached to: |
| 126 | + Serial.print("SSID: "); |
| 127 | + Serial.println(WiFi.SSID()); |
| 128 | + |
| 129 | + // print your board's IP address: |
| 130 | + IPAddress ip = WiFi.localIP(); |
| 131 | + Serial.print("IP Address: "); |
| 132 | + Serial.println(ip); |
| 133 | + |
| 134 | + // print the received signal strength: |
| 135 | + long rssi = WiFi.RSSI(); |
| 136 | + Serial.print("signal strength (RSSI):"); |
| 137 | + Serial.print(rssi); |
| 138 | + Serial.println(" dBm"); |
| 139 | +} |
0 commit comments