Server-side development
The AccessKey ID and AccessKey secret of the Alibaba Cloud account or the RAM user.
Log on to the IoT Platform console. Move the pointer over your profile picture and click AccessKey Management to obtain the AccessKey ID and AccessKey secret.
You can develop your business server to receive device data and send control instructions. This topic uses a Java script to demonstrate how to receive device data and send control instructions.
Prepare the development environment
This topic uses the following Java development environment:
Operating system: Windows 10 (64-bit)
JDK version: JDK 8
Integrated development environment (IDE): IntelliJ IDEA Community Edition
Receive device data on the business server
The server receives messages through an AMQP client. You can configure the AMQP client to connect to IoT Platform and listen for device messages. For more information, see AMQP client connection guide and Java SDK connection example.
This example uses the Java development language and the Apache Qpid JMS client. For instructions on how to use the Qpid JMS client, see the Qpid JMS 0.57.0 documentation.
Follow these steps to develop the AMQP client.
Download the demo code package and unzip it.
Open IntelliJ IDEA and import the amqp-demo project from the demo package.
The pom.xml file already contains the Maven dependency to download the Qpid JMS client.
In the AmqpClient.java file in the src/main/java/com.aliyun.iotx.demo folder, modify the parameter values in the code for connecting the JMS client to IoT Platform. The following table describes the parameters.
private final static Logger logger = LoggerFactory.getLogger(AmqpClient.class); private static String accessKey = "${YourAccessKey}"; private static String accessSecret = "${YourAccessSecret}"; private static String consumerGroupId = "${YourConsumerGroupId}"; // iotInstanceId: The instance ID. private static String iotInstanceId = "${YourIotInstanceId}"; // The clientId parameter is displayed in the Client ID column on the consumer group status page for the server-side subscription in the console. // Use a unique identifier, such as a machine UUID, MAC address, or IP address, as the clientId. This helps you distinguish between different clients. private static String clientId = "${YourClientId}"; // ${YourHost} is the connection domain name. For more information, see the AMQP client connection guide. private static String host = "${YourHost}"; // Specify the number of connections to start for a single process. // A single connection has a limited consumption rate. For more information, see Limits. The maximum is 128 connections. // The number of connections is related to the consumption rate and rebalancing. Add one connection for every 500 QPS. private static int connectionCount = 4;Parameter
Example
Description
accessKey
LTAI****
The AccessKey ID and AccessKey secret of the Alibaba Cloud account or the RAM user.
Log on to the IoT Platform console. Move the pointer over your profile picture and click AccessKey Management to obtain the AccessKey ID and AccessKey secret.
accessSecret
********
consumerGroupId
VWhGZ2QnP7kxWpeSSjt******
The ID of the created consumer group for power bank business data processing. For more information, see Step 6 in Configure cloud development.
iotInstanceId
"iot-cn-6ja***"
The ID of the instance to which the device belongs.
You can view it on the Instance Details page in the console.
If an ID exists, you must enter this ID.
If no ID exists, enter an empty value, such as
iotInstanceId = "".
clientId
12345
The client ID. You can customize this ID. The length cannot exceed 64 characters. Set it to a unique identifier, such as the UUID, MAC address, or IP address of the server where your AMQP client is located. This helps you identify and distinguish between different clients.
After the AMQP client is connected, this parameter is displayed on the details page of the power bank business data processing consumer group in the console.
connectionCount
4
The number of connections for the AMQP client. The maximum value is 128. This parameter is used to scale out real-time message pushes.
On the Consumer Group Details page, connected clients are displayed in the
${clientId}+"-"+numberformat. The minimum value for number is 0.host
iot-***.amqp.iothub.aliyuncs.com
The AMQP connection domain name. For more information, see View and configure instance endpoint information (Endpoint).
Send control instructions from the business server
The server calls the Pub API to send a control instruction, such as CMD,82923,ad322.
Follow these steps to complete the server-side development. For more information about parameter settings, see Java SDK instructions and Pub.
In the pom.xml file of the amqp-demo project, add the Maven dependency. Then, click the Load Maven Changes icon to download the dependency package.
<!-- 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> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.6</version> </dependency>In the src/main/java/com.aliyun.iotx.demo path of the amqp-demo project, create a Java class and add the code. For example, create a file named PubClient.java with the following content.
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 java.util.*; import com.aliyuncs.iot.model.v20180120.*; public class PubClient { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("${RegionId}", "${accessKey}", "${accessSecret}"); IAcsClient client = new DefaultAcsClient(profile); PubRequest request = new PubRequest(); request.setIotInstanceId("${iotInstanceId}"); request.setProductKey("${productKey}"); request.setQos(0); request.setTopicFullName("/${productKey}/${deviceName}/user/cmd/down"); request.setMessageContent(Base64.getEncoder().encodeToString("CMD,82923,ad322".getBytes())); 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()); } } }Set the parameters. Replace the placeholder values with your actual values.
Parameter
Description
${accessKey}
Your account's AccessKey ID and AccessKey secret.
You can create or view your AccessKey in AccessKey Management in the Alibaba Cloud console.
${accessSecret}
${RegionId}
The region code for your IoT Platform service.
You can view the current service region in the upper-left corner of the IoT Platform console.
For information about region codes, see Region list.
${iotInstanceId}
The ID of the instance to which your device belongs. You can view the ID of the current instance on the Instance Details page in the IoT Platform console.
If an ID exists, you must enter this ID.
If no ID exists, you can enter an empty value.
${productKey}
The device certificate information that you saved after you added the device.
You can view it on the Device Details page in the IoT Platform console.
${deviceName}