Development practice of OTA firmware upgrade for IoT devices

Practice of OTA firmware upgrade for IoT devices
1 Introduction
OTA (Over-the-Air Technology) is an over-the-air download technology, which is an essential basic function of the IoT platform. Through the OTA method, we can upgrade the device firmware of IoT devices distributed all over the world without having to send operation and maintenance personnel around. This article takes the firmware upgrade under the MQTT protocol as an example to introduce the OTA firmware upgrade process, topics and data formats used for data transfer.


2. Firmware upgrade OTA process
The firmware upgrade process under the MQTT protocol is shown in the figure below

The topics used in the firmware upgrade process are listed below

1. The device side reports the firmware version to the IoT platform through the following Topic.
/ota/device/inform/${YourProductKey}/${YourDeviceName}
2. The device side subscribes to the following Topic to receive the firmware upgrade notification of the IoT platform.
/ota/device/upgrade/${YourProductKey}/${YourDeviceName}
3. The device side reports the firmware upgrade progress through the following Topic.
/ota/device/progress/${YourProductKey}/${YourDeviceName}


3. Actual firmware upgrade

3.1 Device version information
In order to realize the firmware upgrade function, the device must first report the current firmware version correctly, which we can check in the device details.


3.2 Firmware version distribution
When each device accurately reports the firmware version, we can check the version release status of all devices on the console.


3.3 Upload new firmware
When we need to upgrade the device firmware, we must first upload the new version of the firmware to the IoT platform and mark the new version number.


3.4 Verifying the firmware
After the new firmware is uploaded, we need to screen the test equipment to verify whether the firmware is normal, so as to prevent the new firmware from causing abnormal business of the equipment.

After the verification is passed, you will see that the batch upgrade function becomes available.

3.5 Batch upgrade
Click the batch upgrade menu to enter the upgrade configuration page. We can filter devices to be upgraded from multiple dimensions and configure upgrade strategies.


3.6 Upgrade process
After starting the firmware upgrade task, we will see an upgrade batch. Click to enter the details, you can see the list of devices to be upgraded.

The Upgrading Tab will display the list of devices being upgraded and the progress of the upgrade.
The Upgrade Successful Tab will display a list of devices that have completed the firmware upgrade. Including current firmware version, update time, status.
The Upgrade Failed Tab will display a list of devices that have failed to upgrade. Including the current firmware version, update time, failure reason


appendix
An example of the Payload of the upgrade message pushed by the IoT platform to the device

{
"code": "1000",
"data": {
"size":11472299,
"sign":"83254ac96e141affb8aa42cbfec93723",
"version": "2-45-345b",

"signMethod": "Md5",
"md5":"83254ac96e141affb8aa42cbfec93723"
},
"id": 1568864790381,
"message": "success"
}
Device firmware upgrade simulation code

/**
* node aliyun-iot-device.js
*/
const fs = require('fs');
const path = require('path');
const mqtt = require('aliyun-iot-mqtt');
//device identity triple + region

const options = {
productKey: "replace pk",
deviceName: "Replace dn",
deviceSecret: "Replace ds",
regionId: "cn-shanghai"
}

//establish connection
const client = mqtt.getAliyunIotMqttClient(options);
//Subscribe to the Topic of the ota message
const deviceUpgrade = `/ota/device/upgrade/${options.productKey}/${options.deviceName}`
client. subscribe(deviceUpgrade)


// After each connection, report the current firmware version
const deviceInform = `/ota/device/inform/${options.productKey}/${options.deviceName}`
client.publish(deviceInform, getFirmwareVersion("1-45-345a"))

//During the OTA process, report the progress
const deviceProgress = `/ota/device/progress/${options.productKey}/${options.deviceName}`

// message processing
client.on('message', function(topic, message) {

if (topic == deviceUpgrade) {
//Receive the ota message and start the upgrade process
doUpgrade(message)
}

})

// local update
function doUpgrade(message) {
message = JSON. parse(message)

// 1. Download the firmware package from url, update the download progress...
client.publish(deviceProgress, getOTAUpgradeData(23))
// 2. Verify whether the file signature is consistent with the sign value according to signMethod
// verifyFirmware()
// 3. Restart the device and upgrade the firmware
// burn & reboot()

}
// Update the upgrade progress
function getOTAUpgradeData(step) {
const payloadJson = {
"id": 1,
"params": {
"step": step,
"desc": "xxxxxxxx"
}
}
console.log(payloadJson)
return JSON. stringify(payloadJson);
}
// The current firmware version of the device
function getFirmwareVersion(version) {
const payloadJson = {
"id": 1,
"params": {
"version": version
}
}

console.log(payloadJson)
return JSON. stringify(payloadJson);
}

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us