Full link development practice from device end to cloud end business system

1、 Enterprise Internet of Things Platform Development Practice

By reading this article, you will learn the following skills:

1. The device establishes a two-way connection with the IoT enterprise instance you purchased on Alibaba Cloud through the MQTT protocol, reports the data collected by the device, and listens to the instructions issued by the cloud;

2. The reported data is stored in the specified database in real time through the rule engine configuration, without coding

3. The reported data is transferred to the business server in real time through the rule engine configuration. The AMQP protocol SDK is required

4. The business server calls the API of the IoT platform and sends control instructions to the device side

2、 Create Enterprise Instance

First, we log in to the IoT platform console( https://iot.console.aliyun.com ), click Purchase Instance to create an enterprise instance. You can also use public instances for free testing.

Then, on the purchase page, according to the actual business needs, select the region, instance type, number of devices, message uplink and downlink TPS, rule engine TPS and other parameters. Click Buy Now. After paying successfully, you can see that the enterprise instance is being created.

After a few minutes, the enterprise instance is created. Entering the enterprise instance, we can see the current specification parameters, equipment access point information, AMQP subscription access point information, and cloud API call access point information. As shown below:

3、 Create products and register devices

On the device management page of the enterprise instance, we need to first create a product home temperature controller. The data communication is in JSON format, and the authentication method is the device secret key.

On the function definition page of the product, we add two attributes: temperature and humidity. The details are as follows:

Finally, on the device management page, we register a physical device based on the home thermostat product and obtain the triple of device identity authentication. As shown below:

4、 Equipment access and report data

After obtaining the device identity triplet, you can access to the enterprise instance we subscribed through the MQTT protocol. The device side application logic is shown in the figure below:

After starting the simulation script, we can see that the equipment is online, and the latest reported temperature and humidity values can be seen in the physical model data.

In the log service of monitoring operation and maintenance, you can also see the log of data reported by the equipment. As shown below:

You can also use a device simulator to simulate device behavior without programming

5、 Data storage to database

First, we create a table storage instance IoTDataStore to create a data table iot_ Data, with deviceName and timestamp as primary keys. As shown below:

In the IoT enterprise instance, create a rule engine in the cloud product flow, write data processing SQL, and configure the flow destination as the database table created above.

Configure data source:

Configure data destination:

Create resolution rules and transfer the data flow to the table storage script editing interface:

When the device reports data, we can store the iot in the form_ You can see the stored data in the data table in real time. As shown below:

In the log service of the enterprise instance, we can view the complete flow logs to help us troubleshoot data link exceptions. As shown below:

6、 Business server receives data in real time

Some data in the IoT scenario needs to be processed by the business system in real time. At this time, we can subscribe to the AMQP mode on the server to receive the data reported by the device in real time.

First, we need to create a new consumption group in the server subscription of the enterprise instance to receive specific types of messages. As shown below:

Then, we create resolution rules in the cloud product flow to configure the flow destination as the service side subscription consumption group created above.

Finally, we write a program on the business server to establish an AMQP connection with the IoT enterprise instance using the AccessKey of the AliCloud account. The reference code is as follows:

Process the received business data in the AMQP callback. The reference code is as follows:

private static MessageListener messageListener = new MessageListener() {

@Override

public void onMessage(Message message) {
try {
//If you want to process the received message in a time-consuming way, please do it asynchronously to ensure that there is no time-consuming logic.
byte[] body = message.getBody(byte[].class);
String content = new String(body);
String topic = message.getStringProperty("topic");
String messageId = message.getStringProperty("messageId");
String tag = message.getStringProperty("tag");
logger. info("receive message"
+ ",n topic = " + topic
+ ",n messageId = " + messageId
+ ",n tag = " + tag
+ ",n content = " + content
+"n");
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
};

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us