Help Center/ IoT Device Access/ Best Practices/ Device Access/ Upgrading the MQTT Device Firmware in OTA Mode
Updated on 2024-04-29 GMT+08:00

Upgrading the MQTT Device Firmware in OTA Mode

Scenarios

Message Queuing Telemetry Transport (MQTT) is a publish/subscribe messaging protocol that transports messages between clients and a server. It is suitable for remote sensors and control devices (such as smart street lamps) that have limited computing capabilities and work in low-bandwidth, unreliable networks through persistent connections. Firmware upgrade is a basic function that network devices must support. Online upgrades are important especially when the firmware needs to be upgraded due to security vulnerabilities, software bugs, function optimization, and device performance improvement. This topic describes how to upgrade the firmware in Huawei Cloud IoTDA using MQTT.fx to simulate a device.

The software upgrade process is the same as that of the firmware upgrade. The only difference is that the parameters for reporting version numbers are different. For firmware upgrade, the parameter that specifies the version number is fw_version. For software upgrade, the parameter is sw_version. For details, see Device Reporting the Software and Firmware Versions.

Development Process

Figure 1 Upgrade process
Figure 2 MQTT device upgrade process

Development Environment

  • Software: Huawei Cloud IoTDA, 64-bit Windows 7 or later (64-bit Windows 10 is used in the following demonstration), and MQTT.fx simulator.

Prerequisites

  • You have registered a Huawei Cloud account. If you have not registered, click here to complete the registration.
  • You have completed real-name authentication. If the authentication is not complete, complete real-name authentication on the Huawei Cloud console. Otherwise, IoTDA cannot be used.
  • You have subscribed to the IoTDA service. If you have not subscribed to the service, go to the IoTDA service page, and click Access Console to subscribe to the service.

Preparations

  • 1. Download and install MQTT.fx (version 1.7.1 or later).
  • Prepare the upgrade package. Obtain the firmware upgrade package and its version number from the module vendor. Temporary files are used in this demonstration.
  • Create an MQTT product. (If an MQTT product already exists, skip this step.)
  • Register an individual device.
  • Add the registered device to a device group, which will be used during upgrade task creation.

Uploading an Upgrade Package

  1. Visit the IoTDA product page and click Access Console.
  2. In the navigation pane, choose Devices > Software/Firmware Upgrades.
  3. Click the Manage Resource Package tab and click Firmware List.
  4. Click Upload. On the page displayed, you can upload a firmware package from OBS or your local PC.

    Figure 3 Uploading the upgrade package - OBS file
    Figure 4 Uploading the upgrade package - local file

  5. After the upgrade package is uploaded, configure package parameters and click OK.

    Figure 5 Uploading the upgrade package - OBS file parameters
    Figure 6 Uploading the upgrade package - local file parameters

  6. The uploaded upgrade package is displayed in the firmware list.

    Figure 7 Firmware list

  7. Click Create Upgrade Task. On the Firmware Upgrades tab page, click Create Task. On the page displayed, configure parameters, select upgrade packages, select devices to upgrade, and click Create Now.

    Figure 8 Creating a firmware upgrade task - Test_upgrade_1

    When creating an upgrade task, you can select up to 10 upgrade packages. Supported source versions of the upgrade packages must be unique. If no source version is specified for an upgrade package, the package can be used to upgrade devices of all source versions by default.

  8. View the created task in the task list. You can click Detail to view the task details. On the task details page, you can stop executing upgrade tasks for up to 100 devices in batches or an upgrade task for a single device. You can retry failed upgrade tasks for up to 100 devices in batches or an upgrade task for a single device. You can click All Retry to retry the upgrade task for all failed devices.

    Figure 9 Task details - Test_upgrade_1

Service Implementation

  1. Use MQTT.fx to simulate device access to the platform. For details, see "Performing Connection Authentication".
  2. Use MQTT.fx to subscribe to the downstream message topic of the platform. MQTT.fx receives the version query command delivered by the platform.

    Topic

    Downstream: $oc/devices/{device_id}/sys/events/down

    Parameters

    For details, see Platform Delivering an Event to Obtain Version Information.

    Figure 10 Subscribe/Push

  3. Report the software and firmware version information using MQTT.fx.

    Topic

    Upstream: $oc/devices/{device_id}/sys/events/up

    Parameters

    For details, see Device Reporting the Software and Firmware Versions.

    Example

    Topic: $oc/devices/{device_id}/sys/events/up   
    Data format:  {
          "object_device_id": "{object_device_id}",
          "services": [{
              "service_id": "$ota",
              "event_type": "version_report",
              "event_time": "20151212T121212Z",
              "paras": {
                  "sw_version": "v1.0",
                  "fw_version": "v1.0"
              }
          }]
      } 
    Figure 11 Reporting version numbers

  4. After the version numbers are reported, the simulator receives an upgrade notification from the platform. The notification is as follows:

    Topic

    Upstream: $oc/devices/{device_id}/sys/events/down

    Parameters

    For details, see Platform Delivering an Upgrade Event.

    Figure 12 Obtaining the upgrade notification

  5. After the device receives the upgrade notification, send an HTTP request to download the upgrade package.

    A cURL command is used in this demonstration.

    Figure 13 Downloading the upgrade package

    Example

    curl -X GET -H "Authorization:Bearer ****************************" "https://100.93.28.202:8943/iodm/dev/v2.0/upgradefile/applications/ddecccc223574aee9466fe8f7e16a205/devices/6079042873515a02c16ffd82_123456789/packages/4f201f38c281ca5d40794a3f" -v -k
    • Add Authorization to the HTTP request header. The value of Authorization is Bearer {access_token}, where the value of {access_token} is the token in the upgrade notification. Leave a space between Bearer and {access_token}.
    • If event_type is set to firmware_upgrade_v2 or software_upgrade_v2, the request header does not need to be carried in the request for downloading the software or firmware package. An example request is as follows:

    GET https://******.obs.cn-north-4.myhuaweicloud.com:443/test.bin?AccessKeyId=DX5G7W*********

  6. Enable the device to report the upgrade status.

    Topic

    Upstream: $oc/devices/{device_id}/sys/events/up

    Parameters

    For details, see Device Reporting the Upgrade Status.

    Example

    Topic: $oc/devices/{device_id}/sys/events/up
    Data format:
      {     "object_device_id": "{object_device_id}",
         "services": [{
             "service_id": "$ota",
             "event_type": "upgrade_progress_report",
             "event_time": "20151212T121212Z",
             "paras": {
                 "result_code": 0,
                 "progress": 50,
                 "version": "V1.0",
                 "description": "upgrade processing"
             }
         }] }

    The following figure shows that the upgrade progress is 50%, which is displayed on the platform.

    Figure 14 Reporting the upgrade progress (50%)

    Figure 15 Upgrade progress - Test_upgrade_1-

  7. Complete the upgrade.

    If the upgrade progress is 100% and the current version is the target version, the upgrade task success will be displayed on the platform.

    Figure 16 Reporting the upgrade progress (100%)

    Figure 17 Firmware upgrade