This article describes how to use the data forwarding feature of the rules engine in IoT Platform to build an machine-to-machine (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.

M2M communication

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 of the smart lamp are respectively set to al123456789 and light.
  2. Develop a device SDK for the smart lamp.
    In this example, the device and IoT Platform use MQTT to communicate with each other.
    For information about how to develop a device SDK, see the Link 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. Develop the mobile app.
    In this example, the mobile app and IoT Platform use HTTPS to communicate with each other.

    Format of a command that the mobile app sends to the smart lamp:

    {
        "TargetDevice": "light", 
        "Switch": "off", 
        "Timestamp": 1557750407000
    }
  5. Develop the device SDK to connect the smart lamp with IoT Platform, receive the command, and implement the command.
  6. Configure a data forwarding rule to forward the command from the mobile app to the topic of the smart lamp.
    1. Log on to the IoT Platform console. Choose Rules > Data Forwarding.
    2. Click Create Rule. In the dialog box that appears, enter a rule name and click OK.
    3. In the A flow rule has been created message, click Edit.
    4. On the Data Forwarding Rule page, click Write SQL in the Data Processing section.
    5. In the Write SQL dialog box, write an SQL statement that is used to process the forwarded message, and then click OK. You can use the SQL statement to extract required fields from the message that is sent by the mobile app. Only the fields are sent to the smart lamp.

      In this example, the TargetDevice, Timestamp, and Switch fields are extracted.

      M2M communication
    6. On the Data Forwarding Rule page, click Add Operation in the Data Forwarding section. In the dialog box that appears, set a forwarding message destination. In this example, specify a smart lamp topic that has the Subscribe permission as the destination.
      Note
      • Select Publish to another Topic.
      • When you specify devices, use ${TargetDevice} to indicate all smart lamps. In this example, ${TargetDevice} indicates the smart lamp named light.
      M2M communication
  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 specified 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 the command to the topic in IoT Platform. In this example, the topic is /al987654321/ControlApp/user/update.
    2. Then, IoT Platform sends the command to the topic of the smart lamp based on the data forwarding rule. In this example, the topic is /al123456789/light/user/set.
    3. The smart lamp device receives the commands and performs the required operations.
    Note The app can also send a request to your server to unbind the smart lamp device. After the device is unbound, you cannot use the app to control the smart lamp.