This article will specifically introduce how to quickly connect the stock serial output devices to the Alibaba cloud Internet of things platform through DTU devices that meet the access protocol specifications of Alibaba cloud Internet of things platform.
background information
In industrial, agricultural, medical, urban, buildings, parks and other scenarios, there are a large number of stock devices that communicate with the outside world through serial ports. When transforming such devices into the Internet of things, the serial port transmission protocol of the device itself can not be modified, and data analysis can only be carried out in the cloud. In order to quickly enable such devices to access and use the Alibaba cloud Internet of things platform, Alibaba cloud and its hardware partners jointly define transparent data DTU devices that can access the Internet of things platform through simple configuration.
Figure 1 Data flow chart
The local device is connected to the DTU device through the serial port. The DTU is connected to the Alibaba cloud Internet of things platform through 2G, 3G, 4G or Ethernet networks. The DTU device realizes the access protocol of the Alibaba cloud Internet of things platform. The equipment certificate will be configured into the DTU, which will conduct data communication with the Internet of things platform on behalf of the equipment.
Create products and devices
Create products and devices on the Internet of things platform and obtain device certificate information (ProductKey, DeviceName and DeviceSecret).
- Log in to the Internet of things platform console.
- Create a product.
- In the left navigation bar, select device management > products.
- On the product management page, click create product.
- Fill in the product information and click OK. Complete product creation.
The data of stock equipment is transmitted to the Internet of things platform through DTU equipment according to its own format, so it is necessary to create products with transparent / user-defined data format. Product information settings are shown in the following figure.
- Create a device.
- On the left navigation bar of the console, select device.
- On the device management page, click Add device.
- Select the product just created, enter the equipment name and remark name, and click OK. Complete device creation.
After the device is created successfully, the device certificate information will pop up. You can also click the view button corresponding to the device on the device management page to enter the device details page to view the device certificate information. The device certificate will be configured to the DTU device end.
Definition object model
Object model refers to digitizing an entity in physical space and building a data model of the entity in the cloud. In the IOT platform, defining the IOT model means defining product functions (including attributes, events and services). After the function definition is completed, the system will automatically generate the object model of the product. Taking the motor frequency conversion equipment as an example, three attributes of motor speed, current and set speed need to be created.
- In the left navigation bar of the Internet of things platform console, select device management > products.
- On the product management page, find the previously created product and click the corresponding view button.
- On the product details page, select function definition, and then click add function corresponding to custom function.
- Add attributes one by one according to the following table.
Function type Function name identifier data type Value range Company Read / write type attribute speed speed int32 0 ~ 3000 rpm read-only attribute electric current current int32 0 ~ 30 A read-only attribute Set speed setspeed Int32 0 ~ 3000 rpm Reading and writing
Edit data parsing script
The standard data format supported by Alibaba cloud's Internet of things platform is Alink JSON format, while the original data of stock equipment is transmitted to the Internet of things platform through DTU devices. The Internet of things platform cannot directly process such data. The Internet of things platform provides data parsing function, which can parse uplink user-defined format data into Alink JSON format; Parse the downlink data into the user-defined data format of the device. You need to submit the data analysis script on the Internet of things platform console for the Internet of things platform to call. The data analysis script shall be written according to the data reported by the device and the data distributed by the cloud.
- On the Internet of things platform console, select the data analysis tab on the product details page corresponding to the variable frequency motor product.
- In the edit script input box, enter the parsing script.
explain The identifier of the attribute in the script code must be consistent with that defined when defining the object model.
Click to view the data analysis script writing instructions.The data sent by the sample device to the cloud is in hexadecimal format, so the script needs to convert the hexadecimal format data format to Alink JSON format; And convert the Alink JSON format data distributed by the cloud into hexadecimal format. The sample script is as follows:
var ALINK_ID = "12345"; var ALINK_VERSION = "1.1"; var ALINK_PROP_POST_METHOD = 'thing.event.property.post'; // var ALINK_EVENT_TEMPERR_METHOD = 'thing.event.TempError.post'; // var ALINK_EVENT_HUMIERR_METHOD = 'thing.event.HumiError.post'; var ALINK_PROP_SET_METHOD = 'thing.service.property.set'; // var ALINK_SERVICE_THSET_METHOD = 'thing.service.SetTempHumiThreshold'; /* * * * * * * Pass in parameter - > * 0102 // Total 2 bytes * output result - > *{"method":"thing.event.TempError.post","id":"12345","params":{"Temperature": 2},"version":"1.1"} * Pass in parameter - > * 0202 // Total 2 bytes * output result - > *{"method":"thing.event.HumiError.post","id":"12345","params":{"Humidity":2}, "version":"1.1"} */ /*This function is used to convert the data sent from the device to the object model*/ function rawDataToProtocol(bytes) { /*Convert the RAW data reported by the device into an array, where the bytes object stores the RAW data reported by the device*/ var uint8Array = new Uint8Array(bytes.length); for (var i = 0; i < bytes.length; i++) { uint8Array[i] = bytes[i] & 0xff; } var params = {}; // Define attribute storage object var jsonMap = {}; // Define simulated Alink datagram objects /*Fill in the header of Alink data report agreement*/ jsonMap['version'] = ALINK_VERSION; // Alink protocol version number jsonMap['id'] = ALINK_ID; // Analog message ID jsonMap['method'] = ALINK_PROP_POST_METHOD;// Analog equipment uplink data method: equipment attribute reporting /*Fill in the attribute section of Alink datagram*/ params['speed']= uint8Array[0]; // Converts the first byte received to a speed value params['current'] = uint8Array[1]; // Converts the second byte received to current jsonMap['params'] = params; // Package parameters into data frames return jsonMap; // It will be sent to IoT device management platform upon return } //Here are some auxiliary functions function buffer_uint8(value) { var uint8Array = new Uint8Array(1); var dv = new DataView(uint8Array.buffer,0); dv.setUint8(0, value); return [].slice.call(uint8Array); } function buffer_int16(value) { var uint8Array = new Uint8Array(2); var dv = new DataView(uint8Array.buffer,0); dv.setInt16(0, value); return [].slice.call(uint8Array); } function buffer_int32(value) { var uint8Array = new Uint8Array(4); var dv = new DataView(uint8Array.buffer,0); dv.setInt32(0, value); return [].slice.call(uint8Array); } function buffer_float32(value) { var uint8Array = new Uint8Array(4); var dv = new DataView(uint8Array.buffer,0); dv.setFloat32(0, value); return [].slice.call(uint8Array); } /*This function converts the data sent from the cloud into a hexadecimal number recognized by the device*/ function protocolToRawData(json) { var method = json['method']; var id = json['id']; var version = json['version']; var payloadArray = []; if (method == ALINK_PROP_SET_METHOD) // Receive the command of "set device properties" from IoT device management platform { var send_params = json['params']; var prop_cur = send_params['setspeed']; // Extract the specific value of the setting //Splice rawdata in a custom protocol format payloadArray = payloadArray.concat(buffer_uint8(0x55)); // The first byte data header identifies the user-defined data function payloadArray = payloadArray.concat(buffer_uint8(prop_cur)); // The second byte is the specific setting value } return payloadArray; // When returning, send the data to the device. }
- Test script.
- Test uplink data analysis.
- Select the simulation type to report data for the equipment.
- In the input box under analog input, enter an analog data.
The logic of this example script is: the first byte of the data is the speed value, and the second byte is the current value. For example, 6410, 64 indicates that the speed is 100; 10 indicates a current of 16 amps.
- Click Run.
- In the run result bar on the right, view the analysis results.
- Test downlink data analysis.
- Select the analog type to accept data for the device.
- In the input box under analog input, enter analog downlink data. Examples of downlink data are as follows:
{ "method": "thing.service.property.set", "id": "12345", "version": "1.0", "params": { "setspeed": 123 } }
- Click Run.
- In the run result bar on the right, view the analysis results.
- Test uplink data analysis.
- After confirming that the script is available, click the submit button to submit the script to the Internet of things platform.
be careful The IOT platform cannot call scripts in draft status. Only submitted scripts will be called to parse data.
Device side development
In this example, a computer is used to simulate the DTU device end. The computer is connected with DTU through USB to serial port.
be careful Please ensure that the DTU can connect to the Internet correctly.
- Configure DTU devices.
This example uses a F2x16 DTU device.
- Connect the DTU device to the USB port of the computer.
- On the computer, open the DTU configuration tool, configure the correct serial port number, set the baud rate, and open the serial port.
- Click the login configuration button at the bottom right to enter the configuration state of DTU.
- Click the read configuration button to obtain the configuration of the existing DTU.
- Ensure that the work agreement is port. In the right configuration interface, click serial port, and then configure the local serial port. The configuration information is shown in the figure below.
- Click IoT access configuration and fill in the equipment certificate information and region obtained from the Internet of things platform.
- Click the distribute configuration button below to make the configuration effective.
If the configuration distribution fails, click exit to log in and reconfigure.
- After the configuration is completed, click the exit login button to make the DTU enter the normal working mode.
- Power off the DTU and then power on again. When the online light on DTU is on, it means that it is connected to the Internet of things platform.
You can also view the status of the device on the Internet of things platform console.
- Connect the DTU device to the USB port of the computer.
- Test data communication.
- Test upload data. Test DTU equipment to upload data to the Internet of things platform instead of stock equipment.
- Open the serial port debugging tool.
be careful On the local computer, please make sure that the DTU configuration tool has been closed before using the serial port debugging tool to simulate the sending and receiving of equipment data.
- Set the relevant parameters of the serial port debugging tool, open the serial port, and then click send.
According to the definition of the Internet of things model on the Internet of things platform, the two parameters of speed and current are simulated and sent to the cloud. Assuming that the speed is 150 and the current is 10 amps, fill in two hexadecimal numbers of 960A in sequence in the serial port tool.
- After the data is sent, turn on the real-time refresh switch in the IOT platform console, under the device details tab corresponding to the device, and under the running status tab. You can see the uploaded data later.
- Open the serial port debugging tool.
- Test and receive data from the cloud. Use the Internet of things platform to debug real equipment functions, issue speed setting instructions, and test DTU to receive data from the cloud.
- In the navigation bar on the left side of the Internet of things platform console, select monitoring operation and maintenance > online debugging.
- Select the equipment to be debugged, and then select the real equipment to be debugged.
- Select the function to set the attribute for the defined speed, select the method as setting, enter a test value, and click send command.
- After the command is sent successfully, view the received data in the receiving box of DTU serial port debugging tool. Among the received data, 55 is the data header and the data value is 64 (i.e. 100 in decimal).
Both the cloud and the device can receive correct data, indicating that the configuration is successful.
- Test upload data. Test DTU equipment to upload data to the Internet of things platform instead of stock equipment.
Noun interpretation
- Object model
Alibaba cloud's Internet of things platform abstracts devices into three elements: "attributes", "events" and "services". The three element abstract description of devices is called the thing model.
- attribute
It is generally used to describe the operation status of equipment, such as the speed of motor, the switch and brightness of lamp, and the temperature of water.
- event
Events during equipment operation generally include notification information that needs to be perceived and processed externally, such as on-off notification, alarm, etc.
- service
The ability of the device to be externally invoked, such as adjusting motor speed.
- Equipment certificate
The device certificate refers to ProductKey, DeviceName and DeviceSecret, which are the identification of the device certified by Alibaba cloud Internet of things platform. The device certificate information cannot be disclosed.
- ProductKey
The globally unique identifier issued by the Internet of things platform for the product.
- DeviceName
When registering a device, a user-defined or automatically generated device name is unique within the product dimension.
- DeviceSecret
The device key issued by the Internet of things platform for the device.