iot lab work shop
This project is maintained by DanielYEHsieh
Over-the-air (OTA) update is a very common way for delivering updates to devices. Amazon FreeRTOS supports OTA updates to allow product makers for deploying firmware updates to one or more devices in their fleet.
There are some components that involve in Amamzon FreeRTOS OTA updates:
We will go through the following steps to finish Amazon FreeRTOS OTA demonstration in this lab:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iam:GetRole",
"iam:PassRole"
],
"Resource": "arn:aws:iam::<your_account_id>:role/<your_role_name>"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObjectVersion",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::<example-bucket>/*"
]
}
]
}
We should grant our IAM user permission to perform OTA updates.
{
"Version":"2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:ListAllMyBuckets",
"s3:CreateBucket",
"s3:PutBucketVersioning",
"s3:GetBucketLocation",
"s3:GetObjectVersion",
"acm:ImportCertificate",
"acm:ListCertificates",
"iot:*",
"iam:ListRoles",
"freertos:ListHardwarePlatforms",
"freertos:DescribeHardwarePlatform"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::<example-bucket>/*"
},
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::<your-account-id>:role/<role-name>"
}
]
}
Comment out #define CONFIG_*_DEMO_ENABLED, and define CONFIG_OTA_UPDATE_DEMO_ENABLED
#define CONFIG_OTA_UPDATE_DEMO_ENABLED
Check the configuration of OTA protocols, we use MQTT as the default protocol
#define configENABLED_CONTROL_PROTOCOL ( OTA_CONTROL_OVER_MQTT )
#define configENABLED_DATA_PROTOCOLS ( OTA_DATA_OVER_MQTT )
#define configOTA_PRIMARY_DATA_PROTOCOL ( OTA_DATA_OVER_MQTT )
(Optional) If you want to enable transfer data over HTTP
#define configENABLED_DATA_PROTOCOLS ( OTA_DATA_OVER_HTTP )
#define configOTA_PRIMARY_DATA_PROTOCOL ( OTA_DATA_OVER_HTTPT )
Because of a limited amount of RAM of ESP32, we need to turn off BLE when enabling HTTP as an OTA data protocol
Open <AmazonFreeRTOS>/vendors/espressif/boards/esp32/aws_demos/config_files/aws_iot_network_config.h, and change enabled network to WIFI only
#define configENABLED_NETWORKS (AWSIOT_NETWORK_TYPE_WIFI )
ESP32 support a self-signed SHA-256 with ECDSA code-signing certificate, we will use openssl to generate private key and certificate
In your working folder, create and edit cert_config.txt. Replace YOUR_EMAIL_ADDRESS with your email address
[ req ]
prompt = no
distinguished_name = my_dn
[ my_dn ]
commonName = YOUR_EMAIL_ADDRESS
[ my_exts ]
keyUsage = digitalSignature
extendedKeyUsage = codeSigning
Open your openssl command line tool (Or open git-bash if you installed Git for Windows)
Create ECDSA code-signing private key
cd YOUR_WORK_FOLDER
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -pkeyopt ec_param_enc:named_curve -outform PEM -out ecdsasigner.key
Create an ECDSA code-signing certificate
openssl req -new -x509 -config cert_config.txt -extensions my_exts -nodes -days 365 -key ecdsasigner.key -out ecdsasigner.crt
Go to AWS Certificate Manager, and click Import a certificate
Add self-signed certificate for OTA agent
Open <AmazonFreeRTOS>/demos/include/aws_ota_codesigner_certificate.h, and paste certificate to it. It should be formatted in the following way, and please replace YOUR_BASE64_CERTIFICATE_DATA with yours
static const char signingcredentialSIGNING_CERTIFICATE_PEM[] = "-----BEGIN CERTIFICATE-----\n"
"YOUR_BASE64_CERTIFICATE_DATA\n"
"-----END CERTIFICATE-----\n";
The OTA agent included with Amazon FreeRTOS checks the version of any update and installs it only if it is more recent than the existing firmware version
Edit <AmazonFreeRTOS>/demos/include/aws_application_version.h and increment the APP_VERSION_BUILD token value
#define APP_VERSION_BUILD 6
Rebuild the new firmware image, the new image is put in <AmazonFreeRTOS>/out/aws_demos.bin