Updated on 2024-01-17 GMT+08:00

Example

Preparations

  1. Creating a product and device
    • Create a product. Access the IoTDA service page and click Access Console. Choose Products in the navigation pane and click Create Product. Set the parameters as prompted and click OK. For details, see Creating a Product.
    • Create a device. On the IoTDA console, choose Devices > All Devices in the navigation pane, and click Register Device in the upper right corner of the page. Set the parameters as prompted and click OK. For details, see Registering an Individual Device.

Configuring a Data Forwarding Rule

  1. Access the IoTDA service page and click Access Console.
  2. In the navigation pane, choose Rules > Data Forwarding.

    Figure 1 Data forwarding page

  3. On the Rule List tab page, click Create Rule. Set the rule parameters and forwarded data, and configure the data filtering statement as follows: STARTS_WITH(notify_data.body.topic,'/test/M2M/').

    Figure 2 Creating a data forwarding rule
    • Configuration of Figure 2: For all devices in the resource space XXX, when they report messages with topics containing /test/M2M/, the forwarding rule will be triggered and the messages will be forwarded to the specified forwarding target.
    • To forward the data reported by a specified device, add AND notify_data.header.device_id='${Device ID}' to the SQL statements.
    • To forward the data reported by a specified product, add AND notify_data.header.product_id='${Product ID}' to the SQL statements.
      Figure 3 Example
    • For details about SQL settings, see SQL Statements.

  4. Set the forwarding target. Click Add. Select Device for Forwarding Target, set Topic to $topic() (the topic remains unchanged after forwarding), and set ttl to 5 minutes (data is cached for 5 minutes). Click OK.

    Figure 4 Adding a forwarding target

  5. Enable the rule. The rule creation is complete.

    Figure 5 Enabling the rule

Test

  1. To use the M2M communication function, perform the following steps:
    1. Access the IoTDA service page and click Access Console.
    2. Create a product. Select MQTT for Protocol.
      Figure 6 Creating a product
  2. Register device A (test111) and device B (test222) under the product created in step 1. For details, see Registering an Individual Device.
    Figure 7 Registering a device
  3. This section uses MQTT.fx as an example to describe how to implement M2M communications. You can also test based on your service requirements.
    1. Open two MQTT.fx to simulate devices A (test111) and B (test222).
    2. On the Subscribe page of device B, enter the topic /test/M2M/# and click Subscribe.
      Figure 8 Entering a topic on the Subscribe page of device B
    3. Let device A send a message to device B. On the Publish page of device A, enter the topic /test/M2M/${Any word}. Enter the message to be sent (for example, hello) in the text box, and click Publish.
      Figure 9 Entering a message on the Publish page of device A

      On the Subscribe page of device B, you can see the received message, as shown in the following figure.

      Figure 10 Subscribe page of device B