All Products
Search
Document Center

IoT Platform:Perform an OTA update on a device

Last Updated:Nov 11, 2025

This topic describes how to use Link SDK for Python to configure the over-the-air (OTA) feature and perform an OTA update on a device.

Process of an OTA update

  1. A device submits its firmware version number.

  2. The device subscribes to the topics about OTA updates.

  3. You must configure an OTA update task on the OTA Update page in the IoT Platform console. You can specify multiple conditions to identify the device whose firmware you want to update.

  4. If the device successfully subscribed to topics about OTA updates, you can find the device in the OTA update task that you configured. In this case, the device receives a notification that contains the following information:

    • The version to which you can update the device firmware.

    • The download address, size, and message-digest algorithm (MD5) signature of the OTA update package.

  5. The device downloads the OTA update package and then the firmware update starts. The device submits the update progress to IoT Platform.

  6. After the update is complete, the device automatically submits the new firmware version.

Configure the OTA feature

For more information about the code, see the ota_demo.py file in the demo package.

  1. Register an OTA callback. Configure an OTA update task in the IoT Platform console.

  2. If an OTA update is pushed, the on_ota_message_arrived callback returns the URL, version number, size, signature, module name, and signature method of the destination firmware.

    Then, you can perform an OTA update based on the preceding information. If you want to perform an OTA update, call the download_ota_firmware function.

    from linkkit import linkkit
    import logging
    import time
    
    
    def on_ota_message_arrived(ota_notice_type, version, size, url, sign_method, sign, module, extra):
        # If the value of the ota_notice_type parameter is 0, no OTA update task is configured in the IoT Platform console. If the value is 1, the OTA update task is pushed from IoT Platform. If the value is 3, the device queries OTA tasks in the IoT Platform console.
        if ota_notice_type > 0:
            # TODO: Determine whether to perform an OTA update and when to perform the update based on the version number.
    
            # TODO: If the system requires a long period of time to download a destination firmware package whose size is larger than expected, we recommend that you create a separate thread to process the download operation. This prevents other threads from being blocked by the download operation.  
            # If you request multiple OTA firmware updates, or receive OTA messages that are pushed from IoT Platform when you request the OTA firmware updates within a short period of time, you must implement the data processing logic for multiple threads that concurrently run. 
            # This prevents a file from being written by multiple threads at the same time.
    
            print("on_ota_message version:" + version + " size:" + str(size) + " url:" + url + " sign_method:" + sign_method)
            print("on_ota_message sign:" + sign + " module:" + module + " extra:" + extra)
            # TODO: Modify the firmware_path variable to store the firmware to a custom path.
            firmware_path = "demo_ota.py"
            ret = lk.download_ota_firmware(url, firmware_path, sign_method, sign)
    
            if lk.ErrorCode.SUCCESS == ret:
                # TODO: Deploy the destination firmware, submit a new firmware version, and then check whether the update is complete.
                print("report version ")
                lk.ota_report_version(module, version)
                pass
            else:
                print("download error code %x" % ret.value)
        else:
            print("no firmware ")
    
    # TODO: Specify the device certificate information.
    lk = linkkit.LinkKit(
        host_name="cn-shanghai",
        product_key="xxxxxxxxxxx",
        device_name="device-name",
        device_secret="yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy")
    # TODO: Specify an MQTT endpoint.
    lk.config_mqtt(endpoint="${YourInstanceId}")
    lk.on_ota_message_arrived = on_ota_message_arrived
    __log_format = '%(asctime)s-%(process)d-%(thread)d - %(name)s:%(module)s:%(funcName)s - %(levelname)s - %(message)s'
    logging.basicConfig(format=__log_format)
    lk.enable_logger(logging.DEBUG)
    lk.connect_async()
    
    while True:
        time.sleep(1)
  3. After the destination firmware is downloaded, the device manufacturer can update the device by its own OTA-based update method. After the update is complete, call the ota_report_version function to submit the new version number. If the new version number that is received by IoT Platform is the same as the destination version number, the update is successful.

Download firmware

By default, Link SDK for Python receives OTA update messages from IoT Platform. You can configure Link SDK for Python for a device to query information about firmware updates in the IoT Platform console.

def query_ota_firmware(self, module=None)
Note

By default, the module parameter is set to None. The value None indicates the default module. If you want to update a specific module, specify the name of the module for the module parameter.