這邊寫了一個簡單的 MQTT 發送及訂閱範例:
[
{
"id": "f98c0ea129c57edb",
"type": "tab",
"label": "MQTT TEST",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ddc095604858794c",
"type": "inject",
"z": "f98c0ea129c57edb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 200,
"y": 120,
"wires": [
[
"19df58cfd37eee95"
]
]
},
{
"id": "ea74ca81172a704d",
"type": "mqtt in",
"z": "f98c0ea129c57edb",
"name": "",
"topic": "/test",
"qos": "2",
"datatype": "auto-detect",
"broker": "f5b85ab48d25f0d7",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 190,
"y": 180,
"wires": [
[
"42d2cd098ad64423"
]
]
},
{
"id": "19df58cfd37eee95",
"type": "mqtt out",
"z": "f98c0ea129c57edb",
"name": "",
"topic": "/test",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "f5b85ab48d25f0d7",
"x": 330,
"y": 120,
"wires": []
},
{
"id": "42d2cd098ad64423",
"type": "debug",
"z": "f98c0ea129c57edb",
"name": "MQTT debug",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 330,
"y": 180,
"wires": []
},
{
"id": "f5b85ab48d25f0d7",
"type": "mqtt-broker",
"name": "",
"broker": "localhost",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
時間軸會將時間戳寫入 msg.payload,並由 mqtt out 送出資料到遠端 Broker 的指定主題 (這邊設 /test),接著會由 mqtt in 訂閱遠端 Broker 的指定主題 (即 /test) 輸出到 debug 節點查看接收資料。