Skip to content

Commit

Permalink
Merge pull request #325 from andreagilardoni/non-blocking-ota-download
Browse files Browse the repository at this point in the history
Non blocking ota download
  • Loading branch information
pennam authored Nov 28, 2024
2 parents 428df2a + 3bd4ebd commit 1c4b997
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
139 changes: 139 additions & 0 deletions libraries/OTAUpdate/examples/OTANonBlocking/OTANonBlocking.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
OTANonBlocking
This sketch demonstrates how to make an OTA Update on the UNO R4 WiFi.
Upload the sketch and wait for the invasion!
It requires at least version 0.5.0 of USB Wifi bridge firmware
*/


#include "WiFiS3.h"
#include "OTAUpdate.h"
#include "root_ca.h"
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

OTAUpdate ota;
static char const OTA_FILE_LOCATION[] = "https://downloads.arduino.cc/ota/UNOR4WIFI_Animation.ota";

/* -------------------------------------------------------------------------- */
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the Wi-Fi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with Wi-Fi module failed!");
// don't continue
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wi-Fi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 1 seconds for connection:
delay(1000);
}

printWiFiStatus();

Serial.println("ota.begin()");
int ret = ota.begin("/update.bin");
if(ret != OTAUpdate::OTA_ERROR_NONE) {
Serial.println("ota.begin() error: ");
Serial.println((int)ret);
return;
}

Serial.println("ota.setCACert()");
ret = ota.setCACert(root_ca);
if(ret != OTAUpdate::OTA_ERROR_NONE) {
Serial.println("ota.setCACert() error: ");
Serial.println((int)ret);
return;
}

Serial.println("ota.startDownload()");
int ota_size = ota.startDownload(OTA_FILE_LOCATION, "/update.bin");
if(ota_size <= 0) {
Serial.println("ota.startDownload() error: ");
Serial.println(ota_size);
Serial.println("Make sure your WiFi firmware version is at least 0.5.0");
return;
}

Serial.println("ota.downloadProgress()");
while((ret = ota.downloadProgress()) < ota_size) {
Serial.print("Progress ");
Serial.print(ret);
Serial.print("/");
Serial.println(ota_size);
delay(100);
}

if(ret < 0){
Serial.println("ota.downloadProgress() error: ");
Serial.println((int)ret);
return;
}

Serial.println("ota.verify()");
ret = ota.verify();
if(ret != OTAUpdate::OTA_ERROR_NONE) {
Serial.println("ota.verify() error: ");
Serial.println((int)ret);
return;
}
\
Serial.println("ota.update()");
ret = ota.update("/update.bin");
if(ret != OTAUpdate::OTA_ERROR_NONE) {
Serial.println("ota.update() error: ");
Serial.println((int)ret);
return;
}
}

/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
delay(1000);
}

/* -------------------------------------------------------------------------- */
void printWiFiStatus() {
/* -------------------------------------------------------------------------- */
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
2 changes: 2 additions & 0 deletions libraries/OTAUpdate/examples/OTANonBlocking/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
106 changes: 106 additions & 0 deletions libraries/OTAUpdate/examples/OTANonBlocking/root_ca.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const char* root_ca = \
/* GTS Root R1 */
"-----BEGIN CERTI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A3jdgUM/I2O2dgq43mLyjj0xMqTQrbO/7lZsm\n" \
"-----END CERTIFICATE-----\n";
43 changes: 43 additions & 0 deletions libraries/OTAUpdate/src/OTAUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,49 @@ int OTAUpdate::download(const char* url, const char* file_path) {
return ret;
}

int OTAUpdate::startDownload(const char* url) {
string res = "";
int ret = -1;
if ( url != nullptr && strlen(url) > 0) {
modem.timeout(EXTENDED_MODEM_TIMEOUT);
if(modem.write(string(PROMPT(_OTA_DOWNLOAD_START)), res, "%s%s\r\n", CMD_WRITE(_OTA_DOWNLOAD_START), url)) {
ret = atoi(res.c_str());
} else {
ret = static_cast<int>(Error::Modem);
}
} else {
ret = static_cast<int>(Error::Library);
}
modem.timeout(MODEM_TIMEOUT);
return ret;
}

int OTAUpdate::startDownload(const char* url, const char* file_path) {
string res = "";
int ret = -1;

if ( url != nullptr && strlen(url) > 0 && file_path != nullptr && strlen(file_path) >0) {
modem.timeout(EXTENDED_MODEM_TIMEOUT);
if(modem.write(string(PROMPT(_OTA_DOWNLOAD_START)), res, "%s%s,%s\r\n", CMD_WRITE(_OTA_DOWNLOAD_START), url, file_path)) {
ret = atoi(res.c_str());
} else {
ret = static_cast<int>(Error::Modem);
}
} else {
ret = static_cast<int>(Error::Library);
}
modem.timeout(MODEM_TIMEOUT);
return ret;
}

int OTAUpdate::downloadProgress() {
string res = "";
if (modem.write(string(PROMPT(_OTA_DOWNLOAD_PROGRESS)), res, "%s", CMD(_OTA_DOWNLOAD_PROGRESS))) {
return atoi(res.c_str());
}
return static_cast<int>(Error::Modem);
}

int OTAUpdate::verify() {
string res = "";
if (modem.write(string(PROMPT(_OTA_VERIFY)), res, "%s", CMD(_OTA_VERIFY))) {
Expand Down
6 changes: 6 additions & 0 deletions libraries/OTAUpdate/src/OTAUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class OTAUpdate {
int begin(const char* file_path);
int download(const char* url);
int download(const char* url, const char* file_path);

int startDownload(const char* url);
int startDownload(const char* url, const char* file_path);

int downloadProgress();

int verify();
int update();
int update(const char* file_path);
Expand Down

0 comments on commit 1c4b997

Please sign in to comment.