All Products
Search
Document Center

IoT Platform:Configure OTA update for devices

Last Updated:Nov 20, 2023

Alibaba Cloud IoT Platform provides device SDKs that allow devices to connect to the platform. This topic describes how to configure device connection and over-the-air (OTA) updates. In this example, the fota_posix_demo.c sample program provided by IoT Platform is used.

Usage notes

In this topic, the permissions of a common user are used to perform all operations. If you want to perform specific operations that require administrator permissions, run thesudocommand.

Prerequisites

A product and a device are created, and the device certificate is obtained. The device certificate includes a ProductKey, a DeviceName, and a DeviceSecret. For more information, see Create a product and Create a device.

In this example, a device named SDevice1 is created.

Prepare a development environment

  • In this topic, Link SDK for C is used on the Linux operating system. To compile the SDK, we recommend that you use 64-bit Ubuntu 16.04.

  • The following software is required for SDK development and compilation:

    make (version 4.1 or later) and gcc (version 5.4.0 or later)

    Run the following command to install the software:

    sudo apt-get install -y build-essential make gcc

Procedure

  1. Click the C SDK 4.x link to obtain Link SDK for C.

  2. Decompress the package in your computer and modify the device connection information in /LinkSDK/demos/fota_posix_demo.c.

    In this example, the information of SDevice1 is added.

    char *product_key       = "g18***";
    char *device_name       = "SDevice1";
    char *device_secret     = "cefbebf00***";
    ...
    ...
    char  *url = "iot-***.mqtt.iothub.aliyuncs.com";

    Parameter

    Example

    Description

    url

    iot-***.mqtt.iothub.aliyuncs.com

    The endpoint to which you want to connect the device.

    • Endpoint of a public instance of the new version: On the Instance Details page, click View Development Configurations in the upper-right corner. In the Development Configurations panel, view the endpoint.

    • Endpoint of a public instance of the previous version: The endpoint is in the format of ${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com.

    For more information about instances, see View the endpoint of an instance.

    product_key

    g18***

    The device certificate that is used for verification. For more information, see Obtain device verification information.

    In this example, the unique-certificate-per-device verification method is used.

    device_name

    SDevice1

    device_secret

    cefbebf00***

    In fota_posix_demo.c, sample code for performing OTA updates is provided. The version number before OTA updates is 1.0.0. In actual business scenarios, you must obtain the version number from the device settings and specify the logic to submit the version number. For more information, see Sample code.

       cur_version = "1.0.0";
        res = aiot_ota_report_version(ota_handle, cur_version);
        if (res < STATE_SUCCESS) {
            printf("aiot_ota_report_version failed: -0x%04X\r\n", -res);
        }
  3. Log on to the Linux virtual machine and run the following command to install the required software:

    sudo apt-get install -y build-essential make gcc
  4. Upload the LinkSDK file that you modified in Step 2 to the development environment.

  5. Go to the SDK root directory /LinkSDK and run the make command to compile the sample code:

    make clean
    make

    The fota-posix-demo file that is generated is stored in the ./output directory.

  6. Run the following command to start the demo program:

    ./output/fota-posix-demo
  7. View the log and status of the device.

    • The following figure shows the device connection information and the reported version numbers.运行日志

    • Go to the corresponding instance in the IoT Platform console, choose Devices > Devices in the left-side navigation pane, find the device in the list, and then view its status. If Online is displayed in the Enabled/Disabled column, the device is connected to IoT Platform.

      设备在线

What to do next

Push firmware files to devices: The IoT Platform sends OTA update tasks to the devices. Then, the online devices obtain the update information and perform the OTA update.