Raw data from some devices that have limited resources and low specifications cannot be converted to Thing Specification Language (TSL) models in the JSON format. Therefore, these devices cannot communicate with IoT Platform. To fix the issue, you can pass through raw data from devices to IoT Platform. IoT Platform converts raw device data to Alink JSON data by using data parsing scripts that you submit. Then, you can perform subsequent operations on the converted data.

Background information

The following figure shows how data flows between devices and enterprise servers.

data flow

Connect a device to IoT Platform

This article uses a device that collects environmental data as an example.

  1. Log on to the IoT Platform console.
  2. In the left-side navigation pane, choose Devices > Products. On the page that appears, click Create Product to create a product.
    Table 1. Product information
    Parameter Description
    Product Name The name of the product.
    [DO NOT TRANSLATE] [DO NOT TRANSLATE]
    Node Type Select Directly Connected Device.
    Network Connection Method Select Wi-Fi.
    Data Type Select Custom.
    Authentication Mode Select Device Secret.
  3. After the product is created, click Create TSL.
  4. Choose Edit Draft > Add Self-defined Feature to add the following properties.
    Properties
  5. Click Release Online to publish the TSL model.
  6. In the left-side navigation pane, select Devices and click Add Device to add a device to the product.
    After the device is added, obtain certficate information of the device. The certificate includes the ProductKey, DeviceName, and DeviceSecret parameters.
  7. Provision a device and perform a test
    This article uses a device SDK to provision a device as an example. For more information about how to provision a device, see the Link SDK reference.

    After the device is provisioned, create a TSL model for the device, and then run a test to implement data reporting.

    After you connect the device to IoT Platform, the device is in the Online state. You can view the status on the Devices page.

    Choose Maintenance > Device Log > Cloud run log, search for the device, and view the logs of the Device-to-cloud Messages. Click the MessageID of a message to view the details of the message that is reported from the device.

    The contents of the message are displayed in the hexadecimal notation as follows: 0xaa1fc800003710ff0005d76b15001c013400ad04ffff0400ffff18003000ff2e.

Create a data parsing script

In the IoT Platform console, edit and submit a data parsing script, and test data parsing.

  1. In the left-side navigation pane, choose Devices > Products.
  2. On the Product page, find the target product and click View.
  3. On the Product Details page, click the Data Parsing tab.
  4. On the Data Parsing tab, write a data parsing script in the Edit Script section.

    Edit the data parsing script based on the communication protocol for the device that is used by the device. The following figure shows the packet format of the communication protocol.

    Byte Description Note
    12 Low byte of a PM2.5 value Return value: PM2.5. Range: 0~999ug/m3.
    13 High byte of a PM2.5 value
    14 Low byte of a temperature value*10 Return value: temperature. Range: -10°C~50°C.
    15 High byte of a temperature value*10
    16 Low byte of a humidity value Return value: humidity. Range: 0~99%.
    17 High byte of a humidity value
    18 Low byte of a percentage of carbon dioxide Return value: percentage of carbon dioxide.
    19 High byte of a percentage of carbon dioxide
    22 Low byte of a percentage of formaldehyde*100 Return value: percentage of formaldehyde. Range: 0~9.99.
    23 High byte of a percentage of formaldehyde*100
    28 Low byte of an illuminance value Return value: illuminance. Unit: lux.
    29 High byte of an illuminance value

    The device can only report data. Therefore, you must implement the rawDataToProtocol data parsing function for upstream data rather than implementing the protocolToRawData function.

    The following code shows how to parse data.

    var PROPERTY_REPORT_METHOD = 'thing.event.property.post';
    
    //Upstream data. The following function converts data of a custom format to a TSL model in the JSON format.
    function rawDataToProtocol(bytes) {
    
        var uint8Array = new Uint8Array(bytes.length);
        for (var i = 0; i < bytes.length; i++) {
            uint8Array[i] = bytes[i] & 0xff;
        }
    
        var dataView = new DataView(uint8Array.buffer, 0);
    
        var jsonMap = new Object();
    
            //Specify the request method.
            jsonMap['method'] = ALINK_PROP_REPORT_METHOD
            //Specify the protocol version. Set the version to 1.0. Do not change the value.
            jsonMap['version'] = '1.0'
            //Specify the ID of the request.
            jsonMap['id'] = new Date().getTime();
            var params = {};
            //The 12 and 13 values are the values of the PM2.5 property.
            params['pm25'] = (dataView.getUint8(13)*256+dataView.getUint8(12));
            //The 14 and 15 values are the values of the temperature property.
            params['temperature'] = (dataView.getUint8(15)*256+dataView.getUint8(14))/10;
            //The 16 and 17 values are the values of the humidity property.
            params['humidity'] = (dataView.getUint8(17)*256+dataView.getUint8(16));
            //The 18 and 19 values are the values of the co2 property.
            params['co2'] = (dataView.getUint8(19)*256+dataView.getUint8(18));
            //The 22 and 23 values are the values of the hcho property.
            params['hcho'] = (dataView.getUint8(23)*256+dataView.getUint8(22))/100;
            //The 28 and 29 values are the values of the lightLux property.
            params['lightLux'] = (dataView.getUint8(29)*256+dataView.getUint8(28));
    
            jsonMap['params']  = params;
    
        return jsonMap;
    }
    //Downstream commands. The following function converts a TSL model in the JSON format to data of a custom format.
    function protocolToRawData(json) {
        var payloadArray = [1];//The device can only report data. The device cannot receive commands from IoT Platform. 
        return payloadArray;
    }
    
    //Transform data carried in custom topics to json data.
    function transformPayload(topic, rawData) {
        var jsonObj = {}
        return jsonObj;
    }
  5. Test data parsing.
    1. Select Upstreamed Device Data in the Simulation Type field.
    2. In the text box on the Input Simulation tab, enter test data.
      After the test data is applied, you can view the following device data in the hexadecimal format on the Device Log page: 0xaa1fc800003710ff0005d76b15001c013400ad04ffff0400ffff18003000ff2e.
    3. Click Run.

      The following figure shows the results on the Parsing Results tab.

      Parsing Results
  6. Make sure that data is parsed by the script as expected. Click Submit to submit the script to IoT Platform.

After you submit the script, IoT Platform uses the script to parse subsequent upstream data. After upstream data is parsed, the data is displayed on the Status tab of the Device Details page.

Status