Node-REDなraspberry piを外のイベントで使うときのtips

この記事は [Node-RED Advent Calendar 2018](https://qiita.com/advent-calendar/2018/nodered) 20日目の記事です。

# はじめに
Node-REDを使って動くものってたくさんありますね。
単体ケースで持ち歩き、外で動作させたとき
`このラズパイのIPがわからない！`ということがありませんか？（僕だけ？）
TJBotを外で紹介しよう！といろいろやってみたら、このほうが便利なのでは？
ということがいくつか出てきたので、紹介しようと思います。
TJBotに限らずRaspberry piをテザリング環境などお外で使うときに
参考になればと思います。

## TJBotというダンボールロボット製作キット
いきなりTJBotのことを紹介しましたが、ご存じない方も多いと思うので・・・。
IBMが公開するダンボールによるロボット製作キットで、Raspberry PI + Node-REDで動きます。
2018/12解禁となったTJBot zeroは海外で公開されているフルサイズダンボールキットをさらに小さく・スマートに作れるようになりました。
<blockquote class="twitter-tweet" data-lang="ja"><p lang="en" dir="ltr">Hello., World<a href="https://twitter.com/hashtag/TJBot?src=hash&amp;ref_src=twsrc%5Etfw">#TJBot</a> <a href="https://twitter.com/hashtag/TJBotzero?src=hash&amp;ref_src=twsrc%5Etfw">#TJBotzero</a> <a href="https://twitter.com/hashtag/BMXUG?src=hash&amp;ref_src=twsrc%5Etfw">#BMXUG</a> <a href="https://twitter.com/hashtag/IBMChampion?src=hash&amp;ref_src=twsrc%5Etfw">#IBMChampion</a> <a href="https://t.co/a5k0Z2PDkh">pic.twitter.com/a5k0Z2PDkh</a></p>&mdash; Shoichiro Sakaigawa (@l_s_s_l) <a href="https://twitter.com/l_s_s_l/status/1067814726908862465?ref_src=twsrc%5Etfw">2018年11月28日</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

[日本版 TJBot - プログラム可能なロボット (TJBot zero)]
(https://www.ibm.com/developerworks/jp/code/open/projects/tjbot/)
[自作AIロボットTJBot Zeroで遊んでみる](https://qiita.com/zuhito/items/cd9d8cfefab1fbacaf69)

<blockquote class="twitter-tweet" data-lang="ja"><p lang="ja" dir="ltr">IoT縛りの勉強会/SIer主催 SIerIoTLT vol8@サイボウズ<br>でTJBotのことを話ました。<a href="https://twitter.com/hashtag/BMXUG?src=hash&amp;ref_src=twsrc%5Etfw">#BMXUG</a> <a href="https://twitter.com/hashtag/noderedjp?src=hash&amp;ref_src=twsrc%5Etfw">#noderedjp</a> のみなさんに感謝です！<a href="https://twitter.com/hashtag/iotlt?src=hash&amp;ref_src=twsrc%5Etfw">#iotlt</a><br>Today, I speak about &quot;TJBot&quot; raspberry pi and Node-RED robot in JPN SIerIoTLT Vol8<a href="https://t.co/I91dBQGdmk">https://t.co/I91dBQGdmk</a></p>&mdash; Shoichiro Sakaigawa (@l_s_s_l) <a href="https://twitter.com/l_s_s_l/status/998938217620500481?ref_src=twsrc%5Etfw">2018年5月22日</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


<blockquote class="twitter-tweet" data-lang="ja"><p lang="ja" dir="ltr">TJBotもっと作り込みしたい課題いっぱいだけど、<br>ダンボールの愛嬌と、手を振る動作で<br>大変人気でした！<br><br>さらに進化させたいです！<br>今日の紹介動画はこちら<a href="https://t.co/EHjMfrINQs">https://t.co/EHjMfrINQs</a>
<br><a href="https://twitter.com/hashtag/iotlt?src=hash&amp;ref_src=twsrc%5Etfw">#iotlt</a> <a href="https://twitter.com/hashtag/tjbot?src=hash&amp;ref_src=twsrc%5Etfw">#tjbot</a> <a href="https://twitter.com/hashtag/IBMChampion?src=hash&amp;ref_src=twsrc%5Etfw">#IBMChampion</a> <a href="https://twitter.com/hashtag/BMXUG?src=hash&amp;ref_src=twsrc%5Etfw">#BMXUG</a> <a href="https://twitter.com/hashtag/noderedjp?src=hash&amp;ref_src=twsrc%5Etfw">#noderedjp</a></p>&mdash; Shoichiro Sakaigawa (@l_s_s_l) <a href="https://twitter.com/l_s_s_l/status/998931138109296641?ref_src=twsrc%5Etfw">2018年5月22日</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


## イベントに持ち込んで思うこと
- 携帯や家、職場などIPが変わる環境に持ち込んでもすぐdash-boardにつなぎたい！
- シャットダウンするためだけにSSHするのがちょっと不便

# やってみた
## 第１弾: HW的にIPを表示させる
最初は２行のLCDディスプレイにIPアドレスを表示させることからはじめました
どちらかというとLCDへ文字列を入れる方が手間でした。
IPアドレスの取得自体は[node-red-contrib-hostip](https://www.npmjs.com/package/node-red-contrib-hostip)を使ったのでノードへ入れるだけ。
WiFiでも、LAN経由でもIPアドレスを配列で格納してくれるので、これをLCDへ出力します。
![image.png](https://qiita-image-store.s3.amazonaws.com/0/126158/998511f1-ac36-7d8e-8970-90b12b5606f3.png)

```json
[{"id":"9637b1de.96eef","type":"i2clcd","z":"8dbb53ad.e1e3e","name":"1602 LCD","addr":"0x3f","x":739,"y":637,"wires":[["e7b70e64.2b84f"]]},{"id":"43e8d56a.1d1b9c","type":"inject","z":"8dbb53ad.e1e3e","name":"","topic":"init","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":179,"y":517,"wires":[["27837274.2ead6e"]]},{"id":"4c4fa36d.ccad7c","type":"hostip","z":"8dbb53ad.e1e3e","name":"Host IP","x":159,"y":637,"wires":[["3bbaf8b0.6cadd8"]]},{"id":"d26318d4.310478","type":"change","z":"8dbb53ad.e1e3e","name":"insertIPv4Address","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.0.address","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":549,"y":637,"wires":[["9637b1de.96eef"]]},{"id":"3bbaf8b0.6cadd8","type":"template","z":"8dbb53ad.e1e3e","name":"MyIP-topic","field":"topic","fieldType":"msg","format":"text","syntax":"plain","template":"line1","output":"str","x":349,"y":637,"wires":[["d26318d4.310478"]]},{"id":"e7b70e64.2b84f","type":"delay","z":"8dbb53ad.e1e3e","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":899,"y":637,"wires":[["ff9a0654.d6a1d8"]]},{"id":"27837274.2ead6e","type":"template","z":"8dbb53ad.e1e3e","name":"LCD-Clear","field":"topic","fieldType":"msg","format":"handlebars","syntax":"plain","template":"init","output":"str","x":169,"y":577,"wires":[["651f94fa.30160c"]]},{"id":"1eaeea15.b74766","type":"template","z":"8dbb53ad.e1e3e","name":"Hello-topic","field":"topic","fieldType":"msg","format":"handlebars","syntax":"plain","template":"line1","output":"str","x":169,"y":757,"wires":[["555a9471.436e4c"]]},{"id":"555a9471.436e4c","type":"template","z":"8dbb53ad.e1e3e","name":"Hello-body","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"Hello! TJBot","output":"str","x":349,"y":757,"wires":[["9131db36.af7a98"]]},{"id":"ab8b013c.657c3","type":"template","z":"8dbb53ad.e1e3e","name":"Hello-body2","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"from BMXUG","output":"str","x":349,"y":817,"wires":[["2beceaea.26aea6"]]},{"id":"2f33592a.270656","type":"template","z":"8dbb53ad.e1e3e","name":"Hello-topic2","field":"topic","fieldType":"msg","format":"handlebars","syntax":"plain","template":"line2","output":"str","x":169,"y":817,"wires":[["ab8b013c.657c3"]]},{"id":"9131db36.af7a98","type":"i2clcd","z":"8dbb53ad.e1e3e","name":"1602 LCD","addr":"0x3f","x":519,"y":757,"wires":[["ca99b866.5931a8"]]},{"id":"2beceaea.26aea6","type":"i2clcd","z":"8dbb53ad.e1e3e","name":"1602 LCD","addr":"0x3f","x":519,"y":817,"wires":[["48205721.a0f428"]]},{"id":"ca99b866.5931a8","type":"delay","z":"8dbb53ad.e1e3e","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":679,"y":757,"wires":[["2f33592a.270656"]]},{"id":"651f94fa.30160c","type":"i2clcd","z":"8dbb53ad.e1e3e","name":"1602 LCD","addr":"0x3f","x":339,"y":577,"wires":[["520f6062.91903"]]},{"id":"48205721.a0f428","type":"delay","z":"8dbb53ad.e1e3e","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":679,"y":817,"wires":[["27837274.2ead6e"]]},{"id":"520f6062.91903","type":"delay","z":"8dbb53ad.e1e3e","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":499,"y":577,"wires":[["4c4fa36d.ccad7c"]]},{"id":"ff9a0654.d6a1d8","type":"template","z":"8dbb53ad.e1e3e","name":"LCD-Clear","field":"topic","fieldType":"msg","format":"handlebars","syntax":"plain","template":"init","output":"str","x":169,"y":697,"wires":[["154ac4c3.3d4e9b"]]},{"id":"154ac4c3.3d4e9b","type":"i2clcd","z":"8dbb53ad.e1e3e","name":"1602 LCD","addr":"0x3f","x":339,"y":697,"wires":[["57b82a2f.f109e4"]]},{"id":"57b82a2f.f109e4","type":"delay","z":"8dbb53ad.e1e3e","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":519,"y":697,"wires":[["1eaeea15.b74766"]]},{"id":"1317081b.5ae5c8","type":"comment","z":"8dbb53ad.e1e3e","name":"LCDぱねる　ぐるぐる","info":"","x":139,"y":457,"wires":[]}]
```

## 第２弾： シャットダウン・再起動をノードから実行させる
止めるためだけにSSHするのはちょっと面倒だな（どうせデモでNode-REDのダッシュボード開いているし）」
ということで、停止ボタンを作りました
![image.png](https://qiita-image-store.s3.amazonaws.com/0/126158/2a0e5b17-ad6f-4541-5968-7704bb12b5dd.png)
なんてことはないです。ただのexecノードで

```bash
sudo reboot
```

```bash
sudo poweroff
```

を実行するinjectを用意するだけです。

```json
[{"id":"c6837e27.f35bf","type":"comment","z":"8dbb53ad.e1e3e","name":"とめる","info":"","x":70,"y":720,"wires":[]},{"id":"24608ec9.643b42","type":"inject","z":"8dbb53ad.e1e3e","name":"shutdown!","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":760,"wires":[["7b44cde8.a71d24"]]},{"id":"7b44cde8.a71d24","type":"exec","z":"8dbb53ad.e1e3e","command":"sudo poweroff #","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"turn off Pi","x":314,"y":759,"wires":[[],[],[]]},{"id":"99ab5cb8.eb6cf","type":"exec","z":"8dbb53ad.e1e3e","command":"sudo reboot #","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"reboot Pi","x":314,"y":859,"wires":[[],[],[]]},{"id":"11132c53.f8ed24","type":"inject","z":"8dbb53ad.e1e3e","name":"reboot!","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":130,"y":860,"wires":[["99ab5cb8.eb6cf"]]},{"id":"576e0835.f0c5e8","type":"comment","z":"8dbb53ad.e1e3e","name":"さいきどう","info":"","x":80,"y":820,"wires":[]}]
```

# ふりかえり
## IPアドレスの認知方法をもっと面白くする
- LCD無くてもできるようにできる
- 音声でしゃべる
- スマホとかなにかに通知する(MQTTでなにかする/slack/メール)

というのもいろいろ考えられそうですね

まだまだ不便に思うこと、こんなことしたらいいかな、
という小ネタをまとめて行きたいと思います。