Raspberry Pi の Node-RED でスライダとボタンで LED 制御

## Raspberry Pi にインストールした Node-RED でLEDの制御をしてみる##
### 使用環境 ###
1. Raspberry Pi
2. Node-RED + node-red-node-pi-gpio + node-red-dashboard
3. ブレッドボード
4. LED
5. 抵抗（1kΩ程度）
6. ブレッドボード用ワイヤ（オス―メス）

### 1. Raspberry Pi に接続したLEDのON/OFF ###

　Node-RED を起動してフローエディタで node-red-node-pi-gpio と node-red-dashboard がインストールされていることを確認します。
　ボタンとrpi gpio out をフローに追加します。
　rpi gpio out は、使用するピン番号を選択します。設定後、各ノードをワイヤで結びます。
　実物の回路を作成します。rpi gpio out で設定したピンとブレッドボードの抵抗－LED－Raspberry Pi の GND とワイヤで接続します。
　Node-RED のフローをデブロイし、ダッシュボードを表示します。ボタンをクリックするとLEDが点灯／消灯すると思います。
![2020-07-28-044131_1824x984_scrot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/199447/60bc07de-852a-9968-f501-f741103e0b19.png)


### 2. Slider で明るさ制御 ###

　１．で作成したフローのボタンの代わりにスライダーを使って、LEDの明るさを制御してみましょう。
　rpi gpio out の設定画面で、pwm出力を選択し、スライダの出力値も0から100とします。
　gauge も追加してスライダの値を表示するようにしてみましょう。
　フローが完成したらデブロイして動作確認してみましょう。
![2020-07-28-050016_1824x984_scrot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/199447/b7ecb682-baaa-da82-92c2-a90eaf3d4111.png)


### 3. OFF時の値を記憶、ONで復元 ###
　２．で slider を利用して LED の明るさ制御ができたので、button を追加して、以下の機能を追加してみましょう。
　① OFF ボタンを追加、クリックすると、slider の値を記憶し、slider の値を 0 にする。
　② ON ボタンを追加、クリックすると、①で記憶した値が slider にセットされ元の明るさに戻る

![2020-07-28-050727_1824x984_scrot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/199447/12903246-f183-606a-122d-12dc563d934e.png)


解説
グローバル変数を利用してスライダの値を記憶しています。
ひとつの変数でできそうな気もしていますが、ふたつ使用しています。OFFにした時のスライダの値をグローバル変数に入れて、ONにするときに戻す。というフローなのですが、スライダの値を常にグローバル変数に入れておいて、Off時にその値を記憶用のグローバル変数に入れるという２度手間をしています。変数ひとつでは、なぜかかうまく行きません。

```json
[{"id":"56e51ee9.0be3e","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"4077a8d3.819f28","type":"inject","z":"56e51ee9.0be3e","name":"","topic":"","payload":"val1","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":80,"wires":[["47faa491.fb3f5c"]]},{"id":"a24d7f6a.1d369","type":"inject","z":"56e51ee9.0be3e","name":"","topic":"","payload":"val2","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":480,"wires":[["3f6f05a1.7c450a"]]},{"id":"35dae3b0.8aaf5c","type":"ui_button","z":"56e51ee9.0be3e","name":"","group":"1ec6144c.35129c","order":0,"width":0,"height":0,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"red","icon":"","payload":"","payloadType":"str","topic":"","x":130,"y":380,"wires":[["3f6f05a1.7c450a"]]},{"id":"a4cc8c67.11ea3","type":"ui_slider","z":"56e51ee9.0be3e","name":"","label":"slider","tooltip":"","group":"1ec6144c.35129c","order":1,"width":0,"height":0,"passthru":true,"outs":"all","topic":"","min":0,"max":"100","step":1,"x":610,"y":220,"wires":[["47faa491.fb3f5c"]]},{"id":"ab97b275.57928","type":"ui_text","z":"56e51ee9.0be3e","group":"1ec6144c.35129c","order":2,"width":0,"height":0,"name":"","label":"Sliderの値","format":"{{msg.payload}}","layout":"row-spread","x":1010,"y":160,"wires":[]},{"id":"47faa491.fb3f5c","type":"change","z":"56e51ee9.0be3e","name":"","rules":[{"t":"set","p":"val1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":160,"wires":[["ab97b275.57928","1b0df224.d9033e"]]},{"id":"a8c80c89.a2108","type":"change","z":"56e51ee9.0be3e","name":"set 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":240,"wires":[["a4cc8c67.11ea3"]]},{"id":"3f6f05a1.7c450a","type":"change","z":"56e51ee9.0be3e","name":"","rules":[{"t":"set","p":"val2","pt":"global","to":"val1","tot":"global"},{"t":"set","p":"payload","pt":"msg","to":"val2","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":380,"wires":[["a8c80c89.a2108","26c62963.9c10f6"]]},{"id":"26c62963.9c10f6","type":"ui_text","z":"56e51ee9.0be3e","group":"1ec6144c.35129c","order":3,"width":0,"height":0,"name":"","label":"OFF時の値","format":"{{msg.payload}}","layout":"row-spread","x":630,"y":380,"wires":[]},{"id":"a566d379.fa7ff","type":"ui_button","z":"56e51ee9.0be3e","name":"","group":"1ec6144c.35129c","order":4,"width":0,"height":0,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"green","icon":"","payload":"","payloadType":"str","topic":"","x":130,"y":180,"wires":[["76fc41eb.42f7d"]]},{"id":"76fc41eb.42f7d","type":"change","z":"56e51ee9.0be3e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"val2","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["a4cc8c67.11ea3"]]},{"id":"1b0df224.d9033e","type":"rpi-gpio out","z":"56e51ee9.0be3e","name":"","pin":"40","set":"","level":"0","freq":"","out":"pwm","x":1020,"y":260,"wires":[]},{"id":"1ec6144c.35129c","type":"ui_group","z":"","name":"","tab":"a87753cb.12868","order":1,"disp":true,"width":"6","collapse":false},{"id":"a87753cb.12868","type":"ui_tab","z":"","name":"ホーム","icon":"dashboard","disabled":false,"hidden":false}]

```

## 修正　###
かなり恥ずかしい・・・
変数の使用は、injection してからと、勝手に思い込んでました。いきなり使えます。
次のように変更します。
変数のスコープも flow に変更しました。

![2020-08-01-064519_1824x984_scrot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/199447/9a691f3a-be82-001a-db6c-71feaf94a0db.png)

```json
[{"id":"d2eeb423.09a668","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"d9d18049.0e2f","type":"ui_button","z":"d2eeb423.09a668","name":"","group":"1ec6144c.35129c","order":4,"width":0,"height":0,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"red","icon":"","payload":"","payloadType":"str","topic":"","x":130,"y":380,"wires":[["6b69cbd.7411034"]]},{"id":"866635c1.680b98","type":"ui_slider","z":"d2eeb423.09a668","name":"","label":"slider","tooltip":"","group":"1ec6144c.35129c","order":2,"width":0,"height":0,"passthru":true,"outs":"all","topic":"","min":0,"max":"100","step":1,"x":610,"y":220,"wires":[["499db5d2.bfb38c"]]},{"id":"26ff78f3.6ac588","type":"ui_text","z":"d2eeb423.09a668","group":"1ec6144c.35129c","order":5,"width":0,"height":0,"name":"","label":"Sliderの値","format":"{{msg.payload}}","layout":"row-spread","x":1010,"y":160,"wires":[]},{"id":"499db5d2.bfb38c","type":"change","z":"d2eeb423.09a668","name":"","rules":[{"t":"set","p":"val1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":160,"wires":[["26ff78f3.6ac588","f53ff9df.e0f938"]]},{"id":"111666fd.d82289","type":"change","z":"d2eeb423.09a668","name":"set 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":240,"wires":[["866635c1.680b98"]]},{"id":"6b69cbd.7411034","type":"change","z":"d2eeb423.09a668","name":"","rules":[{"t":"set","p":"val2","pt":"flow","to":"val1","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"val2","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":380,"wires":[["111666fd.d82289","ea33b9fb.3c5d18"]]},{"id":"ea33b9fb.3c5d18","type":"ui_text","z":"d2eeb423.09a668","group":"1ec6144c.35129c","order":6,"width":0,"height":0,"name":"","label":"OFF時の値","format":"{{msg.payload}}","layout":"row-spread","x":630,"y":380,"wires":[]},{"id":"df0f0067.3597b","type":"ui_button","z":"d2eeb423.09a668","name":"","group":"1ec6144c.35129c","order":3,"width":0,"height":0,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"green","icon":"","payload":"","payloadType":"str","topic":"","x":130,"y":180,"wires":[["84969563.cfd608"]]},{"id":"84969563.cfd608","type":"change","z":"d2eeb423.09a668","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"val2","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["866635c1.680b98"]]},{"id":"f53ff9df.e0f938","type":"ui_gauge","z":"d2eeb423.09a668","name":"","group":"1ec6144c.35129c","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1000,"y":260,"wires":[]},{"id":"1ec6144c.35129c","type":"ui_group","z":"","name":"","tab":"a87753cb.12868","order":1,"disp":true,"width":"6","collapse":false},{"id":"a87753cb.12868","type":"ui_tab","z":"","name":"ホーム","icon":"dashboard","disabled":false,"hidden":false}]

```

---
### 参考 ###
1. [Watson IoT Platform - Node-RED - フロー内で共有する変数など](https://qiita.com/egplnt/items/a95b14ee08136c6f02af)