Skip to content

Commit 263fe13

Browse files
facchinmpennam
authored andcommitted
Add example for Deferred OTA
1 parent 0b7a91d commit 263fe13

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
This sketch demonstrates how to handle deferred OTA from Arduino IoT Cloud.
3+
4+
Deferred OTA can be triggered using the arduino-cloud-cli with the following command:
5+
./arduino-cloud-cli ota upload --device-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --file filename.ino.bin --deferred
6+
The update file and the download link will be available to be used within one week.
7+
8+
* always_deny callback will always postpone the OTA update
9+
* always_allow callback will immediately apply the OTA update
10+
* ask_user_via_serial callback will read user input from serial to apply or postpone OTA update
11+
12+
This sketch is compatible with:
13+
- MKR WIFI 1010
14+
- Nano 33 IoT
15+
- Portenta
16+
- Nano RP2040
17+
*/
18+
19+
#include "arduino_secrets.h"
20+
#include "thingProperties.h"
21+
22+
#if defined(ESP32)
23+
static int const LED_BUILTIN = 2;
24+
#endif
25+
26+
bool always_deny() {
27+
return false;
28+
}
29+
30+
bool always_allow() {
31+
return true;
32+
}
33+
34+
static bool ask_user_via_serial_first_run = true;
35+
bool ask_user_via_serial() {
36+
if (ask_user_via_serial_first_run) {
37+
Serial.println("Apply OTA? y / [n]");
38+
ask_user_via_serial_first_run = false;
39+
}
40+
if (Serial.available()) {
41+
char c = Serial.read();
42+
if (c == 'y' || c == 'Y') {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
void setup() {
50+
/* Initialize serial and wait up to 5 seconds for port to open */
51+
Serial.begin(9600);
52+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { }
53+
54+
/* Configure LED pin as an output */
55+
pinMode(LED_BUILTIN, OUTPUT);
56+
57+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
58+
initProperties();
59+
60+
/* Initialize Arduino IoT Cloud library */
61+
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
62+
63+
/* Setup OTA callback */
64+
ArduinoCloud.onOTARequestCb(always_deny);
65+
66+
setDebugMessageLevel(DBG_INFO);
67+
ArduinoCloud.printDebugInfo();
68+
}
69+
70+
void loop() {
71+
ArduinoCloud.update();
72+
}
73+
74+
/*
75+
* 'onLedChange' is called when the "led" property of your Thing changes
76+
*/
77+
void onLedChange() {
78+
Serial.print("LED set to ");
79+
Serial.println(led);
80+
digitalWrite(LED_BUILTIN, led);
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* ESP8266 */
10+
#if defined(BOARD_ESP8266)
11+
#define SECRET_DEVICE_KEY "my-device-password"
12+
#endif
13+
14+
/* MKR GSM 1400 */
15+
#if defined(BOARD_HAS_GSM)
16+
#define SECRET_PIN ""
17+
#define SECRET_APN ""
18+
#define SECRET_LOGIN ""
19+
#define SECRET_PASS ""
20+
#endif
21+
22+
/* MKR WAN 1300/1310 */
23+
#if defined(BOARD_HAS_LORA)
24+
#define SECRET_APP_EUI ""
25+
#define SECRET_APP_KEY ""
26+
#endif
27+
28+
/* MKR NB 1500 */
29+
#if defined(BOARD_HAS_NB)
30+
#define SECRET_PIN ""
31+
#define SECRET_APN ""
32+
#define SECRET_LOGIN ""
33+
#define SECRET_PASS ""
34+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
#if defined(BOARD_HAS_WIFI)
5+
#elif defined(BOARD_HAS_GSM)
6+
#elif defined(BOARD_HAS_LORA)
7+
#elif defined(BOARD_HAS_NB)
8+
#else
9+
#error "Arduino IoT Cloud currently only supports MKR1000, MKR WiFi 1010, MKR WAN 1300/1310, MKR NB 1500 and MKR GSM 1400"
10+
#endif
11+
12+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
13+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14+
15+
void onLedChange();
16+
17+
bool led;
18+
19+
void initProperties() {
20+
#if defined(BOARD_ESP8266)
21+
ArduinoCloud.setBoardId(BOARD_ID);
22+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
23+
#endif
24+
ArduinoCloud.setThingId(THING_ID);
25+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
26+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
27+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
28+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
29+
#elif defined(BOARD_HAS_LORA)
30+
ArduinoCloud.addProperty(led, 1, READWRITE, ON_CHANGE, onLedChange);
31+
ArduinoCloud.addProperty(potentiometer, 2, READ, ON_CHANGE);
32+
ArduinoCloud.addProperty(seconds, 3, READ, 5 * MINUTES);
33+
#endif
34+
}
35+
36+
#if defined(BOARD_HAS_WIFI)
37+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
38+
#elif defined(BOARD_HAS_GSM)
39+
GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
40+
#elif defined(BOARD_HAS_LORA)
41+
LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, _lora_class::CLASS_A);
42+
#elif defined(BOARD_HAS_NB)
43+
NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
44+
#endif

0 commit comments

Comments
 (0)