Gateways and sub-devices
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
You have created a gateway and a sub-device. For more information, see Create a product and Create a device.
Create a product and a device for the gateway. When you create the product, set Node Type to Gateway Device.
Create a product and a device for the sub-device. When you create the product, set Node Type to Gateway Sub-device.
You have completed the steps in Environment requirements and configurations and Authentication and connection.
Background information
For more information about the sub-device feature of IoT Platform, see Gateways and sub-devices.
For more information about the communication protocols between sub-devices and IoT Platform, see Manage topological relationships and Bring sub-devices online or take them offline.
Version requirements
Aliyun IoT Python SDK 1.2.4 or later.
API description
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)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)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)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)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)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.
In the
demo_get_subdev_listfunction, you can add the device certificates (ProductKey, DeviceName, and DeviceSecret) of the sub-devices to an array.In the
on_connectcallback that is invoked after the gateway connects to IoT Platform, executeself.__linkkit.gateway_add_subdev_topoto add the topological relationship for the sub-device.After the topological relationship is added, the
on_gateway_add_subdev_topo_replycallback is invoked. In this callback, executeself.__linkkit.gateway_login_subdevto log on the sub-device.After the sub-device logs on, the
on_gateway_login_subdev_replycallback is invoked. Then, the sub-device can send and receive messages.