Use topics for communication

更新时间:
复制 MD 格式

After you define a topic, devices can use it to communicate with IoT Platform. This enables communication among devices, IoT Platform, business servers, and other cloud products.

Prerequisites

Connect a device to IoT Platform

  1. Connect your device to IoT Platform.

    Important

    When you connect a device using the device emulator, C Link SDK V3.1, V3.2, or V4.x, or the Python Link SDK, the device automatically subscribes to basic communication topics and Thing Specification Language model topics with Subscribe permission. The MQTT.fx client does not automatically subscribe to these topics.

  2. The device communicates with IoT Platform.

    image

    Custom topics

    If the device message format is not Alink JSON, you must use message parsing to parse the data from custom topics that are reported by the device into the Alink JSON format. No restrictions are imposed on the data format for messages that are sent to devices through custom topics. For more information about message parsing and its usage, see What is message parsing?.

    TSL model topics

    1. Before you use TSL model topics for communication, you must add a TSL model to the product that you created. For more information, see Add a single TSL model or Add TSL models in batches.

    2. If the device message format is not Alink JSON, you must use message parsing to parse data in a custom format that is reported by the device into the Alink JSON format. You must also parse data in the Alink JSON format that is sent from IoT Platform into the device's custom format. For more information about message parsing and its usage, see What is message parsing?.

    3. After the data that is reported by the device is parsed and converted, IoT Platform validates the property, event, and service invocation data, such as service request parameters and service invocation results, that are reported by the device. The validated data is displayed on the TSL Model Data tab of the Device Details page for the device in the IoT Platform console.

      • You can select the data validation type when you create a product. For more information, see Create a product.

      • TSL model validation types and descriptions

        Type

        Description

        Scope

        Strict validation

        Validates all fields in the data reported by the device. Only validated data is forwarded.

        Data that passes verification is displayed on the TSL Model Data tab of the Device Details page for the device in the IoT Platform console. Data that fails the verification is not displayed.

        For products created before October 14, 2020, strict validation is the only validation method.

        Weak validation

        Validates only the identifier and dataType fields in the data reported by the device. Other fields are not validated. All data is forwarded.

        Data that passes the verification is displayed on the Thing Specification Language Model Data tab of the Device Details page for the corresponding device in the IoT Platform console. Data that fails the verification is not displayed.

        You can view the data that fails validation using the checkFailedData parameter in the forwarded data. For more information, see Data format.

        For products created from October 14, 2020, to December 31, 2020, you can no longer change the current validation method.

        When you create a product on or after January 1, 2021, you must select weak validation or no validation. You cannot change the validation method after the product is created.

        For more information, see Create a product.

        No validation

        Does not validate the data reported by the device. All data is forwarded.

        The TSL model data is used as follows:

        • TSL model property reporting: Property data is not displayed on the TSL Model Data > Running Status tab of the Device Details page for the device in the IoT Platform console.

        • TSL model service invocation: All services use asynchronous invocation.

        • TSL model event reporting: All events are of the information type.

Communication methods among devices, IoT Platform, business servers, and other cloud products

image
Important
  • A device cannot subscribe to another device's topic or publish messages to another device's topic. For example, the topic /a19mzPZ****/device1/user/update belongs to device1. Only device1 can use this topic to publish or subscribe to messages. device2 cannot use this topic. To enable device-to-device communication, see the Device-to-device communication section below.

  • A device can subscribe to predefined system topics of IoT Platform as needed, such as /sys/${productKey}/${deviceName}/thing/service/property/set. However, a device cannot use /sys/${productKey}/${deviceName}/# to subscribe to all system topics that start with /sys/${productKey}/${deviceName}.

  • Currently, you can cancel a topic subscription only by calling the unsub interface in the device-side Link SDK. For C, see aiot_mqtt_unsub. For other languages, see the device-side Link SDK documentation.

  • This topic describes only some topic-based communication methods. For more information about other communication methods, see Overview of communication methods in IoT Platform.

Server receives device data

  1. To report data, a device needs the Publish permission for the corresponding topic.

  2. To report TSL model data, a device must use the specified topic and format. For more information, see Device properties, events, and services.

  3. Configure server-side subscription. This forwards device data to your server in real time. Server-side subscription forwards messages based on the message type. For more information about the mapping between message types and topics, see Data format. If you want to use a Resource Access Management (RAM) user or RAM role to subscribe to device messages, you must configure custom permissions. For more information, see RAM user access and RAM authorization for IoT Platform.

    • AMQP server-side subscription is recommended. It is easy to configure and provides real-time access to data that is reported by devices. For more information, see AMQP server-side subscription.

    • To perform complex or fine-grained processing on device data, you can configure MNS server-side subscription. For more information about the billing of Simple Message Queue (formerly MNS), see Billing.

Server sends data to a device

  1. To receive messages, a device needs the Subscribe permission for the corresponding topic.

  2. After a device actively subscribes to a topic, the topic appears on the Topic List tab of the device details page. The following subscription methods are available:

    • Connect to IoT Platform using MQTT.fx: After the device is online, use the Subscribe feature to subscribe to the device topic. You can use the online debugging feature in the console to set properties or invoke services.

    • SubscribeTopic: After an emulated or developed device connects to IoT Platform and is online, you can call this API operation to subscribe to the device topic.

    • Device emulator: The device automatically subscribes to basic communication topics and TSL model topics.

    • Device-side Link SDK: If you use C Link SDK V3.1, V3.2, or V4.x, or the Python Link SDK, your device automatically subscribes to Basic Communication Topics and Thing Specification Language Model Topics that have the Subscribe permission. To complete the topic subscription, you can connect your device to IoT Platform and activate it to bring it online. You can then use the Online Debugging feature to set properties or invoke services.

  3. The business server calls cloud APIs. For more information about cloud APIs, see Server remotely controls a device.

    • Synchronous method: To send a message and wait for a synchronous response from the device, you can use revert-RPC (RRPC).

    • Asynchronous method:

      • Custom topics: Call the Pub API operation.

      • TSL model topics: Call SetDeviceProperty to send an instruction to set property values for a single device. Call InvokeThingService to send an instruction to invoke a service for a single device.

Device-to-device communication

A device cannot subscribe to another device's topic or publish messages to another device's topic. You can use data forwarding or topic message routing to forward the message content from one device's topic to another device's topic. This enables communication between different devices.

Send messages from the console to a device

Log on to the IoT Platform console and click the card of the instance.

  • On the O&M > Online Debugging page, you can send instructions to the device to debug features. For more information, see Online debugging.

  • On the Subscribed Topic List of the device details page, find a subscribed custom topic and click Publish Message. You can then use this topic to publish a message from IoT Platform to the device. This does not apply to custom topics that use wildcard characters. For more information, see Use custom topics for communication.

Forward device data to other cloud products

  1. To report data, a device needs the Publish permission for the corresponding topic.

  2. To report TSL model data, a device must use the specified topic and format. For more information, see Device properties, events, and services.

  3. Configure data forwarding. You can forward specified device data to other cloud products, such as Simple Message Queue (formerly MNS) (SMQ), ApsaraDB RDS, Tablestore, Function Compute, Time Series Database (TSDB), Lindorm, DataHub, and ApsaraMQ. For more information about the concept and flow of data forwarding, see Overview of data forwarding.

Optional steps

View cloud logs

  1. Log on to the IoT Platform console.

  2. On the Instance Overview tab, under All Environments, find the instance and click its card.

  3. In the navigation pane on the left, choose O&M > Log Service, and click the IoT Platform Logs tab.

    • IoT Platform logs contain communication records for IoT Platform, devices, and business servers. For more information about error codes and troubleshooting methods, see IoT Platform logs.

    • You can copy the messageID or traceID of the target topic message. You can trace the full path of any message as it flows through IoT Platform to quickly locate faults. For more information, see Message trace.

Publish a product

Note

Revoking a product publication does not affect communication between devices and IoT Platform.

  1. On the Product Details page, click Publish in the upper-right corner to publish the product.

  2. After a product is published, you cannot modify or delete it. For more information, see (Optional) Publish a product.

References

  • For more information about communication methods among devices, IoT Platform, servers, and other cloud products after you define a topic, see Use topics for communication. For more information about other communication methods, see Overview of communication methods in IoT Platform.

  • Data forwarding and server-side subscription process and transmit data based on the data format. For more information about the mapping between data formats and specific topics, see Data format.

  • Devices and the cloud use the Alink protocol for TSL model data communication. This includes devices reporting property or event messages to the cloud, and the cloud sending messages to set properties or invoke services on devices. For a demo, see TSL model communication.

  • For introductory videos on device connection and TSL model usage, see Device connection.

  • For more information about the process of connecting devices of different protocols and types to IoT Platform, see Overview of device connection. To download the device-side Link SDK, see Device connection Link SDK.

  • TSL model topic data is automatically written to a time series data storage space. For more information about how to manage TSL model data storage, see Disable or enable TSL model data storage.

  • To store custom topic data for queries, you must first create a time series data storage rule. For more information, see Configure custom topic data storage.

FAQ