初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
Watson IoT Platformを使ってみるでIoTアプリを準備しました。
#フロー内で共有できる変数 - global
- Node-REDのフロー内で値を共有できる変数を
global
で作成する事ができます。 - Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
[{"id":"5106b054.b39f3","type":"inject","z":"94072ee9.c38dc","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":150,"y":160,"wires":[["3fd0f11e.c35986"]]},{"id":"6b4393d4.788cbc","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":160,"wires":[]},{"id":"3fd0f11e.c35986","type":"function","z":"94072ee9.c38dc","name":"global変数への加算","func":"var s = global.get(\"sum\")||0;\ns += msg.payload;\nglobal.set(\"sum\", s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":160,"wires":[["6b4393d4.788cbc"]]},{"id":"ed60fe08.fe653","type":"function","z":"94072ee9.c38dc","name":"リセット","func":"var zero = 0\nglobal.set(\"sum\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":280,"wires":[["37330d4d.8c5f42"]]},{"id":"2208c51b.9bacea","type":"inject","z":"94072ee9.c38dc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":280,"wires":[["ed60fe08.fe653"]]},{"id":"37330d4d.8c5f42","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":280,"wires":[]},{"id":"833a6849.8161f8","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":220,"wires":[]},{"id":"42ad8aaf.dcb134","type":"inject","z":"94072ee9.c38dc","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":150,"y":220,"wires":[["a757adef.5e9a4"]]},{"id":"a757adef.5e9a4","type":"function","z":"94072ee9.c38dc","name":"global変数への加算","func":"var s = global.get(\"sum\")||0;\ns += msg.payload;\nglobal.set(\"sum\", s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":220,"wires":[["833a6849.8161f8"]]}]
-
一つめのフローの「+1」と書かれたinjectノードのボタンを押して、メッセージが送付されるたび(異なるメッセージ)にglobal変数
sum
の値が増加する事を確認します。
-
二つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でもglobal変数
sum
の値へ加算される事を確認します。
#ノードで共有できる変数 - context
-
Node-REDのノードで値を共有できる変数を
context
で作成する事ができます。 -
前節の
global
変数とは異なり、context
変数ではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する(例:カウンターを加算していく、等)際に使用できます。 -
Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
[{"id":"dfafe05f.baa85","type":"function","z":"caa14425.00de","name":"context変数へ追加","func":"var s = context.get(\"sum\")||0;\ns += msg.payload;\ncontext.set(\"sum\",s);\nmsg.sum = s;\nreturn msg;\n","outputs":1,"noerr":0,"x":290,"y":120,"wires":[["91d34062.7e7f1"]]},{"id":"60095533.c40fb4","type":"inject","z":"caa14425.00de","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":90,"y":120,"wires":[["dfafe05f.baa85"]]},{"id":"91d34062.7e7f1","type":"debug","z":"caa14425.00de","name":"","active":true,"console":"false","complete":"sum","x":560,"y":120,"wires":[]},{"id":"b696395c.102698","type":"debug","z":"caa14425.00de","name":"","active":true,"console":"false","complete":"sum","x":560,"y":200,"wires":[]},{"id":"14c839f2.4d3a9e","type":"inject","z":"caa14425.00de","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":90,"y":200,"wires":[["96508a47.9eace"]]},{"id":"96508a47.9eace","type":"function","z":"caa14425.00de","name":"別フローから加算(できない)","func":"var s = context.get(\"sum\")||0;\ns += msg.payload;\ncontext.set(\"sum\",s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":200,"wires":[["b696395c.102698"]]}]
-
一つめのフローの「+1」と書かれたinjectノード左側のボタンを押して、メッセージが送付されるたび(異なるメッセージ)にcontext変数
sum
の値が増加する事を確認します。
-
二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード(=ノードを共有していない)ため、一つめのフローのcontext変数
sum
へは加算されず、二つめのフローのcontext変数sum
(上記とは同名だが異なる変数)へ加算される事を確認します。
(以下はdeprecateされています。参照Node-RED: Writing Functions)
#フロー内で共有できる変数 - context.global
- Node-REDのフロー内で値を共有できる変数を
context.global.xxx
で作成する事ができます。 - Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
[{"id":"563a7a92.68a924","type":"inject","z":"ff1028b2.e29428","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":380,"wires":[["1c71e61b.77669a"]]},{"id":"4d54ab3a.4f00a4","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":380,"wires":[]},{"id":"1c71e61b.77669a","type":"function","z":"ff1028b2.e29428","name":"context.global.sumへ加算","func":"context.global.sum = context.global.sum + msg.payload;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":380,"wires":[["4d54ab3a.4f00a4"]]},{"id":"a1fa5bd7.2b233","type":"function","z":"ff1028b2.e29428","name":"初期化","func":"context.global.sum = 0;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":320,"wires":[["3622297d.12c756"]]},{"id":"bc2193de.68c978","type":"inject","z":"ff1028b2.e29428","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":320,"wires":[["a1fa5bd7.2b233"]]},{"id":"3622297d.12c756","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":320,"wires":[]},{"id":"ef53b486.3e3738","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":440,"wires":[]},{"id":"5eb4468e.a7dd98","type":"function","z":"ff1028b2.e29428","name":"context.global.sumへ加算","func":"context.global.sum = context.global.sum + msg.payload;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":440,"wires":[["ef53b486.3e3738"]]},{"id":"130f18ba.c828df","type":"inject","z":"ff1028b2.e29428","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":440,"wires":[["5eb4468e.a7dd98"]]}]
-
二つめのフローの「+1」と書かれたinjectノードのボタンを押して、メッセージが送付されるたび(異なるメッセージ)に
context.global.sum
の値が増加する事を確認します。
-
三つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でも
context.global.sum
の値へ加算される事を確認します。
#ノードで共有できる変数 - context
-
Node-REDのノードで値を共有できる変数を
context.xxx
で作成する事ができます。 -
前節の
context.global.xxx
とは異なり、context.xxx
ではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する(例:カウンターを加算していく、等)際に使用できます。 -
Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
[{"id":"ff6100ff.099f88","type":"function","z":"ff1028b2.e29428","name":"context変数へ追加","func":"if (!context.sum) {\n context.sum = 0;\n}\ncontext.sum = context.sum + msg.payload;\nmsg.sum = context.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1120,"wires":[["745271a5.6ae3b"]]},{"id":"ee780d9a.5322f","type":"inject","z":"ff1028b2.e29428","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":1120,"wires":[["ff6100ff.099f88"]]},{"id":"745271a5.6ae3b","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":600,"y":1120,"wires":[]},{"id":"cf0aa564.303ac","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":600,"y":1200,"wires":[]},{"id":"2555f2a4.b9bcbe","type":"function","z":"ff1028b2.e29428","name":"別フローから加算(できない)","func":"context.sum = context.sum + msg.payload;\nmsg.sum = context.sum;\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":1200,"wires":[["cf0aa564.303ac"]]},{"id":"923f735a.f5084","type":"inject","z":"ff1028b2.e29428","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":1200,"wires":[["2555f2a4.b9bcbe"]]}]
-
一つめのフローの「+1」と書かれたinjectノード左側のボタンを押して、メッセージが送付されるたび(異なるメッセージ)に
context.sum
の値が増加する事を確認します。
-
二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード(=ノードを共有していない)ため、
context.sum
の値へは加算されないことを確認します。Debug出力へ表示される「NaN」は「Not a Number」の略で、この「別フローから加算(できない)」functionノード内のcontext.sum
変数が初期化されていないためにエラーとなっていることを表します。
#参考情報
Node RED Programming Guide - Lecture 5
Using Node-RED: Writing Functions