要實現的功能
使用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,0x33,0x00,0x37,0xF4,0x6B]
cmd02=[0x05,0x03,0x02,0x33,0x00,0x37,0xF4,0x2F]
cmd03=[0x05,0x03,0x03,0x33,0x00,0x37,0xF5,0xD3]
cmd04=[0x05,0x03,0x04,0x33,0x00,0x37,0xF4,0xA7]
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":"05036E000003E800000000000000000000000000000000000000000000000000004248000043480000000000000000000000000000AC5C00170000000000000000000000000000000000000000B08800170000000038DD0028FFFF4398000000000000000000000000000000000006000120B7"}
{"key":"CH2","value":"05036E0000001E00000000000000000000000000000000000000000000000000004190000041B80000000000000000000000000000E5A20008333341A7000000000000000000000000000200009C75000000000000E84B0008333341A7000000000000000000000000000000000006000184D1"}
{"key":"CH3","value":"05036E00000064000000000000000000000000000000000000000000000000FFFF419F0000424800000000000000000000000000008D8300170000000000000000000000000000000000020000899E001700000000F18300350000424800000000000000000000000000000000000600016049"}
{"key":"CH4","value":"05036E000003E800000000000000000000000000000000000000000000000000004248000043480000000000000000000000000000AC3600170000000000000000000000000000000000000000B08800170000000038DD0028FFFF4398000000000000000000000000000000000006000100C1"}
附:氣體檢測儀地址表
{"key":"CH2","value":"05036E 0000001E00000000000000000000000000000000000000000000000000004190000041B80000000000000000000000000000E5A20008333341A7000000000000000000000000000200009C75000000000000E84B0008333341A70000000000000000000000000000000000060001 84D1"}
MQTT報文中,value值中,頭部 3個位元組 05036E 尾部2個位元組CRC校驗 84D1去除,中間部分為對應通道的資料。
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時則進行恢復出廠設定 |