All Products
Search
Document Center

IoT Platform:Send commands from IoT Platform to devices

Last Updated:Dec 11, 2023

After a device submits data to IoT Platform, you can send a command from IoT Platform to the device to test downstream messaging. This topic describes how to call the Pub operation to send a command from a cloud application to a thermostat device.

Prerequisites

The thermostat device is connected to IoT Platform. For more information, see Connect the device to IoT Platform and submit data.

Prepare a development environment

In this example, a sample program that calls a cloud API operation to send commands to a device is developed in amqp-demo project resides on the same business server as an Advanced Message Queuing Protocol (AMQP) client. For more information about environment configuration, see Prepare a development environment.

Procedure

Note

In this example, a custom topic is used to send the command. You can use a Thing Specification Language (TSL)-specific topic to send TSL data. For more information, see What is a TSL model?

  1. In the amqp-demo/pom.xml file, add the following dependencies to import cloud SDKs:

    • A Maven dependency that is used to import IoT Platform SDK for Java:

      <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-iot</artifactId>
          <version>7.41.0</version>
      </dependency>
    • A Maven dependency that is used to import Alibaba Cloud SDK for Java.

      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.6.3</version>
      </dependency>
  2. In the src/main/java/com.aliyun.iotx.demo directory, create a class file named PubDemo.java and copy the following sample code to the file.

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import com.aliyuncs.iot.model.v20180120.*;
    
    
    public class PubDemo {
    
         public static void main(String[] args) throws Exception {
    
             DefaultProfile profile = DefaultProfile.getProfile("${RegionId}", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    
             IAcsClient client = new DefaultAcsClient(profile);
    
    
             PubRequest request = new PubRequest();
             request.setIotInstanceId("iot-cn-******");
             request.setProductKey("a2******");
             request.setTopicFullName("/a2******/Device1/user/get");
             request.setMessageContent("eyJ0ZXN0IjoidGFzayBwdWIgYnJvYWRjYXN0In0=");
    
             try {
                 PubResponse response = client.getAcsResponse(request);
                 System.out.println(new Gson().toJson(response));
             } catch (ServerException e) {
                 e.printStackTrace();
             } catch (ClientException e) {
                 System.out.println("ErrCode:" + e.getErrCode());
                 System.out.println("ErrMsg:" + e.getErrMsg());
                 System.out.println("RequestId:" + e.getRequestId());
             }
        }
    }

    In actual scenarios, you must modify specific parameters in the preceding sample code. The following table describe the parameters:

    Parameter

    Example

    Description

    RegionId

    ap-northeast-1

    Replace the ${RegionId} variable in the DefaultProfile.getProfile file with the ID of the region where you activated IoT Platform. For more information about Alibaba Cloud region IDs, see Supported regions.

    TopicFullName

    /a2******/Device1/user/get

    The custom topic to which you want to publish the message.

    In this example, the custom topic /${productKey}/${deviceName}/user/get is used.

    Replace ${productKey} with the ProductKey of the thermostat device and ${deviceName} with Device1.

    For more information, see Use custom topics for communication.

    ProductKey

    a2******

    The ProductKey of the product to which the thermostat device belongs.

    IotInstanceId

    iot-cn-******

    The ID of the IoT_Test_Environment instance.

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

    MessageContent

    eyJ0ZXN0IjoidGFzayBwdWIgYnJvYWRjYXN0In0=

    The message body that you want to send. The message body is generated when you convert the original message into binary data and perform Base64 encoding on the data. In this example, the value of the MessageContent parameter is encoded from the “washingMode: 2, washingTime: 30” message content.

  3. If the following information is returned after you run the PubMessage.java file, the message is sent to the device.

    {"requestId":"6EF6****-****-5***-***7-D2********48","success":true,"messageId":"170440***********"}
  4. Go to the Instance Details page of the corresponding instance in the IoT Platform console. In the left-side navigation pane, choose Maintenance > Device Log > Cloud run log. On the Cloud run log tab, view the log that is related to the API call of the device.

    API调用日志

    You can also view the value of the MessageContent parameter on the device.

    [1695199345.900][LK-0309] pub: /a2******/Device1/user/get
    
    [LK-030A] < 77 61 73 68 69 6E 67 4D  6F 64 65 3A 20 32 2C 20 | washingMode: 2, 
    [LK-030A] < 77 61 73 68 69 6E 67 54  69 6D 65 3A 20 33 30    | washingTime: 30