8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Watson IoT Platform - Node-RED - フロー内で共有する変数など

Last updated at Posted at 2016-05-22

初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
Watson IoT Platformを使ってみるでIoTアプリを準備しました。

#フロー内で共有できる変数 - global

global変数の使い方
[{"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の値が増加する事を確認します。
    スクリーンショット 2016-06-29 11.19.06.png
    スクリーンショット 2016-06-29 11.19.13.png

  • 二つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でもglobal変数 sumの値へ加算される事を確認します。
    スクリーンショット 2016-06-29 11.20.12.png
    スクリーンショット 2016-06-29 11.20.23.png

#ノードで共有できる変数 - context

  • Node-REDのノードで値を共有できる変数をcontextで作成する事ができます。

  • 前節のglobal変数とは異なり、context変数ではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する(例:カウンターを加算していく、等)際に使用できます。

  • Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。

context変数の使い方
[{"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の値が増加する事を確認します。
    スクリーンショット 2016-05-22 9.05.23.png
    スクリーンショット 2016-05-22 8.29.26.png
    スクリーンショット 2016-05-22 8.29.36.png
    スクリーンショット 2016-05-22 8.29.43.png

  • 二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード(=ノードを共有していない)ため、一つめのフローのcontext変数sumへは加算されず、二つめのフローのcontext変数 sum(上記とは同名だが異なる変数)へ加算される事を確認します。
    スクリーンショット 2016-06-29 11.28.23.png


(以下はdeprecateされています。参照Node-RED: Writing Functions
#フロー内で共有できる変数 - context.global

context.global変数の使い方
[{"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"]]}]

  • 一つめのフローのinjectノード左側のボタンを押してcontext.global.sum変数を初期化します。
    スクリーンショット 2016-05-22 8.20.33.png
    スクリーンショット 2016-05-22 8.29.06.png

  • 二つめのフローの「+1」と書かれたinjectノードのボタンを押して、メッセージが送付されるたび(異なるメッセージ)にcontext.global.sumの値が増加する事を確認します。
    スクリーンショット 2016-05-22 8.28.42.png
    スクリーンショット 2016-05-22 8.29.26.png
    スクリーンショット 2016-05-22 8.29.36.png
    スクリーンショット 2016-05-22 8.29.43.png

  • 三つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でもcontext.global.sumの値へ加算される事を確認します。
    スクリーンショット 2016-05-22 8.28.49.png
    スクリーンショット 2016-05-22 8.29.53.png

#ノードで共有できる変数 - context

  • Node-REDのノードで値を共有できる変数をcontext.xxxで作成する事ができます。

  • 前節のcontext.global.xxxとは異なり、context.xxxではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する(例:カウンターを加算していく、等)際に使用できます。

  • Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。

context.global変数の使い方
[{"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の値が増加する事を確認します。
    スクリーンショット 2016-05-22 9.05.23.png
    スクリーンショット 2016-05-22 8.29.26.png
    スクリーンショット 2016-05-22 8.29.36.png
    スクリーンショット 2016-05-22 8.29.43.png

  • 二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード(=ノードを共有していない)ため、context.sumの値へは加算されないことを確認します。Debug出力へ表示される「NaN」は「Not a Number」の略で、この「別フローから加算(できない)」functionノード内のcontext.sum変数が初期化されていないためにエラーとなっていることを表します。
    スクリーンショット 2016-05-22 9.05.36.png
    スクリーンショット 2016-05-22 9.05.42.png

#参考情報
Node RED Programming Guide - Lecture 5
Using Node-RED: Writing Functions

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?