This article describes how to use the topic-based message routing service of IoT Platform to build an M2M communication architecture. 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.

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 and DeviceName parameters for the smart lamp are set to al123456789 and light respectively.
  2. Develop a device SDK for the smart lamp.
    In this example, the device and IoT Platform use MQTT to communicate with each other.

    The message payload is as the following.

    {
        "TargetDevice": "light", 
        "Switch": "off", 
        "Timestamp": 1557750407000
    }
    For more information about device SDK development, see Link Kit SDK documentation.
  3. In the IoT Platform console, create a product and a device for the mobile app.
    In this example, the ProductKey and DeviceName parameters of the mobile app are respectively set to al987654321 and ControlApp.
    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 can connect to IoT Platform as a device.
  4. 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.
  5. Develop the mobile app.
    In this example, the mobile app and IoT Platform use HTTPS to communicate with each other.
  6. 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 specified by the deviceName parameter. In this example, the device name is light.
  7. The mobile app user sends control commands from the app.
    1. The topic to which the app sends commands 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.