|
| 1 | +/* |
| 2 | + * This example demonstrates how to use to update the firmware using Arduino_ESP_OTA library |
| 3 | + * |
| 4 | + * Steps: |
| 5 | + * 1) Create a sketch for your ESP board and verify |
| 6 | + * that it both compiles and works. |
| 7 | + * 2) In the IDE select: Sketch -> Export compiled Binary. |
| 8 | + * 3) Create an OTA update file utilising the tools 'lzss.py' and 'bin2ota.py' stored in |
| 9 | + * https://github.com/arduino-libraries/ArduinoIoTCloud/tree/master/extras/tools . |
| 10 | + * A) ./lzss.py --encode SKETCH.bin SKETCH.lzss |
| 11 | + * B) ./bin2ota.py ESP SKETCH.lzss SKETCH.ota |
| 12 | + * 4) Upload the OTA file to a network reachable location, e.g. LOLIN_32_Blink.ino.ota |
| 13 | + * has been uploaded to: http://downloads.arduino.cc/ota/LOLIN_32_Blink.ino.ota |
| 14 | + * 5) Verify if a custom ca_cert is needed by default Amazon root CA are used |
| 15 | + * https://www.amazontrust.com/repository/ |
| 16 | + * 6) Perform an OTA update via steps outlined below. |
| 17 | + */ |
| 18 | + |
| 19 | +/****************************************************************************** |
| 20 | + * INCLUDE |
| 21 | + ******************************************************************************/ |
| 22 | + |
| 23 | +#include <Arduino_ESP32_OTA.h> |
| 24 | + |
| 25 | +#include <WiFi.h> |
| 26 | + |
| 27 | +#include "arduino_secrets.h" |
| 28 | + |
| 29 | +#include "root_ca.h" |
| 30 | + |
| 31 | +/****************************************************************************** |
| 32 | + * CONSTANT |
| 33 | + ******************************************************************************/ |
| 34 | + |
| 35 | +/* Please enter your sensitive data in the Secret tab/arduino_secrets.h */ |
| 36 | +static char const SSID[] = SECRET_SSID; /* your network SSID (name) */ |
| 37 | +static char const PASS[] = SECRET_PASS; /* your network password (use for WPA, or use as key for WEP) */ |
| 38 | + |
| 39 | + |
| 40 | +#if defined(ARDUINO_NANO_ESP32) |
| 41 | +static char const OTA_FILE_LOCATION[] = "https://raw.githubusercontent.com/arduino-libraries/Arduino_ESP32_OTA/main/examples/NANO_ESP32_Blink/NANO_ESP32_Blink.ino.ota"; |
| 42 | +#else |
| 43 | +static char const OTA_FILE_LOCATION[] = "https://raw.githubusercontent.com/arduino-libraries/Arduino_ESP32_OTA/main/examples/LOLIN_32_Blink/LOLIN_32_Blink.ino.ota" |
| 44 | +#endif |
| 45 | + |
| 46 | +/****************************************************************************** |
| 47 | + * SETUP/LOOP |
| 48 | + ******************************************************************************/ |
| 49 | + |
| 50 | +void setup() |
| 51 | +{ |
| 52 | + Serial.begin(9600); |
| 53 | + while (!Serial) {} |
| 54 | + |
| 55 | + while (WiFi.status() != WL_CONNECTED) |
| 56 | + { |
| 57 | + Serial.print ("Attempting to connect to '"); |
| 58 | + Serial.print (SSID); |
| 59 | + Serial.println("'"); |
| 60 | + WiFi.begin(SSID, PASS); |
| 61 | + delay(2000); |
| 62 | + } |
| 63 | + Serial.print ("You're connected to '"); |
| 64 | + Serial.print (WiFi.SSID()); |
| 65 | + Serial.println("'"); |
| 66 | + |
| 67 | + Arduino_ESP32_OTA ota; |
| 68 | + Arduino_ESP32_OTA::Error ota_err = Arduino_ESP32_OTA::Error::None; |
| 69 | + |
| 70 | + /* Configure custom Root CA */ |
| 71 | + ota.setCACert(root_ca); |
| 72 | + |
| 73 | + Serial.println("Initializing OTA storage"); |
| 74 | + if ((ota_err = ota.begin()) != Arduino_ESP32_OTA::Error::None) |
| 75 | + { |
| 76 | + Serial.print ("Arduino_ESP_OTA::begin() failed with error code "); |
| 77 | + Serial.println((int)ota_err); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + Serial.println("Starting download to flash ..."); |
| 83 | + int const ota_download = ota.download(OTA_FILE_LOCATION); |
| 84 | + if (ota_download <= 0) |
| 85 | + { |
| 86 | + Serial.print ("Arduino_ESP_OTA::download failed with error code "); |
| 87 | + Serial.println(ota_download); |
| 88 | + return; |
| 89 | + } |
| 90 | + Serial.print (ota_download); |
| 91 | + Serial.println(" bytes stored."); |
| 92 | + |
| 93 | + |
| 94 | + Serial.println("Verify update integrity and apply ..."); |
| 95 | + if ((ota_err = ota.update()) != Arduino_ESP32_OTA::Error::None) |
| 96 | + { |
| 97 | + Serial.print ("ota.update() failed with error code "); |
| 98 | + Serial.println((int)ota_err); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + Serial.println("Performing a reset after which the bootloader will start the new firmware."); |
| 103 | +#if defined(ARDUINO_NANO_ESP32) |
| 104 | + Serial.println("Hint: Arduino NANO ESP32 will blink Red Green and Blue."); |
| 105 | +#else |
| 106 | + Serial.println("Hint: LOLIN32 will blink Blue."); |
| 107 | +#endif |
| 108 | + delay(1000); /* Make sure the serial message gets out before the reset. */ |
| 109 | + ota.reset(); |
| 110 | +} |
| 111 | + |
| 112 | +void loop() |
| 113 | +{ |
| 114 | + |
| 115 | +} |
0 commit comments