Node-REDのメモ 応用編 GPIO+Websocket+Dashboard
Node-REDでGPIOの入出力データをWebsocketで飛ばして、Dashboardから操作/Dashboardで表示するときの設定手順のメモ。
鶏頭で忘れっぽいのでメモ。
それぞれの基本は、Node-REDのHowTo(その1) ~ (その4)参照
その3 の Websocketでデータ送信(クライアント)、Websocketでデータを受信(クライアント)の手順で作成したノードに接続
このとき、GPIO入力データをそのまま送信すると、受け取り側で「0はON?それともOFF?」となってしまうので、GPIO入力の0/1をON/OFFの文字列に変換するファンクションノードを追加している。
また、GPIO出力側も同様に、送信側から送られてきたON/OFFの文字列をGPIO出力データの1/0の数値に変換するファンクションノードを挿入している。
ターゲットボードの正論理/負論理が変更になった場合はこれらのファンクションノードで調整すれば良い。
[
{
"id": "6e229d5a.774af4",
"type": "tab",
"label": "GPIO+Websocket",
"disabled": false,
"info": ""
},
{
"id": "109b9bce.e015e4",
"type": "rpi-gpio out",
"z": "6e229d5a.774af4",
"name": "LED_0",
"pin": "18",
"set": true,
"level": "0",
"freq": "",
"out": "out",
"x": 730,
"y": 80,
"wires": []
},
{
"id": "4834db5b.5c24d4",
"type": "rpi-gpio in",
"z": "6e229d5a.774af4",
"name": "SW0",
"pin": "15",
"intype": "up",
"debounce": "25",
"read": true,
"x": 190,
"y": 200,
"wires": [
[
"3ff59514.52c732"
]
]
},
{
"id": "a7d90bda.d7b4b8",
"type": "websocket in",
"z": "6e229d5a.774af4",
"name": "",
"server": "",
"client": "47e24d88.c603e4",
"x": 280,
"y": 80,
"wires": [
[
"a38bd8a1.0a5bd8",
"5e99ea44.cfdac4"
]
]
},
{
"id": "9bc37a7.e1e9808",
"type": "websocket out",
"z": "6e229d5a.774af4",
"name": "",
"server": "",
"client": "c4985621.1edb48",
"x": 830,
"y": 200,
"wires": []
},
{
"id": "a38bd8a1.0a5bd8",
"type": "debug",
"z": "6e229d5a.774af4",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 730,
"y": 140,
"wires": []
},
{
"id": "3ff59514.52c732",
"type": "function",
"z": "6e229d5a.774af4",
"name": "1/0→OFF/ON",
"func": "if (msg.payload) {\n msg.payload = 'OFF';\n}\nelse {\n msg.payload = 'ON';\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 540,
"y": 200,
"wires": [
[
"9bc37a7.e1e9808",
"a38bd8a1.0a5bd8"
]
]
},
{
"id": "5e99ea44.cfdac4",
"type": "function",
"z": "6e229d5a.774af4",
"name": "OFF/ON→0/1",
"func": "if (msg.payload == 'OFF') {\n msg.payload = 0;\n}\nelse {\n msg.payload = 1;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 540,
"y": 80,
"wires": [
[
"109b9bce.e015e4"
]
]
},
{
"id": "47e24d88.c603e4",
"type": "websocket-client",
"z": "",
"path": "ws://PiDev25.local:1880/ws/led0",
"tls": "",
"wholemsg": "false"
},
{
"id": "c4985621.1edb48",
"type": "websocket-client",
"z": "",
"path": "ws://PiDev25.local:1880/ws/sw0",
"tls": "",
"wholemsg": "false"
}
]
この状態でRaspberryPi側でスイッチをON/OFFすると、Dashboardの「SW」の文字列のON/OFFが切り替わる。 また、DashboardのLLED_ON/LED_OFFのボタンをクリックすると、LEDが点灯/消灯する
[
{
"id": "ca6a740f.4d43b8",
"type": "tab",
"label": "Websocket_GPIO",
"disabled": false,
"info": ""
},
{
"id": "ac19aeee.9f8f5",
"type": "ui_button",
"z": "ca6a740f.4d43b8",
"name": "",
"group": "b7e42e0e.52bdb",
"order": 1,
"width": 2,
"height": 1,
"passthru": false,
"label": "LED0_ON",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "ON",
"payloadType": "str",
"topic": "",
"x": 260,
"y": 120,
"wires": [
[
"299e8287.226e6e",
"95b08556.d2cce"
]
]
},
{
"id": "2098e31e.e6832c",
"type": "ui_button",
"z": "ca6a740f.4d43b8",
"name": "",
"group": "b7e42e0e.52bdb",
"order": 2,
"width": 2,
"height": 1,
"passthru": false,
"label": "LED0_OFF",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "OFF",
"payloadType": "str",
"topic": "",
"x": 270,
"y": 160,
"wires": [
[
"299e8287.226e6e",
"95b08556.d2cce"
]
]
},
{
"id": "8db70b6.86a1178",
"type": "ui_text",
"z": "ca6a740f.4d43b8",
"group": "b7e42e0e.52bdb",
"order": 3,
"width": 2,
"height": 1,
"name": "",
"label": "SW",
"format": "{{value}}",
"layout": "row-spread",
"x": 610,
"y": 240,
"wires": []
},
{
"id": "8f45af19.269e",
"type": "websocket in",
"z": "ca6a740f.4d43b8",
"name": "",
"server": "fad64a63.6141a",
"client": "",
"x": 270,
"y": 240,
"wires": [
[
"8db70b6.86a1178",
"95b08556.d2cce"
]
]
},
{
"id": "299e8287.226e6e",
"type": "websocket out",
"z": "ca6a740f.4d43b8",
"name": "",
"server": "d7b33218.4a1f28",
"client": "",
"x": 630,
"y": 120,
"wires": []
},
{
"id": "95b08556.d2cce",
"type": "debug",
"z": "ca6a740f.4d43b8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 610,
"y": 180,
"wires": []
},
{
"id": "b7e42e0e.52bdb",
"type": "ui_group",
"z": "",
"name": "グループ1",
"tab": "1b4acf7b.a194c9",
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "fad64a63.6141a",
"type": "websocket-listener",
"z": "",
"path": "/ws/sw0",
"wholemsg": "false"
},
{
"id": "d7b33218.4a1f28",
"type": "websocket-listener",
"z": "",
"path": "/ws/led0",
"wholemsg": "false"
},
{
"id": "1b4acf7b.a194c9",
"type": "ui_tab",
"z": "",
"name": "タブ3",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]