非動態(tài)連接的“MQTT IN”不支持輸入,需要完整配置連接參數(shù)以及訂閱地址;
配置連接MQTT服務(wù)器認證參數(shù);
復制下段JSON文本到Node-RED導入窗口,防止參數(shù)輸入錯誤導致連接失?。?/span>
[{"id":"9a5655d35c064b4a","type":"inject","z":"f2918211b951343b","name":"手動連接","props":[{"p":"action","v":"connect","vt":"str"},{"p":"broker","v":"{\"type\":\"mqtt-broker\",\"broker\":\"192.168.88.5\",\"port\":1883,\"force\":true,\"clientid\":\"nodered_debug\",\"autoConnect\":false,\"protocolVersion\":\"3.1.1\",\"keepalive\":\"120\",\"username\":\"admin_nodered_debug\",\"password\":\"2E04DD7795E3B09D0C18350C14B204AC\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":880,"y":120,"wires":[["2fbfe744fc88bd59"]]},{"id":"2fbfe744fc88bd59","type":"mqtt in","z":"f2918211b951343b","name":"","topic":"","qos":"2","datatype":"auto","broker":"b68d71bd98332c07","nl":false,"rap":true,"rh":0,"inputs":1,"x":1090,"y":180,"wires":[["23764904f218a6d5"]]},{"id":"b68d71bd98332c07","type":"mqtt-broker","name":"動態(tài)連接MQTT","broker":"192.168.88.5","port":"1883","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
導入完成后會輸出成功日志如下圖。
打開剛才導入的“MQTT IN”節(jié)點,與常規(guī)連接一樣,使用動態(tài)連接也需要配置一個用于存儲動態(tài)連接參數(shù)的空間,需要配置一個空的“服務(wù)端”,可以像我一樣將IP地址配置為127.0.0.1,端口為8888(也可以為其他任意值,不能為空,其余參數(shù)可以為空),實際節(jié)點在連接時不會優(yōu)先使用輸入的參數(shù)而是使用傳入的參數(shù)連接(導入的“手動連接”JSON中對應的字段參數(shù)),缺少的參數(shù)會采用此處默認參數(shù)(根據(jù)說明得出,并未實際測試),需要注意使用動態(tài)連接節(jié)點應當通過上一節(jié)點驅(qū)動而不是底層自動驅(qū)動因此需要取消勾選“Connect automatically”屬性;
打開導入的“手動連接”,一共配置了兩個字段,一個用于存在請求信息(msg.action,相當于告知MQTT IN節(jié)點應當如何解析數(shù)據(jù)),一個用于存儲連接參數(shù)(msg.broker);
msg.action支持一下功能:
“connect”,連接請求,用于告知MQTT IN節(jié)點使用msg.broker參數(shù)連接服務(wù)器;
“subscribe”,訂閱請求,用于告知MQTT IN節(jié)點使用msg.topic訂閱服務(wù)器的主題;
“unsubscribe”,取消訂閱,與“subscribe”對應使用,取消主題的訂閱;
“disconnect”,斷開服務(wù)器連接;
這里先說明“connect”方式需要的參數(shù),需要見參數(shù)存在broker字段,因此需要選擇使用JOSN,如下圖所示:
“type”,用于標記參數(shù),可以任意配置,也可缺省;
“broker”,用于存儲MQTT服務(wù)器地址;
“port”,用于存儲MQTT服務(wù)器端口;
“force”,強制斷開連接,一般使能,缺省為失能,可能返回錯誤(沒有斷開服務(wù)器連接時再次請求連接);
“clientid”,MQTT連接三要素的客戶端ID;
“autoConnect”,自動連接使能,建議關(guān)閉,避免發(fā)生錯誤;
“protocolVersion”,MQTT連接的版本;
“keepalive”,MQTT的?;钚奶ㄗh配置為120;
“username”,MQTT連接三要素的用戶名;
“password”,MQTT連接三要素的密鑰;
配置以上參數(shù)MQTT動態(tài)連接即可實現(xiàn),下面是連接的服務(wù)器日志,由于添加了數(shù)據(jù)庫認證會比無認證的多幾個認證日志;
點擊觸發(fā)后“MQTT IN”節(jié)點會提示已連接;
斷開連接就要比連接要簡單的多,只要傳入請求方式“msg.action”等于“disconnect”,Node-RED上面會連接很多的MQTT服務(wù)器,斷開對應的連接就需要再次使用到上面配置的“空服務(wù)器”;
上面已經(jīng)提到訂閱請求需要配置“msg.action”為“subscribe”,這是不在使用“broker”存儲參數(shù),而是使用“topic”存儲,這里的topic需要存儲連個信息一個是訂閱的地址(msg.topic.topic),一個是訂閱的等級(msg.topic.qos),如下圖:
取消訂閱只需要令“msg.action”為“unsubscribe”,其余與訂閱一致;
使用動態(tài)連接方式連接自建MQTT服務(wù)器,并通過“123”主題回傳當前時間戳測試;
Debug調(diào)試窗口輸入時間戳,動態(tài)連接訂閱有效;
MQTT服務(wù)器調(diào)試日志:
筆者工程文件如下,導入Node-RED使用:
[{"id":"a906262bda80a272","type":"inject","z":"5d4fa2aa0a8f4fcf","name":"手動連接","props":[{"p":"action","v":"connect","vt":"str"},{"p":"broker","v":"{\"type\":\"mqtt-broker\",\"broker\":\"192.168.88.5\",\"port\":1883,\"force\":true,\"clientid\":\"nodered_debug\",\"autoConnect\":false,\"protocolVersion\":\"3.1.1\",\"keepalive\":\"120\",\"username\":\"admin_nodered_debug\",\"password\":\"2E04DD7795E3B09D0C18350C14B204AC\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":440,"wires":[["ce21e6f7efcbbea1"]]},{"id":"ce21e6f7efcbbea1","type":"mqtt in","z":"5d4fa2aa0a8f4fcf","name":"","topic":"","qos":"2","datatype":"auto","broker":"b68d71bd98332c07","nl":false,"rap":true,"rh":0,"inputs":1,"x":370,"y":520,"wires":[["666a19ab1c8cb2bd"]]},{"id":"666a19ab1c8cb2bd","type":"debug","z":"5d4fa2aa0a8f4fcf", "name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":520,"wires":[]},{"id":"553cee9604c2a8ff","type":"inject","z":"5d4fa2aa0a8f4fcf","name":"斷開連接","props":[{"p":"action","v":"disconnect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay": 0.1,"topic":"","x":180,"y":620,"wires":[["ce21e6f7efcbbea1"]]},{"id":"07a0589c3a502788","type":"inject","z":"5d4fa2aa0a8f4fcf","name":"手動訂閱","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","v":"{\"topic\":\"123\",\"qos\":0}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":500,"wires":[["ce21e6f7efcbbea1"]]},{"id":"d81035879f55d047","type":"inject","z":"5d4fa2aa0a8f4fcf","name":"手動斷開訂閱","props":[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"topic","v":"{\"topic\":\"123654\",\"qos\":0}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":560,"wires":[["ce21e6f7efcbbea1"]]},{"id":"b8542c59cc5a49bd","type":"mqtt out","z":"5d4fa2aa0a8f4fcf","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b68d71bd98332c07","x":370,"y":740,"wires":[]},{"id":"77600c8d5cc27a7e","type":"inject","z":"5d4fa2aa0a8f4fcf","name":"動態(tài)訂閱測試","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"123","payloadType":"date","x":180,"y":740,"wires":[["b8542c59cc5a49bd"]]},{"id":"b68d71bd98332c07","type":"mqtt-broker","name":"動態(tài)連接MQTT","broker":"127.0.0.1","port":"8888","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
今天的分享就到這里啦,EBYTE每一天都致力于更好的助力物聯(lián)化、智能化、自動化的發(fā)展,提升資源利用率,更多串口服務(wù)器、數(shù)傳電臺、lora模塊等無線數(shù)傳模塊產(chǎn)品更多資料,感興趣的小伙伴可以登錄我們的億佰特官網(wǎng)和企業(yè)公眾號(微信號:cdebyte)進行了解,也可以直接撥打400電話咨詢技術(shù)專員!
相關(guān)閱讀:
1、使用Node-RED實現(xiàn)Modbus TCP到RTU的轉(zhuǎn)換案例
2、MQTT協(xié)議報文中TPC驅(qū)動和字符轉(zhuǎn)換教程
3、西門子PLC利用函數(shù)塊連接MQTT服務(wù)器發(fā)布消息教程