Connect a LoRa device to IoT Platform

Updated at:
Copy as MD

After you configure a LoRa gateway, you need to create a Product and a Device in IoT Platform, define a Thing Model, and then submit a data parser script.

Create a product and a device

  1. Log on to the IoT Platform console.
  2. On the Overview page, find the instance that you want to manage and click the instance ID or instance name.

  3. In the left-side navigation pane, choose Device Management > Products.
  4. On the Products page, click Create Product to create a product that uses LoRaWAN as its network connection method.
    Parameter Description
    Product name Enter a custom name for the Product.
    Category Select Custom Category.
    Node type Select Directly Connected Device.
    Network connection method Select LoRaWAN.
    Join permission Select a join permission that you previously created and authorized.
    Data format Select Passthrough/Custom.
    Authentication method Select Device Secret.
  5. After the Product is created, in the Add Device section, click Add.
    You can find the DevEUI and PIN code for the device on its label.
  6. Test the device connection to IoT Platform.
    Follow the labels on your device to connect the antenna, GPS antenna, and a battery or power supply.
    About two minutes after you power on the device, its status changes to Online on the Device List tab of the Devices page in the IoT Platform console.

Define the Thing Model

A Thing Model is a digital representation of a physical entity, which defines the entity's data model in the cloud. In IoT Platform, defining a Thing Model means defining the product's features, which include properties, events, and services. After you define the features, the system automatically generates the Thing Model for the product. In this example, a weather monitoring device reports data such as temperature, humidity, air pressure, and geographic coordinates. Therefore, you must first define these data points as properties in IoT Platform.

  1. In the IoT Platform console, navigate to the left-side navigation pane of your instance and choose Device Management > Products.
  2. On the Products page, find the product that you created and click View in the Actions column.
  3. On the Product Details page, on the Feature Definition tab, choose Edit Draft > Add Custom Feature and add the following properties.
    Property Identifier Type Value range Step Unit Access mode
    Temperature Temperature double -99–100 0.01 ℃ Read/Write
    Humidity Humidity double 1–100 0.01 % Read/Write
    Atmospheric Pressure Atmosphere float 550–1060 0.01 hPa Read/Write
    Longitude Longitude double -180–180 0.01 ° Read/Write
    Latitude Latitude double -90–90 0.01 ° Read/Write
    Altitude Altitude float 0–9999 0.01 m Read/Write
    X-axis Acceleration Acceleration_X float -1000–1000 0.01 mg Read/Write
    Y-axis Acceleration Acceleration_Y float -1000–1000 0.01 mg Read/Write
    Z-axis Acceleration Acceleration_Z float -1000–1000 0.01 mg Read/Write
    Speed Speed float -10000–10000 0.01 km/h Read/Write
    Battery Voltage Battery_voltage float 0–100000 0.01 V Read/Write
    Gas Resistance Gas_resistance float -10000–10000 0.01 None Read/Write

    For detailed instructions on how to add a Thing Model, see Add a TSL model.

  4. Click Publish.

Write the data parser script

In this example, the LoRa device reports data in binary format, such as 01880537A5109D5A00846C. Bytes 1 and 2 are the data identifier 01 88. Bytes 3, 4, and 5 contain the latitude data (latitude:34.1925). Bytes 6, 7, and 8 contain the longitude data (longitude:108.8858). Bytes 9, 10, and 11 contain the altitude data (altitude:339m).

IoT Platform processes data in the Alink JSON format and cannot directly handle binary payloads from your device. You must write a data parser script to translate between the two formats. The script converts binary uplink data into Alink JSON, and converts Alink JSON downlink commands into binary. After you submit the script, IoT Platform uses it to parse all communication with the device.

  1. Log on to the IoT Platform console. On the Product Details page of your instance, select the Data Parsing tab.
  2. In the Edit Script editor, enter the parser script.
    Note The property identifiers in your script must exactly match the identifiers that you defined in the Thing Model.

    For detailed guidance on writing a data parser script, see Parse LoRaWAN device messages.

    The data parser script for this example is as follows:

    // var COMMAND_REPORT = 02;
    // var COMMAND_SET = 01;
    var ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'; // The standard Alink JSON topic for a device to upload property data to the cloud.
    var ALINK_PROP_SET_METHOD = 'thing.service.property.set';
    var ALINK_VERSION = "1.1";
    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 = {};
        // var fHead = uint8Array[0]; // The 0th byte is the reporting protocol.
        // if (fHead == COMMAND_REPORT)
        {
            jsonMap['method'] = ALINK_PROP_REPORT_METHOD; // Alink JSON format: property reporting.
            jsonMap['version'] = ALINK_VERSION; // Alink JSON format: a fixed field for the protocol version.
            jsonMap['id'] = '' + 12345; // Alink JSON format: the request ID.
            var params = {};
            switch (dataView.getInt16(0)) {
            case 0x0267:
                params['Temperature'] = Math.floor(dataView.getInt16(2) * 0.1 * 10) / 10; // Truncate to one decimal place.
                params['Humidity'] = Math.floor(100 * dataView.getUint8(6) * 0.01 / 2 * 10) / 10;
                params['Atmosphere'] = Math.floor(dataView.getInt16(9) * 0.1 * 10) / 10;
                break;
            case 0x0188:
                var buffer = new Uint8Array(4);
                buffer[0] = 0;
                buffer[1] = uint8Array[2];
                buffer[2] = uint8Array[3];
                buffer[3] = uint8Array[4];
                var latitude = new DataView(buffer.buffer, 0);
                params['Latitude'] = Math.floor(latitude.getInt32(0) * 0.0001 * 10000) / 10000;
                buffer[0] = 0;
                buffer[1] = uint8Array[5];
                buffer[2] = uint8Array[6];
                buffer[3] = uint8Array[7];
                var longitude = new DataView(buffer.buffer, 0);
                params['Longitude'] = Math.floor(longitude.getInt32(0) * 0.0001 * 10000) / 10000;
                buffer[0] = 0;
                buffer[1] = uint8Array[8];
                buffer[2] = uint8Array[9];
                buffer[3] = uint8Array[10];
                var altitude = new DataView(buffer.buffer, 0);
                params['Altitude'] = Math.floor(altitude.getInt32(0) * 0.01 * 100) / 100;
                break;
            case 0x0371:
                params['Acceleration_X'] = dataView.getInt16(2);
                params['Acceleration_Y'] = dataView.getInt16(4);
                params['Acceleration_Z'] = dataView.getInt16(6);
                break;
            case 0x0702:
                params['Battery_voltage'] = dataView.getInt16(2)/10;
                params['Speed'] = Math.floor(dataView.getInt16(6) * 0.01 * 100) / 100;
                break;
            case 0x0902:
                params['Gas_resistance'] = dataView.getInt16(2);
                break;
            }
            jsonMap['params'] = params; // Alink JSON format: the standard params field.
        }
        return jsonMap;
    }
    function protocolToRawData(bytes) {
        var method = json['method'];
        var id = json['id'];
        var version = json['version'];
        var payloadArray = [];
        return payloadArray;
    }
                            
  3. Test the script.
    1. For the simulation type, select Device-Reported Data.
    2. In the input field under Simulate Input, enter a sample payload: 01880537A5109D5A00846C.
    3. Click Run.
    The Result section shows the parsed JSON. The output includes a method (thing.event.property.post), an id (12345), a params object containing Latitude (34.1925), Longitude (108.8858), and Altitude (339), and a version (1.1).
  4. After you confirm that the script correctly parses the data, click Submit to upload the script to IoT Platform.
    Note IoT Platform uses only submitted scripts to parse data; it does not run scripts that are in draft mode.
    After the script successfully parses data from the device, you can view it on the TSL Data > Real-time tab of the Device Details page.