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.

Flowchart

Procedure

  1. In the IoT Platform console, create a product and device for the smart lamp and define the features of the product. For more information, see Create a product, Create multiple devices at a time, and Add a TSL feature.
    In this example, the ProductKey parameter is set to al123456789, and the DeviceName parameter is set to light.
  2. 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.
  3. 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?.
  4. Log on to the IoT Platform console, and create a product and device for the mobile app. For more information, see Create a product and Create a device.
    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.

  5. 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.
  6. 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:

    {
        "TargetDevice": "light", 
        "Switch": "off", 
        "Timestamp": 1557750407000
    }
    For information about how to use device SDKs, see What is Link SDK?.
  7. 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.
  8. The mobile app user sends a command from the app.
    1. 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.

    2. 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.
    3. 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.