AWS IoT for ESP32 v1.0.0
An ESP-IDF based solution
Over-The-Air-updates

Device OTA using AWS IoT

The best part of the library is OTA, you literally need not configure anything on device for OTA update to work. Just implement your application task. The library will take care of the OTA update process. All you need to do is create an OTA update Job on AWS IoT with target as your device that's it.

OTA Update Process

  1. Build your application and upload the binary file on S3 bucket.

    You may host your file anywhere but it should be publicly accessible using HTTP(S). After hosting the file, to ensure that your file is publicy accessible you can try downloading the file on your local computer using web browser.

  2. Create a job document for OTA update.

    The format of the job document required by the library is shown below. Replace the url value with HTTP(S) link to your file that you uploaded in the previous step.

    {
    "action" : "OTA",
    "url" : "https://your_bucket.s3.your_region.amazonaws.com/esp32_app.bin"
    }
  3. Create the job for OTA update.

After the job is created the target device will receive notification about this job and put the device in OTA mode. The library will download the binary file, update the firmware and reboot itself.

Implementation

For demonstration we will just blink an LED in the application task.

To test and verify firmware update you need two different versions of your application. We will create two applications that will blink an LED at different rates with different version number. Sample code of both the versions is shown below.

Application v1.0.0:

// blink at 1000ms
void app_task(void *param)
{
// ...
while(1)
{
GPIO_pinWrite(LED_PIN, 0);
TASK_DELAY_MS(1000);
GPIO_pinWrite(LED_PIN, 1);
TASK_DELAY_MS(1000);
}
// ...
}

Application v2.0.0:

// blink at 250ms
void app_task(void *param)
{
// ...
while (1)
{
GPIO_pinWrite(LED_PIN, 0);
TASK_DELAY_MS(250);
GPIO_pinWrite(LED_PIN, 1);
TASK_DELAY_MS(250);
}
// ...
}

How to test & verify

  1. Build application v1.0.0 and flash it your device.
  2. Build application v2.0.0 and upload its binary file to S3 bucket.
  3. Create/Update the Job document with the url link to application v2.0.0 version.
  4. Create an OTA Job with this Job document and your device as target.

The device should recevie the job notification and start the update process. After updating the firmware the device reboots, you should then see the LED blinking at faster rate.

You may create a duplicate of first project (v1.0.0) then start modifying it for the second version (v2.0.0). Another way is to first build & flash your device with first version then modify the blink rate & change its version number and build it. Then upload this binary to your S3 bucket.

1. Initialize the system configuration

Please refer to step #1 of Publish-Subscribe user guide to use thing certificates.

If you want to use other two methods then please read the Provisioning methods of Lib AWS under Overview section and configure the system configuration variable of type systemInitConfig_st.

2. Connect to AWS

Using the above configuration initialize the system and connect to AWS.

bool initSuccess = SYSTEM_init(&s_sysConfig);
if (initSuccess)
{
SYSTEM_start();
}
bool SYSTEM_init(systemInitConfig_st *s_pConfig)
Initiliaze the system with given configuration.

3. Create application task

Just add the code to blink the led and change the version number in app_config.h file.

Sample application task code:

void app_task(void *param)
{
while (1)
{
switch (SYSTEM_getMode())
{
{
// blink led
}
break;
default:
break;
}
TASK_DELAY_MS(100);
}
}
TaskHandle_t tHandle_appTask = NULL;
xTaskCreate(&app_task, "app_task", 1024, NULL, 4, &tHandle_appTask);
bool AWS_isConnected()
Check if the device is connected state.
systemMode_et SYSTEM_getMode()
Get the system mode.
@ SYSTEM_MODE_NORMAL
Definition: lib_system.h:34

4. Build and flash first version of application

# build the project
idf.py build
# build and flash it
idf.py flash monitor -p <your serial port>

5. Create OTA Job for second version

Create an OTA Job with a job document that contains the url to the second version of application binary. Ensure you select your target device.