This project aims to demonstrate how we can use ESP32 with Azure IoT Hub and implement OTA features through the platform.
To work with this library, you will have to follow some prerequisites sets that are described at the wiki: https://github.com/kbeaugrand/ESP32_AzureIoT_OTA/wiki.
In this section, you install this repository as an Arduino Library on your computer.
-
Download the library from the releases on github: https://github.com/kbeaugrand/ESP32_AzureIoT_OTA/releases
-
Extract the release to Arduino Library (see: https://www.arduino.cc/en/Guide/Libraries#manual-installation)
In this section you create a new sketch called "Device Initiator" that helps you to configure the device settings. This sketch will create a new partition scheme on the device storage, and create the device settings file on that storage.
Note
Storing those settings enables the possibility to work with Firmware update Over The Air without storing the device identity with the Firmware.
-
Start a new sketch from an example
- Open VS Code and press
F1
orCtrl + Shift + P
to open command palette - Select
Ardiuno: Examples
command - On Arduino Examples Window, select
Examples from Custom Libraries
>ESP32_AzureIoT_OTA
>ESP32_Device_Initiator
- Open VS Code and press
-
Configure your ESP 32 board from VSCode
- Open VS Code and press
F1
orCtrl + Shift + P
to open command palette - Select
Ardiuno: Board Config
command - On Arduino Board Configuration Window select the
Minimal SPIFFS
Partition Scheme
- Open VS Code and press
-
On
ESP32_Device_Initiator
, type your device connection settingsconst char *connectionString = "Type your Device Connection String"; const char *ssid = "Type here your WiFi SSID"; const char *password = "Type here your WiFi shared Key";
-
Upload this sketch to Arduino ESP32
Alt + Cmd + U
orAlt + Ctrl + U
Note
Once this sketch is uploaded on the ESP 32, the integrated LED will be high.
In this section, you create the device firmware code.
-
Start a new sketch from an example
- Open VS Code and press
F1
orCtrl + Shift + P
to open command palette - Select
Ardiuno: Examples
command - On Arduino Examples Window, select
Examples from Custom Libraries
>ESP32_AzureIoT_OTA
>ESP32_Device_OTA
- Open VS Code and press
-
Configure your ESP 32 board from VSCode
- Open VS Code and press
F1
orCtrl + Shift + P
to open command palette - Select
Ardiuno: Board Config
command - On Arduino Board Configuration Window select the
Minimal SPIFFS
Partition Scheme
[!NOTE] This sketch will use the device settings file previously created to get its credentials and connect to WiFi and IoT Hub. You will have to update the device logic on this one.
- Open VS Code and press
-
Configure the Arduino build output for this sketch
-
Open the
.vscode/arduino.json
-
Update the settings file with this line:
{ "output": "./.build" }
-
-
Upload this sketch to Arduino ESP32
Alt + Cmd + U
orAlt + Ctrl + U
In this section, you create a cloud storage account and upload you device firmware to it.
-
Open the sketch repository with VS Code
-
Update the
FIRMWARE_VERSION
#define FIRMWARE_VERSION "0.0.2"
-
Build the firmware
Alt + Cmd + R
orAlt + Ctrl + R
- Navigate to your new storage account container in the Azure portal.
- Select the Upload button.
- Click on the Select file button.
- On the
Open
Window, select the firmware binary from the sketch build directory./.build/
.The default name isESP32_Device_OTA.ino.bin
.
In this section, you update your device twin to set a firmware update from the cloud portal.
-
In your IoT hub navigation menu, open IoT Devices, then select the IoT device you created.
-
Select the Device Twin button.
-
Use the twin editor to update your device twin with this following statements
{ "properties": { "desired": { "firmware": { "version": "0.0.2", "source": "https://<storage_account_name>.blob.core.windows.net/<container_name>/ESP32_Device_OTA.ino.bin" } } }
-
Select the Save button.
-
Select the Refresh button to review the reported properties and verify once the device successfully applied the firmware update
{ "properties": { "reported": { "firmware": { "version": "0.0.2" } } }
Note
On device Serial Monitor, you may see the device update done
Open VS Code and press F1
or Ctrl + Shift + P
to open command palette
Select Arduino: Open Serial Monitor
command
> Debug: Begin request for: https://<storage_account_name>.blob.core.windows.net/<container_name>/ESP32_Device_OTA.ino.bin
> Debug: Trying to start OTA with 1040864 bytes
> Info: Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!
> Info: Written: 1040864 successfully
> Info: OTA done!
> Info: Update successfully completed. Rebooting.