2024年11月24日 08:39:32 来源:厦门星纵物联科技有限公司 >> 进入该公司展台 阅读量:33
目录
设备接入服务(IoTDA)是华为云的物联网平台,提供海量设备连接上云、设备和云端双向消息通信、批量设备管理、远程控制和监控、OTA升级、设备联动规则等能力,并可将设备数据灵活流转到华为云其他服务。本文档主要介绍如何将星纵物联LoRaWAN®网关通过MQTT方式对接到华为云IoTDA,并且通过MQTT主题将传感器数据发布到平台。
华为云IoTDA:
在左侧导航栏,选择“产品 ”,单击创建产品。产品名称、协议类型、数据格式等参照下图进行设置。
网关MQTT接入物联网平台需要提前获取MQTT接入信息(MQTT服务器地址/MQTT服务端口/客户端ID/用户名/密码)。
注意:8883为MQTTS的连接端口,若是MQTT连接端口请配置1883。
注意:若未连接成功,请先确认网关的网络状态及连接信息是否填写正确。网关上MQTT服务器端口请填写1883。
如下介绍网关如何通过发布Topic将传感器数据上报到华为云IoTDA。传感器以AM103为例。
设备消息上报(透传上报) | |
主题 | $oc/devices/{device_id}/sys/messages/up |
用途 | 设备无法按照产品模型中定义的属性格式进行数据上报时,可调用此接口将设备的自定义数据格式上报给平台,平台对该消息不做解析。其中{device_id}需修改为设备ID。 |
设备上报属性(物模型上报) | |
主题 | $oc/devices/{device_id}/sys/properties/report |
用途 | 设备按产品模型中定义的格式将属性数据上报给平台,其中{device_id}需修改为设备ID。 |
进入产品详情页,选择“模型定义”开发产品模型。单击“自定义模型”,配置产品的服务。
{
"services": [
{
"service_id": "BasicData", //服务ID
"properties": {
"battery": xx, //属性名称
"co2": xx,
"humidity": xx,
"temperature": xx
}
}
]
}
AM103物模型解码函数:
function Decode(fPort, bytes) {
var decoded = {
services: [
{
service_id: "BasicData",
properties: {}
}
]
};
for (var i = 0; i < bytes.length;) {
var channel_id = bytes[i++];
var channel_type = bytes[i++];
// BATTERY
if (channel_id === 0x01 && channel_type === 0x75) {
decoded.services[0].properties.battery = bytes[i];
i += 1;
}
// TEMPERATURE
else if (channel_id === 0x03 && channel_type === 0x67) {
decoded.services[0].properties.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;
i += 2;
}
// HUMIDITY
else if (channel_id === 0x04 && channel_type === 0x68) {
decoded.services[0].properties.humidity = bytes[i] / 2;
i += 1;
}
// PIR ACTIVITY
else if (channel_id === 0x05 && channel_type === 0x6A || channel_id === 0x0A && channel_type === 0x6A) {
decoded.services[0].properties.activity = readUInt16LE(bytes.slice(i, i + 2));
i += 2;
}
// LIGHT
else if (channel_id === 0x06 && channel_type === 0x65) {
decoded.services[0].properties.illumination = readUInt16LE(bytes.slice(i, i + 2));
decoded.services[0].properties.infrared_and_visible = readUInt16LE(bytes.slice(i + 2, i + 4));
decoded.services[0].properties.infrared = readUInt16LE(bytes.slice(i + 4, i + 6));
i += 6;
}
// CO2
else if (channel_id === 0x07 && channel_type === 0x7D) {
decoded.services[0].properties.co2 = readUInt16LE(bytes.slice(i, i + 2));
i += 2;
}
// TVOC
else if (channel_id === 0x08 && channel_type === 0x7D) {
decoded.services[0].properties.tvoc = readUInt16LE(bytes.slice(i, i + 2));
i += 2;
}
// PRESSURE
else if (channel_id === 0x09 && channel_type === 0x73) {
decoded.services[0].properties.pressure = readUInt16LE(bytes.slice(i, i + 2)) / 10;
i += 2;
} else {
break;
}
}
return decoded;
}
/* ******************************************
* bytes to number
********************************************/
function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0];
return value & 0xffff;
}
function readInt16LE(bytes) {
var ref = readUInt16LE(bytes);
return ref > 0x7fff ? ref - 0x10000 : ref;
}