Gateways and sub-devices

Updated at:

Devices without an IP address can attach to a gateway as sub-devices. The sub-devices then connect to IoT Platform through the gateway to communicate with the platform. This topic describes how to configure the Python Link Software Development Kit (SDK) to connect devices without IP capabilities to IoT Platform through a gateway.

Prerequisites

Background information

Version requirements

Aliyun IoT Python SDK 1.2.4 or later.

API description

  1. Add a sub-device topological relationship and its response callback.

    # Adds topological relationships for sub-devices in a batch and returns the request ID.
    def gateway_add_subdev_topo(self, subdevList)
    
    # The listener callback. Match the request_id in the callback with the request_id returned by gateway_add_subdev_topo.
    def on_gateway_add_subdev_topo_reply(self, request_id, code, data, msg, user_data)
  2. Sub-device logon and its response callback.

    # Logs on sub-devices in a batch. 
    def gateway_login_subdev(self, subdevList)
    
    # The listener callback. Match the request_id in the callback with the request_id returned by gateway_login_subdev.
    def on_gateway_login_subdev_reply(self, request_id, code, data, msg, user_data)
    
  3. Delete a sub-device topological relationship and its response callback.

    # Deletes topological relationships for sub-devices in a batch. 
    def gateway_delete_subdev_topo(self, subdevList)
    
    # The listener callback. Match the request_id in the callback with the request_id returned by gateway_delete_subdev_topo.
    def on_gateway_delete_subdev_topo_reply(self, request_id, code, data, msg, user_data)
  4. Sub-device logout and its response callback.

    # Logs out sub-devices in a batch.
    def gateway_logout_subdev(self, subdevList)
    
    # The listener callback. Match the request_id in the callback with the request_id returned by gateway_logout_subdev.
    def on_gateway_logout_subdev_reply(self, request_id, code, data, msg, user_data)
  5. Dynamic sub-device registration and its response callback.

    # Dynamically registers sub-devices in a batch. The productSecret is required.
    def gateway_product_register_subdev(self, subdevList)
    
    # The listener callback for the result of dynamic registration with a product. Match the request_id in the callback with the request_id returned by gateway_batch_product_register.
    def on_gateway_product_register_subdev_reply(self, request_id, code, data, msg, user_data)
  6. Sub-device topological relationship change notification.

    # The listener callback for a change in the topological relationship.
    def on_gateway_topo_change(self, request_id, params, userdata)

Demo description

After the gateway is online, add a topological relationship for a sub-device and log on the sub-device. This allows the sub-device to send and receive messages. Perform the following steps in the gateway_demo.py file. You can download the demo from Environment requirements and configurations.

  1. In the demo_get_subdev_list function, you can add the device certificates (ProductKey, DeviceName, and DeviceSecret) of the sub-devices to an array.

  2. In the on_connect callback that is invoked after the gateway connects to IoT Platform, execute self.__linkkit.gateway_add_subdev_topo to add the topological relationship for the sub-device.

  3. After the topological relationship is added, the on_gateway_add_subdev_topo_reply callback is invoked. In this callback, execute self.__linkkit.gateway_login_subdev to log on the sub-device.

  4. After the sub-device logs on, the on_gateway_login_subdev_reply callback is invoked. Then, the sub-device can send and receive messages.