LoginSignup
6
3

More than 5 years have passed since last update.

enebularでクリチカした

Last updated at Posted at 2018-12-17

これは enebular Adevent Calendar 2018 の17日目参加記事です。
こんにちは。普段は野良ハックチームで活動している まつした と申します。
この記事は enebular developer Meetup Vol.6でLTしたenebularでクリスマスツリーを光らせたデモの詳細になります。

IMB_HUTQOc.GIF

クリスマスツリーを光らせる

IoTのHello WorldといえばLチカ(LEDをチカチカ)、AdventCalendarといえばクリチカ(クリスマスツリーをチカチカ)です。

昨年のIoTLT Advent Calndar でも @ukk0 さんが
当Qiita記事がいいねされたら電飾が光りまくるX'mas Internet of Tree をつくる という素晴らしい記事をアップしています。今年もドンキで同じ電飾を売っていましたので enebular と obniz を使っておうちでクリチカしましょう!

IMG_0503.JPG

作ったもの

今回はツイッターで「enebular」というキーワードを検知すると点灯間隔が変わるクリスマスツリーを作りました。2種類の電飾を使って通常時とキーワード検知時のループ間隔を変えています。

なぜツイートすると光るのか?

勉強会などで登壇する場合には、聞いている方の顔色などを見ながら受けているのか?つまらないと思っているのか?などと考えながら話す内容を変えたりするわけですが、会場は冷めてPCで他の仕事をしている…と感じても、実はtwitter上で盛り上がっているなどということはよくあります。
それを知る方法としてtwitterを別画面見ながら話したり、プレゼン画面自体にコメントを表示する方法もよく使われています。
しかし、プレゼン自体に影響を与えず、かつ、さりげなく反応をしる方法としてインテリア(12月ならばクリスマスツリー)の光り方で知ることができるのでは無いかと考えました。

システム構成

enebularとobnizを使っていて構成は以下の通りです。

20181213_enebular_Meetup#6_Matsushita.jpg

点灯の仕様

こんな感じです。

状態 点灯間隔 点灯の様子
通常時 2.0秒 チカチカ2.GIF
ツイート検知時 0.3秒 チカチカ1.GIF

配線

今回は2種類(青の電飾/緑と黄のMIX電飾)をそれぞれobnizのio0/io1、io2/io3に接続しています。
先に紹介したukk0さんの記事にある通り、順方向と逆方向で半数づつのLEDが光る作りになっているのですが、今回は一方向のみ利用します。逆に言えばどちらに挿してもとりあえず光ります。
今回はモバイルバッテリーからの給電で2本を接続して光らせる事ができました。たまにlow voltageが出ていたので試していませんが2本が限界かもしれないです。

enebular(Node-RED)のフロー

2つの点灯パターンは点灯時間が違うだけですが、異なる方法で点灯のループを実現しています。(後述)

flow.png

フローは以下の通りですがobniz-idは自分のものに書き換えてください。

[{"id":"5d093c92.1230c4","type":"inject","z":"527748fe.1cbb48","name":"着く","topic":"","payload":"[{\"io0\":true, \"io1\":false, \"io2\":true, \"io3\":false}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":130,"y":120,"wires":[["e025c711.836418","864aee03.2a11a"]]},{"id":"8e02c63b.9153c8","type":"inject","z":"527748fe.1cbb48","name":"消す","topic":"","payload":"[{\"io0\":false, \"io1\":false, \"io2\":false, \"io3\":false}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":130,"y":160,"wires":[["864aee03.2a11a"]]},{"id":"864aee03.2a11a","type":"websocket out","z":"527748fe.1cbb48","name":"obniz ws","server":"","client":"448267c6.5701a8","x":800,"y":300,"wires":[]},{"id":"7a2120c2.45cd2","type":"delay","z":"527748fe.1cbb48","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":360,"wires":[["e025c711.836418"]]},{"id":"e025c711.836418","type":"switch","z":"527748fe.1cbb48","name":"交互実行","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":260,"y":260,"wires":[["a04cfe17.77d05"],["2eef695f.2e97c6"]]},{"id":"a04cfe17.77d05","type":"change","z":"527748fe.1cbb48","name":"ON:0/1 OFF:2/3","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"io0\":true, \"io1\":false, \"io2\":false, \"io3\":false}]","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":260,"wires":[["864aee03.2a11a","5b463895.2f99b8"]]},{"id":"2eef695f.2e97c6","type":"change","z":"527748fe.1cbb48","name":"OFF:0/1 ON:2/3","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"io0\":false, \"io1\":false, \"io2\":true, \"io3\":false}]","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":300,"wires":[["864aee03.2a11a","8e8b2b3e.7b49b8"]]},{"id":"3c02b795.9aecb8","type":"inject","z":"527748fe.1cbb48","name":"起動","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":"","x":130,"y":260,"wires":[["e025c711.836418"]]},{"id":"5b463895.2f99b8","type":"change","z":"527748fe.1cbb48","name":"set false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":260,"wires":[["7a2120c2.45cd2"]]},{"id":"8e8b2b3e.7b49b8","type":"change","z":"527748fe.1cbb48","name":"set true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":300,"wires":[["7a2120c2.45cd2"]]},{"id":"b1311ea0.359c7","type":"debug","z":"527748fe.1cbb48","name":"デバッグ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":580,"y":480,"wires":[]},{"id":"a98f146a.eab518","type":"websocket in","z":"527748fe.1cbb48","name":"obniz ws","server":"","client":"448267c6.5701a8","x":300,"y":480,"wires":[["b1311ea0.359c7"]]},{"id":"fd028648.d19fa8","type":"http in","z":"527748fe.1cbb48","name":"","url":"/obniz_tree","method":"post","upload":false,"swaggerDoc":"","x":140,"y":440,"wires":[["b1311ea0.359c7","8cf7605f.a836c","113109b4.88d8d6"]]},{"id":"8cf7605f.a836c","type":"http response","z":"527748fe.1cbb48","name":"","statusCode":"","headers":{},"x":170,"y":480,"wires":[]},{"id":"113109b4.88d8d6","type":"change","z":"527748fe.1cbb48","name":"チカチカ","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"io\":{\"animation\":{\"name\":\"animation-1\",\"status\":\"loop\",\"states\":[{\"duration\":300,\"state\": {\"io0\":true,\"io1\":false,\"io2\":false,\"io3\":false}},{\"duration\":300,\"state\":{\"io0\":false,\"io1\":false,\"io2\":true,\"io3\":false}}]}}}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":440,"wires":[["864aee03.2a11a","9eb77f2c.69213"]]},{"id":"bae810a.c91caf","type":"inject","z":"527748fe.1cbb48","name":"チカチカ","topic":"","payload":"[{\"io\":{\"animation\":{\"name\":\"animation-1\",\"status\":\"loop\",\"states\":[{\"duration\":300,\"state\": {\"io0\":true,\"io1\":false,\"io2\":false,\"io3\":false}},{\"duration\":300,\"state\":{\"io0\":false,\"io1\":false,\"io2\":true,\"io3\":false}}]}}}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":360,"y":80,"wires":[["864aee03.2a11a"]]},{"id":"704dfabc.c2fe04","type":"inject","z":"527748fe.1cbb48","name":"チカチカ停止","topic":"","payload":"[{\"io\":{\"animation\":{\"name\":\"animation-1\",\"status\":\"pause\"}}}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":370,"y":160,"wires":[["864aee03.2a11a"]]},{"id":"59dc75fb.4c32ac","type":"inject","z":"527748fe.1cbb48","name":"チカチカ再開","topic":"","payload":"[{\"io\":{\"animation\":{\"name\":\"animation-1\",\"status\":\"resume\"}}}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":370,"y":120,"wires":[["864aee03.2a11a"]]},{"id":"9eb77f2c.69213","type":"delay","z":"527748fe.1cbb48","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":440,"y":440,"wires":[["f3c0fdf5.b1b2f"]]},{"id":"f3c0fdf5.b1b2f","type":"change","z":"527748fe.1cbb48","name":"チカチカ停止","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"io\":{\"animation\":{\"name\":\"animation-1\",\"status\":\"pause\"}}}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":440,"wires":[["864aee03.2a11a"]]},{"id":"134fca02.e511c6","type":"comment","z":"527748fe.1cbb48","name":"デバッグ用","info":"","x":120,"y":80,"wires":[]},{"id":"71d61f2e.270c5","type":"comment","z":"527748fe.1cbb48","name":"通常チカチカ","info":"","x":130,"y":220,"wires":[]},{"id":"c30527d4.be4b58","type":"comment","z":"527748fe.1cbb48","name":"早いチカチカ","info":"","x":130,"y":380,"wires":[]},{"id":"448267c6.5701a8","type":"websocket-client","z":"","path":"wss://2.ws.obniz.io/obniz/xxxx-xxxx/ws/1","tls":"","wholemsg":"false"}]

チカチカのループ処理

enebular(Node-RED)は意外とループ処理が面倒です。(感じ方には個人差があります)
obnizにはIOをループさせたい時にioAnimationという便利な機能があります。IOのON/OFF繰り返しの内容と間隔を設定すると繰り返し実行してくれるので、クリスマスツリーLEDにはとても便利です。今回はツイート検知時の300ms間隔ループは以下のJSONで実行しています。

[
    {
        "io": {
            "animation": {
                "name": "animation-1",
                "status": "loop",
                "states": [
                    {
                        "duration": 300,
                        "state": {
                            "io0": true,
                            "io1": false,
                            "io2": false,
                            "io3": false
                        }
                    },
                    {
                        "duration": 300,
                        "state": {
                            "io0": false,
                            "io1": false,
                            "io2": true,
                            "io3": false
                        }
                    }
                ]
            }
        }
    }
]

ハマりがちなポイント

Websocketでobnizと連携する

今回、enebularとobnizをWebsocketで連携させましたが、その場合には注意が必要です。
obnizのWebsocket APIの実体はリダイレクト先にあるのですが、enebular(Node-RED)はリダイレクトに対応していないため、APIとしてリダイレクト先を指定する必要があります。
@hmaruyamaenebularでobnizをLチカしてみた を参考にさせていただきました。

obnizの公式ドキュメントが間違っている

先に書いた通りioAnimationはとても便利なのですが、サンプルコードのJSONが間違っていてコピペしたら動きませんでした。よく見るとanimation属性が意味なく二重になっていてあからさまに怪しいのですが、何も考えずにコピペしてはいけませんね。
ちなみに、この間違いをobnizのサポートが把握してから、10分後には修正が終わっていました。obnizのサポートは神です!

スクリーンショット 2018-12-14 16.23.30.png

Twitter APIの申請が厳しすぎる

すでにご存じの方も多いと思いますが、現在TwitterのAPI利用申請がとても厳しくなっており承認までにかなり時間がかかります。アイデアを思いついてから申請したので、当然のごとく間に合わず現状は泣く泣くIFTTTからWebhook連携です…

実際に使ってみて

実際に enebular developer Meetup Vol.6 でLTさせていただきました。
ツイートされるとクリスマスツリーの点灯で分かってとても嬉しいのですが、それ以上に点灯してない時に盛り上がっていないのか?という方が気になってしまいました 笑
あとクリスマスツリーを持っていくとみんな写真とか動画を撮ってくれてうれしいですね!

View this post on Instagram

クリスマスツリーをチカらせる #enebular

n0bisukeさん(@n0bisuke)がシェアした投稿 - 2018年12月月13日午前2時50分PST

それではenebularでよきクリスマスを!!!

6
3
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
6
3