This topic describes how to create a product whose data type is custom, add a device to the product, obtain certificate information of the device, create a Thing Specification Language (TSL) model, and edit a data parsing script in the IoT Platform console.

Create a product and add a device

In the IoT Platform console, create a product, add a device to the product, and obtain certificate information of the device. The certificate information includes the ProductKey, DeviceName, and DeviceSecret parameters.

  1. Log on to the IoT Platform console.
  2. In the left-side navigation pane, choose Devices > Products. On the Products page, 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 Cellular (2G/3G/4G/5G).
    Data Type Select Custom.
    Authentication Mode Select Device Secret.
  3. In the left-side navigation pane, click Devices and click Add Device to add a device to the product.
    After the device is added, go to the Devices page. Find the target device, click View to go to the Device Details page, and view the certificate information of the device. Specify the certificate information of the device in a data transmission unit (DTU) device

Create a TSL model

TSL is a data model that digitizes a physical entity and constructs the entity in the cloud. In IoT Platform, a TSL model refers to a set of features. After you define features for a product, IoT Platform generates a TSL model for the product. This topic uses a variable frequency motor as an example. You must define the spinning speed, electrical current, speed reset properties.

  1. Log on to the IoT Platform console. 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 Define Feature tab, and choose Edit Draft > Add Self-defined Feature.
  4. Add the following properties and click Release Online to publish the TSL model.
    Feature type Feature Identifier Data type Value range Unit Read/write type
    Property Spinning speed speed int32 0 to 3000 rpm Read-only
    Property Electrical current current int32 0 to 300 A Read-only
    Property Speed reset setspeed int32 0 to 3000 rpm Read/write

Edit a data parsing script

IoT Platform proceses data in the Alink JSON format. If serial devices communicate with IoT Platform by using DTU devices, IoT Platform cannot process pass-through data that is sent from serial devices. IoT Platform converts data by using data parsing scripts. Data parsing scripts allow you to convert upstream data into the Alink JSON format and downstream data into the custom format of your device. IoT Platform calls data parsing scripts that you submit to parse data. You must edit data parsing scripts based on upstream and downstream data.

  1. Log on to the IoT Platform console, go to the Product Details page, and click the Data Parsing tab.
  2. On the Data Parsing tab, enter a data parsing script in the Edit Script field.
    For more information about how to edit a data parsing script, visit Submit data parsing script.

    This topic uses a device that sends data to IoT Platform in the hexadecimal format as an example. You must use the data parsing script to convert upstream data of the hexadecimal format to data of the Alink JSON format and downstream data of the Alink JSON format to data of the hexadecimal format.

    The following code shows how to edit a data parsing script.

    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';
    /* * * * * *
    * Upstream data ->
    * 0102 // Two bytes in total * Result ->
    * {"method":"thing.event.TempError.post","id":"12345","params":{"Temperature": 2},"version":"1.1"}
    # Upstream data->
    * 0202 // Two bytes in total * Result ->
    * {"method":"thing.event.HumiError.post","id":"12345","params":{"Humidity":2}, "version":"1.1"}
    */
    /*The following function converts upstream data that is reported from serial devices into a TSL model of the Alink JSON format. */
    function rawDataToProtocol(bytes) {
        /*Convert upstream data into an array. Upstream data is stored in the bytes object. */
        var uint8Array = new Uint8Array(bytes.length);
        for (var i = 0; i < bytes.length; i++) {
            uint8Array[i] = bytes[i] & 0xff;
        }
    
        var params = {};                            // Create an object to store properties.
        var jsonMap = {};                           // Create an object that defines the format of an Alink message.
    
        /* Specify header fields for an Alink message. */
        jsonMap['version'] = ALINK_VERSION;         // The version of the Alink protocol.
        jsonMap['id'] = ALINK_ID;                   // The ID of the Alink message.
        jsonMap['method'] = ALINK_PROP_POST_METHOD; // The method that is used to report device properties.
        /*Specify properties for the Alink message. */
        params['speed'] = uint8Array[0];            // Convert the first byte that is received to the value of the spinning speed property.
        params['current'] = uint8Array[1];          // Convert the second byte that is received to the value of the electrical current property.
        jsonMap['params'] = params;                 //Pack the required parameters to a JSON array. 
    
        return jsonMap;                             // Return the results to IoT Platform.
    }
    
    // The following lists miscellaneous 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);
    }
    /*The following function converts downstream data to hexadecimal data that can be 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)    // If the Set Device Properties command is received from IoT Platform, run the following code.
        {
            var send_params = json['params'];
            var prop_cur = send_params['setspeed'];   // Specify the recevied value for the prop_cur parameter.
    
            // Concatenate raw data based on the custom format.
            payloadArray = payloadArray.concat(buffer_uint8(0x55)); // The first byte that indicates the header of a message.
            payloadArray = payloadArray.concat(buffer_uint8(prop_bool)); // The second byte that indicates the value of a property.
        }
    
        return payloadArray;    // Send values to the device.
    }
                            
  3. Run the script.
    • Test data reporting.
      1. Select Upstreamed Device Data in the Simulation Type field.
      2. On the Input Simulation tab, enter test data in the text box.

        The preceding script specifies the first byte as the spinning speed and the second byte as the electrical current. For example, if you specify 6410 as test data, the value 64 indicates that the spinning speed is 100 rpm and the value 10 indicates that the electrical current is 10 A.

      3. Click Run.

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

        Upstreamed Device Data
    • Test data receiving.
      1. Select Received Device Data in the Simulation Type field.
      2. On the Input Simulation tab, specify test data in the text box. Specify the following parameters:
        {
            "method": "thing.service.property.set",
            "id": "12345",
            "version": "1.0",
            "params": {
                "setspeed": 123
            }
        }
      3. Click Run.

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

        Received Device Data
  4. After you confirm that test data can be parsed by a script as expected, click Submit to submit the script to IoT Platform.
    Note IoT Platform cannot call scripts that are in the draft state. Before IoT Platform can call a script, you must submit the script.

What to do next

Configure a DTU device