Use topic-based message routing to establish M2M communication

更新时间:
复制 MD 格式

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, go to your instance. Create a product and a device for the smart light, and define its features. For more information, see Create a product, Create multiple devices at a time, and Add a TSL model for a single device.

    In this example, set Product Name to Smart Light, Category to Custom Category, and Node Type to Directly Connected Device. Leave the other parameters at their default values.

    Set DeviceName to light.

    This example uses a Smart Light with the ProductKey al123456789.

    Add Thing Specification Language (TSL) model features as needed. For example, add the Switch property. Set the data type to Boolean, where 0 means off and 1 means on, to control the smart light.

  2. Add a custom topic with subscribe permissions to receive control instructions from the mobile app.

    This example adds the topic: /al123456789/light/user/set. For more information, see Use custom topics for communication.

  3. Develop the smart light device. Implement features such as connecting the smart light to IoT Platform, and receiving and executing instructions.

    In this example, the device uses MQTT to communicate with IoT Platform. The smart light device must subscribe to the topic /al123456789/light/user/set to receive instructions. After the device receives an instruction, develop a logical method to execute it.

    For more information about device SDK development, see Device connection with Link SDK.

  4. In the IoT Platform console, go to your instance and create a product and a device for the mobile app. For more information, see Create a product and Create a single device.

    In this example, set Product Name to Mobile App, Category to Custom Category, and Node Type to Directly Connected Device. Use the default values for the other parameters.

    Set DeviceName to ControlApp.

    In this example, the Mobile App has a ProductKey of al987654321.

    After you create a product and a device for the mobile app, the app can connect to IoT Platform as a device.

    When a mobile app user registers and logs on, your server sends the app's device information to the mobile app. The mobile app can then connect to IoT Platform as a device.

  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 device.

    In this example, the mobile app uses HTTPS to communicate with IoT Platform.

    The following shows the payload data format for a control instruction. The mobile app sends this instruction to the smart light over a custom topic.

    {
        "TargetDevice": "light", 
        "Switch": 0, 
        "Timestamp": 1557750407000
    }

    For more information about device SDK development, see Device connection with 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.