Node-RED のスニペット集
このページは、Node-RED のスニペットをまとめる予定のページです。
目次
スニペット
JSON ファイルの読み込み
[{"id":"a025f830.d9b208","type":"tab","label":"フロー","disabled":false,"info":""},{"id":"529d25aa.c852cc","type":"json","z":"a025f830.d9b208","name":"","property":"payload","action":"","pretty":false,"x":450,"y":80,"wires":[["feb335b0.dfaa08"]]},{"id":"ea262e4f.1c0b5","type":"file in","z":"a025f830.d9b208","name":"JSON 読み込み","filename":"/home/pi/test.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":300,"y":80,"wires":[["529d25aa.c852cc"]]},{"id":"feb335b0.dfaa08","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":80,"wires":[]},{"id":"4e223946.788d08","type":"inject","z":"a025f830.d9b208","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":80,"wires":[["ea262e4f.1c0b5"]]}]
読み込んだ JSON の値を flow コンテキスト (変数) にセット
[{"id":"a025f830.d9b208","type":"tab","label":"フロー","disabled":false,"info":""},{"id":"529d25aa.c852cc","type":"json","z":"a025f830.d9b208","name":"","property":"payload","action":"","pretty":false,"x":450,"y":80,"wires":[["feb335b0.dfaa08","6778456c.b2a20c"]]},{"id":"ea262e4f.1c0b5","type":"file in","z":"a025f830.d9b208","name":"JSON 読み込み","filename":"/home/pi/test.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":300,"y":80,"wires":[["529d25aa.c852cc"]]},{"id":"feb335b0.dfaa08","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":80,"wires":[]},{"id":"4e223946.788d08","type":"inject","z":"a025f830.d9b208","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":80,"wires":[["ea262e4f.1c0b5"]]},{"id":"6778456c.b2a20c","type":"change","z":"a025f830.d9b208","name":"","rules":[{"t":"set","p":"test","pt":"flow","to":"payload.test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":120,"wires":[["e5d02f73.e728"]]},{"id":"ded4fe88.b6fd8","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":120,"wires":[]},{"id":"e5d02f73.e728","type":"function","z":"a025f830.d9b208","name":"flow から値を取得","func":"return { payload: flow.get('test') };","outputs":1,"noerr":0,"x":770,"y":120,"wires":[["ded4fe88.b6fd8"]]}]
- change ノードで flow コンテキストに値をセットし、確認として function ノードで読み出します。
TCP → HTTP POST (application/x-www-form-urlencoded)
[{"id":"a025f830.d9b208","type":"tab","label":"フロー","disabled":false,"info":""},{"id":"5c2bd026.3e6fb","type":"tcp in","z":"a025f830.d9b208","name":"","server":"client","host":"1.2.3.4","port":"1234","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":160,"y":60,"wires":[["356b1c98.067714","1396c63.116cd3a"]]},{"id":"b9dba40c.fbb5e8","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":100,"wires":[]},{"id":"7a860750.1fb818","type":"http request","z":"a025f830.d9b208","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://example.com/test.php","tls":"","persist":false,"proxy":"","authType":"","x":550,"y":60,"wires":[["ef72018d.809c6"]]},{"id":"ef72018d.809c6","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":60,"wires":[]},{"id":"356b1c98.067714","type":"function","z":"a025f830.d9b208","name":"リクエスト変換","func":"msg.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };\nmsg.payload = { test: msg.payload };\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":60,"wires":[["b9dba40c.fbb5e8","7a860750.1fb818"]]},{"id":"1396c63.116cd3a","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":100,"wires":[]}]
- TCP クライアントとして機器などからデータを取得し、HTTP でデータを POST します。
- 必要な設定
- tcp in ノード: 接続先 IP アドレス、ポートの変更
- function ノード: パラメータ名 (test) の変更
- http request ノード: URL の変更
Alexa で Windows PC を ON/OFF する
[{"id":"a025f830.d9b208","type":"tab","label":"フロー","disabled":false,"info":""},{"id":"3e3a8069.0764e","type":"wake on lan","z":"a025f830.d9b208","mac":"00:11:22:33:44:55","host":"","udpport":9,"name":"WOL","x":390,"y":60,"wires":[]},{"id":"ee1dcfbf.c84c4","type":"alexa-home","z":"a025f830.d9b208","conf":"390ae4f2.cb443c","device":"100674","acknoledge":true,"name":"PC","topic":"","x":80,"y":80,"wires":[["76411b4d.563844","847bc238.ca3a3"]]},{"id":"76411b4d.563844","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":230,"y":120,"wires":[]},{"id":"847bc238.ca3a3","type":"switch","z":"a025f830.d9b208","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":210,"y":80,"wires":[["3e3a8069.0764e"],["c9baac77.365dc"]]},{"id":"c9baac77.365dc","type":"exec","z":"a025f830.d9b208","command":"net rpc shutdown -I IPアドレス -U \"ユーザー名%パスワード\"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"shutdown","x":400,"y":100,"wires":[["17682cb6.3b6023"],[],[]]},{"id":"17682cb6.3b6023","type":"debug","z":"a025f830.d9b208","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":100,"wires":[]},{"id":"390ae4f2.cb443c","type":"alexa-home-conf","z":"","username":""}]
- 追加が必要なノード
- node-red-contrib-alexa-home-skill
- node-red-node-wol
- 必要な設定
- alexa-home ノード: Node-RED Alexa Home Skill Bridge で登録したユーザー名、パスワード、デバイスの設定
- wake on lan ノード: 操作したい PC (Wake On Lan に対応している) の MAC アドレスの設定
- exec ノード:
net rpc shutdown
の引数 (操作したい PC の IP アドレス、ユーザー名、パスワード)