EG边缘计算网关连接中移ONENET物联网平台(MQTT协议)
上文:EG边缘计算网关连接阿里云物联网平台(MQTT协议)
需求概述
本章节主要实现一个流程:EG8200mini采集Modbus RTU数据,通过MQTT协议连接中移ONENET物联网平台
Modbus RTU采集此处不做过多赘述,可参考其他案例(串口读取Modbus传感器数据)介绍。下文默认已经采集到Modbus RTU数据。
要采集的Modbus点位表:
地址(Doc) | 数据类型 | 属性 | 名称 |
00001 | Boolean | 读写 | MotorControl1 |
00002 | Uint16 | 读写 | MotorControl2 |
40001 | Uint16 | 读写 | Requency1 |
40002 | Uint16 | 读写 | Requency2 |
需求分析
网关配置:
第一步:采集Modbus RTU数据
第二步:拼接Json
第三步:MQTT上报并接收平台响应
平台配置:
第一步:添加产品
第二步:添加物模型
第三步:添加设备
第四步:数据展示
需求实现
网关配置
第一步:采集Modbus从机的数据
此处为采集Modbus RTU数据,不在过多赘述,可参考其他案例。采集到Modbus RTU的流程如下:
此处为语雀内容卡片,点击链接查看:Modbus RTU 读数据 · IOTRouter
源码:
[{"id":"d968892da4e40a46","type":"inject","z":"b7c6f1e56286d94a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":1780,"wires":[["9b935c0dd6a63827","3bc2eb81af744a6d"]]},{"id":"9b935c0dd6a63827","type":"modbusRead","z":"b7c6f1e56286d94a","name":"读线圈","uid":"1","dataType":"1","len":"120","modbus":"f4b98c3be57ddcad","columns":[{"Key":"MotorControl1","Address":"0","Type":"23","Tdata":""},{"Key":"MotorControl2","Address":"1","Type":"23","Tdata":""}],"x":670,"y":1780,"wires":[["de1ce0dfa2e082f8"]]},{"id":"30fa1cad395c11c4","type":"modbusRead","z":"b7c6f1e56286d94a","name":"读频率","uid":"1","dataType":"3","len":"120","modbus":"f4b98c3be57ddcad","columns":[{"Key":"Requency1","Address":"0","Type":"1","Tdata":""},{"Key":"Requency2","Address":"1","Type":"1","Tdata":""}],"x":670,"y":1860,"wires":[["de1ce0dfa2e082f8"]]},{"id":"de1ce0dfa2e082f8","type":"join","z":"b7c6f1e56286d94a","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"0.5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":870,"y":1820,"wires":[["b53287da47908499"]]},{"id":"3bc2eb81af744a6d","type":"delay","z":"b7c6f1e56286d94a","name":"","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":470,"y":1860,"wires":[["30fa1cad395c11c4"]]},{"id":"b53287da47908499","type":"debug","z":"b7c6f1e56286d94a","name":"调试 254","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1040,"y":1820,"wires":[]},{"id":"f4b98c3be57ddcad","type":"modbus config","name":"","model":"2","host":"127.0.0.1","port":"502","serialport":"/dev/ttyAS3","baudrate":"115200","databits":"8","stopbits":"1","parity":"none","timeout":"50","reqtimeout":"1000"}]
第二步:拼接Json
参考ONENETonJson协议MQTT数据格式以及平台配置第二步的物模型设置。将网关配置第一步采集的数据JSON格式化:
上行Json示例:
{
"id": "123",
"version": "1.0",
"params": {
"Power": {
"value": "on",
"time": 1524448722123
},
"WF": {
"value": 23.6,
"time": 1524448722123
}
}
}
平台下行反馈Json示例:
{
"id": "123",
"code": 200,
"msg": "xxxx"
}
此处为语雀视频卡片,点击链接查看:onenet上报封装.mp4
源码:
var RS485_RECV = msg.payload
var time = Date.now()
var pid = "31ACZV4snl"
var deviceName = "device001"
var topic = `$sys/${pid}/${deviceName}/thing/property/post`
msg.topic = topic
var mqttSend = {
"id": "123",
"version": "1.0",
"params": {
"DO1": {
"value": RS485_RECV.MotorControl1 ? true : false,
"time": time
},
"DO2": {
"value": RS485_RECV.MotorControl2 ? true : false,
"time": time
},
"frequency1": {
"value": RS485_RECV.Requency1,
"time": time
},
"frequency2": {
"value": RS485_RECV.Requency2,
"time": time
}
}
}
msg.payload = JSON.stringify(mqttSend)
return msg;
第三步:MQTT上报
使用MQTT发布
节点,参考平台配置第四步的内容填写MQTT连接参数:
pid:产品ID
device-name:设备名称
设备属性上报Topic:$sys/{pid}/{device-name}/thing/property/post
上报响应订阅topic:$sys/{pid}/{device-name}/thing/property/post/reply
此处为语雀视频卡片,点击链接查看:onenet数据上报.mp4
平台配置
第一步:添加产品
进入ONENET物联网平台
产品开发->创建产品:
所属地址:自由选择
节点类型:网关设备
协议类型:MQTT
数据格式:oneJson
联网方式:自定义
开发方案:自定义方案
产品厂商:自定义
产品型号:自定义
品牌名称:自定义
此处为语雀视频卡片,点击链接查看:添加产品.mp4
第二步:添加物模型
产品开发->设置物模型->添加自定义模型:
此处为语雀视频卡片,点击链接查看:添加物模型.mp4
第三步:添加设备
设备接入管理->设备管理->添加设备:
所属产品:选择第一步创建的产品
设备名称:自定义
设备描述:自定义
此处为语雀视频卡片,点击链接查看:ev_20240702_173246.mp4
第四步:激活设备
参考文档MQTT设备连接:
MQTT连接参数:
连接地址:mqtts.heclouds.com
端口:1883
clientId:设备名称
username:产品ID
password:计算后的token(参考下一步说明)
Token计算参考文档Token计算:
res:products/31ACZV4snl (规则products/{产品ID})
et:1721859317 (失效时间)
key:awGbZ4HLsXkvaxOlixv/642tOZM3dXUzf+Xd6UnV098= (产品key)
根据上述步骤获取的连接参数:
地址:mqtts.heclouds.com
端口:1883
clientId:device001
username:31ACZV4snl
password:version=2018-10-31&res=products%2F31ACZV4snl&et=1721791062&method=md5&sign=iDH4WvVwIGlpRkFlBC1Eow%3D%3D
将MQTT连接参数配置到MQTT发布节点
:
此处为语雀视频卡片,点击链接查看:MQTT-onenet配置.mp4
此时可以看到设备已经在线:
第五步:数据展示
设备管理->详情->属性:
网关触发数据发送,可以看到ONENET物联网平台物模型数据更新:
总结
EG边缘计算网关与中移ONENET物联网平台的无缝对接,用户可以轻松实现对工业设备、传感器等现场设备的远程监控和管理。EG网关负责数据采集和协议转换,ONENET平台提供云端数据处理、分析与可视化,形成了一个高效、稳定的物联网系统。利用MQTT协议的轻量高效特点,确保设备数据能够及时、可靠地上传到云平台,实现工业设备的智能化和远程控制。
更多了解:成都纵横智控