需求概述
实现一个流程:EG8200mini采集Modbus RTU数据,通过MQTT协议连接华为云物联网平台
Modbus RTU采集此处不做过多赘述,可参考其他案例(串口读取Modbus传感器数据)介绍。下文默认已经采集到Modbus RTU数据。
要采集的Modbus点位表:
地址(Doc) | 数据类型 | 属性 | 名称 |
00001 | Boolean | 读写 | MotorControl1 |
00002 | Uint16 | 读写 | MotorControl2 |
40001 | Uint16 | 读写 | Requency1 |
40002 | Uint16 | 读写 | Requency2 |
MQTT通信相关参数及JSON数据格式要求,参考华为云设备属性上报_设备接入 IoTDA:
发布主题(Topic):$oc/devices/{device_id}/sys/properties/report
数据格式样例:
{
"services": [
{
"service_id": "Temperature", //服务ID
"properties": {
"value": 57, //属性名称
"value2": 60 //属性名称
}
}
]
}
需求分析
网关配置:
第一步:采集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
参考需求概述的MQTT数据格式以及平台配置第二步的物模型设置。将网关配置第一步采集的数据JSON格式化:
源码:
var data = msg.payload
msg.payload = JSON.stringify({
"services": [{
"service_id": "serverId",
"properties": {
"temMotorControl1p": data.MotorControl1,
"MotorControl2": data.MotorControl2,
"Requency1": data.Requency1,
"Requency2": data.Requency2
},
}
]
})
return msg
第三步:MQTT上报
使用MQTT发布
节点,参考平台配置第四步的内容填写MQTT连接参数:
设备属性上报Topic:$oc/devices/{device_id}/sys/properties/report
此处为语雀视频卡片,点击链接查看:ev_20240703_100557.mp4
平台配置
第一步:添加产品
进入华为云物联网平台
设备接入IoTDA->产品->创建产品:
产品名称:自定义
协议类型:MQTT
数据格式:JSON
设备类型选择:自定义类型
设备类型:自定义
其他默认:
此处为语雀视频卡片,点击链接查看:ev_20240702_155023.mp4
第二步:添加物模型
产品->EG网关对接演示->详情->自定义模型->添加服务->新增属性:
此处为语雀视频卡片,点击链接查看:ev_20240702_170221.mp4
第三步:添加设备
设备->所有设备->注册设备:
所属产品:选择第一步创建的产品
设备标识码:自定义填入
设备ID:自定义
设备名称:自定义
备注描述:自定义
此处为语雀视频卡片,点击链接查看:ev_20240702_173246.mp4
第四步:激活设备
设备->所有设备->详情:
MQTT连接参数:
将MQTT连接参数配置到MQTT发布节点
,详见网关配置第三步:
此时可以看到设备已经在线:
第五步:数据展示
设备->所有设备->详情->物模型数据:
网关触发数据发送,可以看到华为云物联网平台物模型数据更新:
总结
本章节介绍了一个相对复杂的流程,旨在表述网关所具备的能力,因此并未对程序的安全性、稳定性、灵活性等方面做优化。通过理解本流程,相信你已经对网关的可视化编程有了一定的熟悉。掌握它们将会为你制作更复杂的流程提供更多的选择和可能!
更多:成都纵横智控