All Products
Search
Document Center

IoT Platform:Subscribe to device messages from IoT Platform

Last Updated:Nov 27, 2023

After a device is connected to IoT Platform, the device submits data to IoT Platform. Then, the data can be forwarded to your server by using Advanced Message Queuing Protocol (AMQP). This topic describes how to configure an AMQP server-side subscription. This way, your server can receive data from a street lamp by using an AMQP client.

Prerequisites

A device is connected to IoT Platform and data is submitted. For more information, see Connect a device to IoT Platform and submit data.

Prepare a development environment

In this example, Java is used to develop a sample Advanced Message Queuing Protocol (AMQP) client. We recommend that you use an Apache Qpid JMS client. To download the client and view the instructions, see Qpid JMS 0.57.0.

In this example, the following components are used in the development environment:

Configure an AMQP server-side subscription

  1. On the Overview tab in the IoT Platform console, click IoT_Test_Environment.

  2. Configure a consumer group to consume messages. Your server can obtain the messages by listening to the consumer group.

    1. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription. On the Server-side Subscription page, click Consumer Groups.

    2. On the Consumer Groups tab, click Create Consumer Group.

    3. In the Create Consumer Group dialog box, set the Consumer Group Name parameter to Receive_Thermostat_Data and click OK.

      After the consumer group is created, the Consumer Group Details page appears. You can view the consumer group ID on the page. You must copy and save the consumer group ID. The ID is required when you connect the AMQP client to IoT Platform.消费组ID

  3. Configure the server-side subscription for the product to which the thermostat device belongs. This allows your server to subscribe to various types of messages of the product.

    1. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription.

    2. On the Subscriptions tab of the Server-side Subscription page, click Create Subscription.

    3. In the Create Subscription dialog box, configure the parameters and click OK. The following table describes the parameters.

      Parameter

      Description

      Products

      Select Home_Thermostat.

      Subscription Type

      Select AMQP.

      Consumer Group

      Select the Receive_Thermostat_Data consumer group that you created in the previous step.

      Message Type

      Select Device Upstream Notification and Submit historical TSL Data. For more information about message types, see Configure a subscription.

    You can use the data forwarding feature to send data to AMQP consumer groups and use AMQP clients to receive messages. For example, use the Thermostat_Data_Forwarding rule to forward the submitted TSL data to the Receive_Thermostat_Data consumer group. For more information, see Forward data to an AMQP consumer group.

Connect an AMQP client to IoT Platform

  1. Download the demo package and decompress the package.

  2. Open IntelliJ IDEA and import the sample project amqp-demo in the demo package.

    In the pom.xml file, the Maven dependency is added to download a Qpid JMS client.

  3. In the AmqpClient.java file that is stored in the src/main/java/com.aliyun.iotx.demo directory, configure the required parameters to connect the JMS client to IoT Platform. The following table describes the parameters.

        private static String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        private static String accessSecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        private static String consumerGroupId = "1yRO*****************";
    
        // iotInstanceId: the ID of the instance. If you use a public instance that was activated before July 30, 2021, specify an empty string for the parameter. 
        private static String iotInstanceId = "iot-cn-******";
    
        // The value of the clientId parameter is displayed in the IoT Platform console as the client ID on the consumer group status page for service subscription. 
        // We recommend that you use a unique identifier, such as the UUID, MAC address, or IP address as the value of the clientId parameter. This allows you to identify clients. 
        private static String clientId = "test_IoT";
    
        //${YourHost} specifies an endpoint. For more information about endpoints, see the "Connect an AMQP client to IoT Platform" topic. 
        private static String host = "iot-cn-******.amqp.iothub.aliyuncs.com";
    
        // The number of connections that you want a process to establish.
        // The consumption queries per second (QPS) of a connection is limited. You can specify up to 64 connections. For more information, see the "Limits" topic.
        // The number of connections and the corresponding consumption QPS are relevant to the rebalance scheme. We recommend that you increment the number of connections by one each time the consumption QPS increases by 500.
        private static int connectionCount = 4;

    Parameter

    Description

    accessKey

    Log on to the IoT Platform console, move the pointer over the profile picture, and then click AccessKey Management to obtain the AccessKey ID and AccessKey secret.

    Important

    In this example, environment variables are configured to manage the AccessKey pair. This helps prevent security risks that are caused by hard coding the AccessKey pair into your business code.

    You must add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to your on-premises operating system and specify the AccessKey ID and AccessKey Secret.

    accessSecret

    consumerGroupId

    The ID of the Receive_Thermostat_Data consumer group. For more information, see Step 2 in the Configure an AMQP server-side subscription section.

    iotInstanceId

    The ID of the IoT_Test_Environment instance. You can view the instance ID on the Overview tab in the IoT Platform console.

    clientId

    The ID of the client. You must specify a custom ID. The ID must be 1 to 64 characters in length. We recommend that you use a unique identifier as the client ID, such as the UUID, MAC address, or IP address of the server on which the client runs.

    After the AMQP client is connected to IoT Platform and started, perform the following steps to view the details of the client: Log on to the IoT Platform console and click the card of the instance that you want to manage. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription. On the Consumer Groups tab, find the consumer group that you want to manage and click View in the Actions column. The ID of each client is displayed on the Consumer Group Status tab. You can use client IDs to identify clients with ease.

    host

    The AMQP endpoint that is saved after you purchase the instance. For more information, see Purchase an Enterprise Edition instance.

    On the Instance Details page of the IoT_Test_Environment instance in the IoT Platform console, you can click View Development Configurations and click AMQP to view the AMQP information.

    connectionCount

    The number of connections that are enabled on the AMQP client. Maximum value: 128. This parameter is used for scale-out in real-time message pushing scenarios.

    On the Consumer Group Details page, each connected client is displayed as ${clientId}+"-"+ Number. The minimum number is 0.

  4. If an output that is similar to the following information appears after the AmqpClient.java sample code file is executed, the AMQP client is connected to IoT Platform and can receive messages.

    Note

    In this example, the Thread.sleep(60 * 1000); code snippet is added to terminate the program after the program starts and runs for 1 minute. You can specify the running time based on your business requirements.

    运行结果

    Response parameters

    Parameter

    Example

    Description

    topic

    /***********/******/thing/event/property/post

    The topic that is used to submit device properties.

    messageId

    1324198300680719360

    The message ID.

    content

    {"temperature":23,"humidity":21,"time":1604548451951}

    The content of the message.

    After you run the code, you can view the status of the consumer group in the server-side subscription.

    For more information, see Manage consumer groups.

    消费组状态

View logs

After all the settings are configured, log on to the IoT Platform console. In the left-side navigation pane, choose Maintenance > Device Log. Select Home_Thermostat from the product list. You can view the logs on the Cloud run log tab.

日志信息

What to do next

Send commands from IoT Platform to devices