汉枫Wi-Fi串口服务器HF2211S应用案例
要实现的功能
使用HF2211S模块通过WiFi模块连接到厂区网络,以mobusRTU协议,主动读取气体检测仪的多路传感器数据,并通过MQTT协议将读取到的数据以JSON格式推送到指定服务器。
具体细分为如下几个小过程:
- 模块上电后主动连接到指定WiFi
- 模块连接到指MQTT服务器
- 模块通过modbusRTU协议主动读取设备数据
- 模块主动将读取到的数据推送到mqtt服务器
配置环境
- PC系统(Windows 10 专业版 21H1)
- WIFI AP(使用手机共享2.4G热点)
- USB转485通讯模块
配置软件
- Advanced_IP_Scanner
- IOTServiceSetupX64_3.0.03
- modbusPLC Simulator
- mosquitto-2.0.11
Advanced_IP_Scanner
局域网设备扫描软件,在该处用于获取局域网内HF2211S从AP中获取到的IP地址。
IOTServiceSetupX64_3.0.03
用于发现并配置HF2211S
modbusPLC Simulator
用于模拟气体探测仪,可以查看从模块发来的报文,便于调试modbusRTU主站。
mosquitto-2.0.11
MQTT服务端软件,HF2211S将485口读取到的数据不做解析,转发到MQTT服务器。
配置截图
安装前文中提到的配置软件
电脑连接模块自带的WiFi
打开配置软件,会自动显示搜索到的局域网内的模块,点击设备编辑,进入设备配置界面
主要设置如下参数
- WIFI模式 STA
STA无线名称、密码
- 连接协议相关信息
协议:MQTT、订阅主题、发布主题、服务器端地址、端口号
- 串口信息
波特率、数据位、停止位、校验位
基本参数设置设置完成后,点击高级设置,再点击编辑脚本,导入串口脚本,实现模块主动读取设备数据及数据上送功能。
将如下脚本存储为txt文件,并导入脚本
key=""
cmd01=[0x05,0x03,0x01,0x34,0x00,0x37,0x45,0xAA]
cmd02=[0x05,0x03,0x02,0x34,0x00,0x37,0x45,0xEE]
cmd03=[0x05,0x03,0x03,0x34,0x00,0x37,0x44,0x12]
cmd04=[0x05,0x03,0x04,0x34,0x00,0x37,0x45,0x66]
num=0
TIMER HeartBeat 500
num=num+1
IF(num==1)
SEND(UART,uart0,cmd01)
key="CH1"
END
IF(num==2)
SEND(UART,uart0,cmd02)
key="CH2"
END
IF(num==3)
SEND(UART,uart0,cmd03)
key="CH3"
END
IF(num==4)
SEND(UART,uart0,cmd04)
key="CH4"
num=0
END
END
RECV UART uart0
DATA=INPUT
DATA_S=DATA.hexString()
DATA_B="{\"key\":\""+key+"\",\"value\":\""+DATA_S+"\"}"
SEND(SOCK,netp,DATA_B)
END
上述代码部分说明
cmd01=[0x05,0x03,0x01,0x33,0x00,0x37,0xF4,0x6B] //需要通过串口发送的命令1
ER HeartBeat 500 //500毫秒执行一次命令
设备状态查看
串口报文捕获
05 03 01 33 00 37 F46B
05036E000003E800000000000000000000000000000000000000000000000000004248000043480000000000000000000000000000AC5C00170000000000000000000000000000000000000000B08800170000000038DD0028FFFF4398000000000000000000000000000000000006000120B7
05 03 02 33 00 37 F42F
05036E0000001E00000000000000000000000000000000000000000000000000004190000041B80000000000000000000000000000E5A20008333341A7000000000000000000000000000200009C75000000000000E84B0008333341A7000000000000000000000000000000000006000184D1
05 03 03 33 00 37 F5D3
05036E00000064000000000000000000000000000000000000000000000000FFFF419F0000424800000000000000000000000000008D8300170000000000000000000000000000000000020000899E001700000000F18300350000424800000000000000000000000000000000000600016049
05 03 04 33 00 37 F4A7
05036E000003E800000000000000000000000000000000000000000000000000004248000043480000000000000000000000000000AC3600170000000000000000000000000000000000000000B08800170000000038DD0028FFFF4398000000000000000000000000000000000006000100C1
MQTT报文捕获
{"key":"CH1","value":"05036E0064000000000000000000000000000000000000000000000000000041A000004248000000000000000000000000000090DA001F000000000000000000000000000000000001000056F0001F00000000E8D5001C000042200000000000000000000000000000000000060001002190E4"}
{"key":"CH2","value":"05036E001E00000000000000000000000000000000000000000000000000004190000041B80000000000000000000000000000E65E0008333341A7000000000000000000000000000200009C75000000000000E84B0008333341A700000000000000000000000000000000000600010082AE92"}
{"key":"CH3","value":"05036E0064000000000000000000000000000000000000000000000000FFFF419F00004248000000000000000000000000000088CA00170000000000000000000000000000000000020000899E001700000000F183003500004248000000000000000000000000000000000006000100242A29"}
{"key":"CH4","value":"05036E03E800000000000000000000000000000000000000000000000000004248000043480000000000000000000000000000B18100170000000000000000000000000000000000000000B08800170000000038DD0028FFFF43980000000000000000000000000000000000060001001E2210"}
附:气体检测仪地址表
{"key":"CH2","value":"05036E
001E00000000000000000000000000000000000000000000000000004190000041B80000000000000000000000000000E65E0008333341A7000000000000000000000000000200009C75000000000000E84B0008333341A700000000000000000000000000000000000600010082 AE92"}
MQTT报文中,value值中,头部 3个字节 05036E 尾部2个字节CRC校验 AE92去除,中间部分为对应通道的数据。
MQTT报文中,第二通道氧气实时浓度对应的报文为 3333 41A7,对应浮点数: 20.899999618530273
通讯 | Modbus RTU 9600 n 8 1 | 5 |
---|---|---|
Point | VirRegAddr | ASDU |
可燃气体(通道1) | 1 | |
量程 | 40308 | 2个字节,Byte0:量程高位Byte1:量程低位 |
低报警点(float) | 40319 float | |
A1报警点(float) | 40321 float | |
A2报警点(float) | 40323 float | |
实时浓度(float) | 40334 float | |
报警状态 | 40336 | 2个字节 Byte0:0x00 |
Byte0: 0x00 正常 | ||
0x01 断线 | ||
0x02 超量程 | ||
0x03 A2 | ||
0x04 A1 | ||
存储模式 | 40361 | 2个字节, Byte0:保留0x00; |
Byte1=0x00;关闭 | ||
Byte1=0x01 自动 | ||
Byte1=0x02 手动 | ||
Byte1=0x03 单点 | ||
存储周期 | 40362 | 2个字节,Byte0:高位 Byte1:低位 |
氧气(通道2) | 2 | |
量程 | 40564 | 50302 |
低报警点(float) | 40575 float | |
A1报警点(float) | 40577 float | |
A2报警点(float) | 40579 float | |
实时浓度(float) | 40590 float | |
报警状态 | 40592 | |
存储模式 | 40617 | |
存储周期 | 40618 | |
硫化氢(通道3) | 3 | |
量程 | 40820 | 50303 |
低报警点(float) | 40831 float | |
A1报警点(float) | 40833 float | |
A2报警点(float) | 40835 float | |
实时浓度(float) | 40846 float | |
报警状态 | 40848 | |
存储模式 | 40873 | |
存储周期 | 40874 | |
一氧化碳(通道4) | 4 | |
量程 | 41076 | 50304 |
低报警点(float) | 41087 float | |
A1报警点(float) | 41089 float | |
A2报警点(float) | 41091 float | |
实时浓度(float) | 41102 float | |
报警状态 | 41104 | |
存储模式 | 41129 | |
存储周期 | 41130 | |
实时时间(年分) | 40023 | 2个字节, Byte0:年份高位 Byte1:年份低位 |
实时时间(月,日) | 40024 | 2个字节, Byte0:月 Byte1:日 |
实时时间(时,分) | 40025 | 2个字节, Byte0:时 Byte1:分 |
实时时间(秒) | 40026 | 2个字节, Byte0: 0x00 Byte1: 秒 |
仪器地址 | 40027 | 2个字节 Byte0:保留0x00 Byte1:仪器地址 |
仪器报警开关 | 40029 | 2个字节 Byte0:声音报警开关(0x0关,0x01开)Byte1: 光电报警开关(0x00关,0x01开) |
恢复出厂设置 | 40039 | 2个字节Byte0:保留0x00当往Byte1中写入0x01时则进行恢复出厂设置 |