Node-REDのメモ MQTT編
Node-REDでMQTT通信操作をするときの設定手順のメモ。
鶏頭で忘れっぽいのでメモ。
Node-REDは起動済みでブラウザで接続済みであるものとします。
ここではテストツールにmosquitto broker および mosquitto client toolsを使うこととします。
これらは以下のコマンドでインストールできます。
Ubuntuで動作確認。RaspberryPiでも大丈夫なはず。
sudo apt install mosquitto mosquitto-clients
MQTTでデータを送信してみます
あらかじめ、ターミナル or コンソールで以下のようにsubscriberコマンドを実行します。
mosquitto_sub -h 《ブローカのアドレス》 -v -t 《トピック》
例えば以下。マシン名PiDev25.localのブローカからすべてのトピック(“#”)を取得するよう指定しています。
-v 指定により、対象メッセージのトピックも表示されます。
mosquitto_sub -h Pidev25.local -v -t "#"
Node-RED側で mqttの送信をトリガするとmosquitto_subの表示に送信したデータが表示されます。
以下の表示例でデータが受信できていることが確認できます。
test_data 0
test_data 1
test_data true
test_data false
test_data 文字列
test_data {"data1":0,"data2":1,"data3":"data"}
[
{
"id": "f580f01a.f771f",
"type": "tab",
"label": "MQTT_publish",
"disabled": false,
"info": ""
},
{
"id": "33450bad.82764c",
"type": "inject",
"z": "f580f01a.f771f",
"name": "",
"topic": "test_data",
"payload": "0",
"payloadType": "num",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 40,
"wires": [
[
"e6218cda.844308"
]
]
},
{
"id": "ea6d9236.74e038",
"type": "inject",
"z": "f580f01a.f771f",
"name": "",
"topic": "test_data",
"payload": "1",
"payloadType": "num",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 80,
"wires": [
[
"e6218cda.844308"
]
]
},
{
"id": "8d7baa4c.08385",
"type": "inject",
"z": "f580f01a.f771f",
"name": "",
"topic": "test_data",
"payload": "true",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 120,
"wires": [
[
"e6218cda.844308"
]
]
},
{
"id": "82148e63.4b97e",
"type": "inject",
"z": "f580f01a.f771f",
"name": "",
"topic": "test_data",
"payload": "false",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 120,
"y": 160,
"wires": [
[
"e6218cda.844308"
]
]
},
{
"id": "d0b1ac7f.0ac0b",
"type": "inject",
"z": "f580f01a.f771f",
"name": "",
"topic": "test_data",
"payload": "文字列",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 120,
"y": 200,
"wires": [
[
"e6218cda.844308"
]
]
},
{
"id": "7e6180b5.f29098",
"type": "inject",
"z": "f580f01a.f771f",
"name": "オブジェクト",
"topic": "test_data",
"payload": "{\"data1\":0,\"data2\":1,\"data3\":\"data\"}",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 240,
"wires": [
[
"d5a5b2db.83462",
"e6218cda.844308"
]
]
},
{
"id": "6f26fe46.a01f58",
"type": "inject",
"z": "f580f01a.f771f",
"name": "バッファ",
"topic": "test_data",
"payload": "[0,1,2,3]",
"payloadType": "bin",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 140,
"y": 280,
"wires": [
[
"d5a5b2db.83462",
"e6218cda.844308"
]
]
},
{
"id": "d5a5b2db.83462",
"type": "debug",
"z": "f580f01a.f771f",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 500,
"y": 280,
"wires": []
},
{
"id": "e6218cda.844308",
"type": "mqtt out",
"z": "f580f01a.f771f",
"name": "mqtt://PiDev25.local:1883",
"topic": "",
"qos": "",
"retain": "",
"broker": "6e6a010.01825",
"x": 570,
"y": 40,
"wires": []
},
{
"id": "6e6a010.01825",
"type": "mqtt-broker",
"z": "",
"name": "",
"broker": "PiDev25.local ",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]
MQTTでデータを受信してみます
デプロイ後、ターミナル or コンソールで以下のようにpublisherコマンドを実行します。
mosquitto_pub -h 《ブローカのアドレス》 -t 《トピック》 -m 《メッセージ》
例えば以下。マシン名PiDev25.localのブローカへ、トピック test_data で、メッセージ test を送信しています。
mosquitto_pub -h PiDev25.local -t "test_data" -m "test"
コマンドを実行するとNode-RED側で受信され、メッセージ処理ノードが実行されます。
[
{
"id": "13f22fdd.2e009",
"type": "tab",
"label": "MQTT_subscribe",
"disabled": false,
"info": ""
},
{
"id": "1760f49a.fd2d3b",
"type": "debug",
"z": "13f22fdd.2e009",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 420,
"y": 60,
"wires": []
},
{
"id": "30229cea.13aba4",
"type": "mqtt in",
"z": "13f22fdd.2e009",
"name": "mqtt://PiDev25.local:1883",
"topic": "#",
"qos": "2",
"datatype": "auto",
"broker": "6e6a010.01825",
"x": 150,
"y": 60,
"wires": [
[
"1760f49a.fd2d3b"
]
]
},
{
"id": "6e6a010.01825",
"type": "mqtt-broker",
"z": "",
"name": "",
"broker": "PiDev25.local ",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]