Practice of IoT platform cloud general data analysis script

Data Analysis Script (Cloud Gateway) Practice

In the IoT scenario, for devices with low configuration, limited resources or requirements for network traffic, it is often not appropriate to construct JSON data on the device side to communicate with the IoT platform. At this time, the device can directly report the original data to the IoT platform through a customized topic. The IoT platform provides the data analysis function, which can convert the original equipment data into a structured JSON format in the cloud according to the script you submit, so as to facilitate the storage and display of the cloud business system.

1. Customized Topic Data Analysis

When the device publishes data through a custom topic and the topic carries a parsing mark (? _sn=default), the IoT platform, after receiving the data, calls the business data parsing script you submitted on the console to parse the original data reported by the device into JSON structures, and then flows the data.

Explain

• Currently, only East China 2 (Shanghai) region supports customized topic data analysis.

• When the device reports data, add a data resolution flag (? _sn=default) after the user-defined topic of the message is published.

For example, the original data sent by the device to Topic/${productKey}/${deviceName}/user/update needs to be parsed into JSON format. When the device reports, the topic is:/${productKey}/${deviceName}/user/update_ sn=default。

• When creating a custom topic on the Internet of Things platform, it is defined as a normal topic without adding a parsing tag.

• Only analyze the original data reported by the device to the cloud, not the cloud downlink data.

• Before and after parsing, the topic of the message remains unchanged.

For example, the device is sent to/${productKey}/${deviceName}/user/update_ The data with sn=default is still in the topic of/${productKey}/${deviceName}/user/update after parsing.

2. Script analysis practice cases

In this practice, we take the data collection of water pump operation as an example. The original data generated on the device side is a string: DATA, 235000, which is converted into a structured JSON format on the IoT platform cloud through the data script analysis engine: {"temperature": 23, "speed": 5000}.

3. Cloud development

3.1 Create product

Log in to the console and create the product: water pump

3.2 Add Custom Topic

Enter the product details, select Tab in the Topic class list, and add a custom communication Topic:/${productKey}/${deviceName}/user/data

3.3 Write Data Analysis Script

Write a script for data conversion according to the business situation, verify the correctness of the script through analog input, and then submit it to the Internet of Things platform

The complete script of this case is as follows:

/**

*Convert the device customized topic data to json format data, and call when the device reports the data to the Internet of Things platform

*Input parameter: topic string, topic of the reported message by the device

*Input parameter: rawData byte [] array cannot be empty

*

*Output parameter: jsonObj JSON object cannot be empty

*/

function transformPayload(topic, rawData) {

var jsonObj = {}

var dataSrc = String.fromCharCode. apply(String, rawData);

var data = dataSrc.split(',');

jsonObj. temperature = parseInt(data[1]);

jsonObj. speed = parseInt(data[2]);

return jsonObj;

}

3.4 Registered equipment

After the definition of the water pump product and the submission of the data analysis script, we will register a device to obtain the identity authentication triplet information

4. Device side development

We use Node.js script to simulate the water pump to report the operation data to the IoT Internet of Things platform. The complete code is as follows:

const mqtt = require('aliyun-iot-mqtt');

//Device Identity

const options = {

ProductKey: "Replace productKey",

DeviceName: "Replace deviceName",

DeviceSecret: "Replace deviceSecret",

regionId: "cn-shanghai"

};

//Added reported topics_ sn=default

const topic = `/${options.productKey}/${options.deviceName}/user/data?_ sn=default`;

//Device side data

var data = 'DATA,23,5000';

//1. Establish connection

const client = mqtt.getAliyunIotMqttClient(options);

console. log('topic= '+topic)

console. log('payload= '+data)

//2. Publish data to topic

client. publish(topic, data);

5. Online operation

Start the node of the water pump simulation on the computer After the js script, we can observe the upstream message analysis log in the console log service of IoT IoT platform, and the message content has been converted into structured JSON data in the cloud

In the tab of physical model data analysis, you can see the log of the parsing process.

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