|
| 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 | +} |
0 commit comments