IoT Platform:Use topic-based message routing to establish M2M communication
Last Updated:Oct 26, 2022
This topic describes how to use the topic-based message routing service of IoT Platform
to build an M2M communication architecture. In this topic, a connection between a
smart lamp and a mobile app is used as an example.
Background information
The following figure shows how to control the smart lamp by using the mobile app.
In this example, the ProductKey parameter is set to al123456789, and the DeviceName parameter is set to light.
Add a custom topic on which the smart lamp has the Subscribe permission. The smart
lamp can use the topic to receive the commands that are sent by the mobile app.
In this example, the /al123456789/light/user/set topic is added. For more information, see Custom topics.
Use the device SDK to connect the smart lamp to IoT Platform. This way, the smart
lamp can receive and implement the commands sent by the mobile app.
In this example, the smart lamp and IoT Platform are connected over MQTT.
For information about how to use device SDKs, see What is Link SDK?.
In this example, the ProductKey parameter is set to al987654321, and the DeviceName parameter is set to ControlApp.
After you create the product and device, the mobile app can connect to IoT Platform
as a device.
When a registered user logs on to the mobile app, your server sends the device information
to the mobile app. This way, the mobile app is connected to IoT Platform.
Call the CreateTopicRouteTable operation to create a message routing relationship between the topics of the app
and the smart lamp.
Set the SrcTopic parameter to the topic of the app: /al987654321/ControlApp/user/update.
Set the DstTopics parameter to the topic of the smart lamp: /al123456789/light/user/set.
Develop the mobile app.
In this example, the mobile app and IoT Platform are connected over HTTPS.
Format of a command that the mobile app sends to the smart lamp:
For information about how to use device SDKs, see What is Link SDK?.
The mobile app user scans the QR code to bind the app with the smart lamp.
After the app sends a request to the server to bind a device, the server binds the
smart lamp and returns the device name. The returned name is indicated by the deviceName
parameter. In this example, the device name is light.
The mobile app user sends a command from the app.
The app sends commands to the topic in IoT Platform. In this example, the topic is
/al987654321/ControlApp/user/update.
Only JSON format commands are supported.
IoT Platform routes commands to the topic of the smart lamp device based on the defined
message routing relationship. The topic is /al123456789/light/user/set.
The smart lamp device receives the commands and performs the required operations.
Note You can configure the mobile app to send an unbinding request to the server. This
way, the server calls the DeleteTopicRouteTable operation of IoT Platform to delete the message routing relationship. After the routing
relationship is deleted, you cannot use the app to control the smart lamp.