配置LoRa网关后,您需要创建LoRa产品和设备,定义物模型,编写和提交LoRa设备的数据解析脚本。

创建产品和设备

在物联网平台注册LoRa产品和设备。

  1. 登录物联网平台控制台
  2. 实例概览页面,找到对应的实例,单击实例进入实例详情页面。
    注意 目前华东2(上海)、华北2(北京)、华南1(深圳)地域开通了企业版实例服务。其他地域,请跳过此步骤。
    实例概览
  3. 在左侧导航栏,选择设备管理 > 产品
  4. 产品页面,单击创建产品,创建一个连网方式为LoRaWAN的产品。
    表 1. 产品信息
    参数 说明
    产品名称 自定义产品名称。
    所属分类 选择自定义品类
    节点类型 选择直连设备
    连网方式 选择LoRaWAN
    说明 首次创建连网方式为LoRaWAN的产品,需单击立即授权,授权物联网平台访问物联网络管理平台。
    入网凭证 选择您在物联网络平台中创建并已授权的入网凭证。
    数据格式 选择为透传/自定义
  5. 在左侧导航栏,选择设备管理 > 设备
  6. 设备页面,单击添加设备,在刚创建的产品下添加设备。
    说明 设备的DevEUI和PIN Code,请在您的设备标签上查看。

定义物模型

本示例中,需定义以下属性:温度、湿度、二氧化碳浓度、挥发有机物、甲醛、光照强度和PM2.5。

  1. 在物联网平台控制台对应实例下的左侧导航栏,选择设备管理 > 产品
  2. 产品页面,找到之前创建的LoRa产品,单击对应的查看
  3. 产品详情页面的功能定义页签下,选择编辑草稿 > 添加自定义功能
  4. 在默认模块下,添加下图中的属性。添加完成后,单击发布上线,发布物模型。
    添加物模型具体操作,请参见单个添加物模型。您也可直接导入物模型JSON文件,具体操作,请参见批量添加物模型。物模型TSL文件内容,请参见附件:物模型TSL物模型属性

编写数据解析脚本

本示例中LoRa设备上报的数据是二进制格式。阿里云物联网平台的标准数据格式为Alink JSON格式,不能直接使用二进制数据进行业务处理。对此,物联网平台提供数据解析功能,您可以依据设备数据格式和物模型,编写数据解析脚本,提交至物联网平台,用于解析数据。

  1. 在物联网平台控制台对应实例上,LoRa产品的产品详情页面,选择数据解析页签。
  2. 数据解析页签下,选择脚本语言,然后在编辑脚本下的输入框中,输入数据解析脚本。
    数据解析脚本编写指导,请访问LoRaWAN设备数据解析

    本示例的数据解析脚本如下:

    var PROPERTY_REPORT_METHOD = 'thing.event.property.post';
    /*
    示例数据:
    传入参数:
        AA1FC800003710FF000503690B0018013500FFFFFFFFFFFFFFFFFFFFFFFFFF6C
    输出结果:
    {
      "method": "thing.event.property.post",
      "id": 1526870753,
      "params": {
        "temperature": 10,
        "humidity": 88
      },
      "version": "1.0"
    }
    */
    //上行数据,自定义二进制转Alink JSON格式。
    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();
    
            //属性上报method。
            jsonMap['method'] = PROPERTY_REPORT_METHOD;
            //协议版本号固定字段。
            jsonMap['version'] = '1.0';
            //标示该次请求ID值。
            jsonMap['id'] = new Date().getTime();
            var params = {};
            //12、13对应产品属性中PM2.5。
            params['pm25'] = (dataView.getUint8(13)*256+dataView.getUint8(12));
            //14、15对应产品属性中temperature。
            params['temperature'] = (dataView.getUint8(15)*256+dataView.getUint8(14))/10;
            //16、17对应产品属性中humidity。
            params['humidity'] = (dataView.getUint8(17)*256+dataView.getUint8(16));
            //18、19对应产品属性中co2。
            params['co2'] = (dataView.getUint8(19)*256+dataView.getUint8(18));
            //22、23对应产品属性中甲醛hcho。
            params['hcho'] = (dataView.getUint8(23)*256+dataView.getUint8(22))/100;
            //26、27对应产品属性中挥发性有机物voc。
            //params['voc'] = (dataView.getUint8(27)*256+dataView.getUint8(26))/100;
            //28、29对应产品属性中光照lightLux。
            params['lightLux'] = (dataView.getUint8(29)*256+dataView.getUint8(28));
    
            jsonMap['params'] = params;
    
        return jsonMap;
    }
    //下行指令,Alink JSON格式物模型数据转二进制格式。
    function protocolToRawData(json) {
        var payloadArray = [];
        // 追加LoRa下行帧头部。
        payloadArray = payloadArray.concat(0x5d);
        payloadArray = payloadArray.concat(0x0a);//厂商提供数据端口。
        payloadArray = payloadArray.concat(0x00);
        // 追加LoRa业务内容。
        return payloadArray;
    }
  3. 测试脚本。
    1. 选择模拟类型为设备上报数据
    2. 模拟输入下的输入框中,输入一个模拟数据,例如:AA1FC800003710FF000503690B0018013500FFFFFFFFFFFFFFFFFFFFFFFFFF6C
    3. 单击执行
      运行成功后,可在运行结果看到以下解析结果。
      {
        "method": "thing.event.property.post",
        "id": 1577359668030,
        "params": {
          "hcho": 655.35,
          "pm25": 11,
          "lightLux": 65535,
          "co2": 65535,
          "temperature": 28,
          "humidity": 53
        },
        "version": "1.0"
      }
  4. 确认脚本能正确解析数据后,单击提交,将脚本提交到物联网平台。
    说明 物联网平台不能调用草稿状态的脚本,只有已提交的脚本才会被调用来解析数据。

附件:物模型TSL

使用时,请替换productKey值为您实际产品的ProductKey值。

{
  "schema": "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
  "profile": {
    "version": "1.0",
    "productKey": "h0****fI"
  },
  "properties": [
    {
      "identifier": "temperature",
      "name": "温度",
      "accessMode": "rw",
      "desc": "温度",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "-20",
          "max": "70",
          "unit": "°C",
          "unitName": "摄氏度",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "humidity",
      "name": "湿度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "int",
        "specs": {
          "min": "0",
          "max": "100",
          "unit": "%",
          "unitName": "百分比",
          "step": "1"
        }
      }
    },
    {
      "identifier": "co2",
      "name": "二氧化碳",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "int",
        "specs": {
          "min": "0",
          "max": "10000",
          "unit": "mg/m³",
          "unitName": "毫克每立方米",
          "step": "1"
        }
      }
    },
    {
      "identifier": "voc",
      "name": "挥发性有机物",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "0",
          "max": "1000",
          "unit": "mg/m³",
          "unitName": "毫克每立方米",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "hcho",
      "name": "甲醛",
      "accessMode": "rw",
      "desc": "HCHOValue",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "0",
          "max": "5",
          "unit": "ppm",
          "unitName": "百万分率",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "pm25",
      "name": "pm25",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "int",
        "specs": {
          "min": "0",
          "max": "10000",
          "unit": "μg/m³",
          "unitName": "微克每立方米",
          "step": "1"
        }
      }
    },
    {
      "identifier": "lightLux",
      "name": "光照强度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "0",
          "max": "10000",
          "unit": "Lux",
          "unitName": "照度",
          "step": "0.1"
        }
      }
    }
  ],
  "events": [
    {
      "identifier": "post",
      "name": "post",
      "type": "info",
      "required": true,
      "desc": "属性上报",
      "method": "thing.event.property.post",
      "outputData": [
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "70",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "1"
            }
          }
        },
        {
          "identifier": "co2",
          "name": "二氧化碳",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "voc",
          "name": "挥发性有机物",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "hcho",
          "name": "甲醛",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "5",
              "unit": "ppm",
              "unitName": "百万分率",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "pm25",
          "name": "pm25",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "μg/m³",
              "unitName": "微克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "lightLux",
          "name": "光照强度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "Lux",
              "unitName": "照度",
              "step": "0.1"
            }
          }
        }
      ]
    }
  ],
  "services": [
    {
      "identifier": "set",
      "name": "set",
      "required": true,
      "callType": "async",
      "desc": "属性设置",
      "method": "thing.service.property.set",
      "inputData": [
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "70",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "1"
            }
          }
        },
        {
          "identifier": "co2",
          "name": "二氧化碳",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "voc",
          "name": "挥发性有机物",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "hcho",
          "name": "甲醛",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "5",
              "unit": "ppm",
              "unitName": "百万分率",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "pm25",
          "name": "pm25",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "μg/m³",
              "unitName": "微克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "lightLux",
          "name": "光照强度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "Lux",
              "unitName": "照度",
              "step": "0.1"
            }
          }
        }
      ],
      "outputData": []
    },
    {
      "identifier": "get",
      "name": "get",
      "required": true,
      "callType": "async",
      "desc": "属性获取",
      "method": "thing.service.property.get",
      "inputData": [
        "temperature",
        "humidity",
        "co2",
        "voc",
        "hcho",
        "pm25",
        "lightLux"
      ],
      "outputData": [
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "70",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "1"
            }
          }
        },
        {
          "identifier": "co2",
          "name": "二氧化碳",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "voc",
          "name": "挥发性有机物",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "mg/m³",
              "unitName": "毫克每立方米",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "hcho",
          "name": "甲醛",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "5",
              "unit": "ppm",
              "unitName": "百万分率",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "pm25",
          "name": "pm25",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "μg/m³",
              "unitName": "微克每立方米",
              "step": "1"
            }
          }
        },
        {
          "identifier": "lightLux",
          "name": "光照强度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "10000",
              "unit": "Lux",
              "unitName": "照度",
              "step": "0.1"
            }
          }
        }
      ]
    }
  ]
}

后续步骤

设备接入物联网平台