※本記事は、NorthDetail Advent Calendar 2020の一環として投稿しています
メリークリスマス!
アドベントカレンダーも最終日のクリスマス、そしてクリスマスと言えばラズパイですよね!
ということで、 North Detail Advent Calendar 2020 の最後は SORACOM LTE-M Button for Enterprise を使ってクリスマスツリーを光らせる仕組みを Raspberry Pi で作ってみたいと思います。
2019年 / クリスマスの定番(?) Raspberry Pi と Philips Hue でクリスマスツリーをライティング
2018年 / クリスマスの定番(?) Raspberry PiとAlexaで光るクリスマスツリーを作る【動画あり】
2017年 / クリスマスの定番(?) Raspberry Piで光るクリスマスリースを作る【動画あり】
まずはどのような動きになるか、動画をご覧ください。
では、どうやって作ったのか見ていきましょう。
今回は SORACOM IoT ストアから、下記セットとして購入しました。
SORACOM LTE-M Button for Enterprise
LTE の SIM つきボタン。コンソールから登録するだけで、すぐに使える優れもの。
各自、好きなように作りましょう。
まず、ツリーを作成しましょう。
LEDライトの根本を切断して、導線を剥き出しにし、お手持ちのジャンパーワイヤーと接続します。
今回は簡単にマスキングテープで固定しています。
できたら、ツリーをLEDライトで飾りましょう。(今回は内側からLEDライトを通しています。)
Raspberry Pi のセットアップについては、こちらの記事などを参照してください。
SORACOM の IoT 体験キット 〜距離測定センサー〜 のハンズオンをやってみたらとても手軽に楽しめた
動作するようになったら、 Node-RED をセットアップします。 Raspberry Pi での導入する手順はこちらを参照します。
Raspberry Piで実行する / Node-RED User Group Japan
できたら、 Node-RED の画面へアクセスします。
Node-RED の画面が表示されたら、早速フローを作っていきましょう。
今回は、ボタンが押されると HTTP の POST メソッドで情報を受け取る必要があるので、 POST メソッドの待ち受け機能を作り、そこから内容によって LED の ON/OFF 制御を行なうようにします。
下記のようなフローとなります。
JSONだとこちらになります。(インポート可能です)
[{"id":"ccd3644d.b2f4a8","type":"tab","label":"フロー 2","disabled":false,"info":""},{"id":"215d630f.53a9dc","type":"http in","z":"ccd3644d.b2f4a8","name":"","url":"/nodered","method":"post","upload":false,"swaggerDoc":"","x":120,"y":60,"wires":[["45e2661c.d20bd8","bafda30c.e4893"]]},{"id":"45e2661c.d20bd8","type":"http response","z":"ccd3644d.b2f4a8","name":"","statusCode":"200","headers":{},"x":340,"y":60,"wires":[]},{"id":"bafda30c.e4893","type":"switch","z":"ccd3644d.b2f4a8","name":"","property":"payload.clickType","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":150,"y":140,"wires":[["d2b1d357.fa0be"],["bc3f9f57.974b1"],["ce397cd9.14cfe"]]},{"id":"cd2a4bdb.7d90a8","type":"rpi-gpio out","z":"ccd3644d.b2f4a8","name":"","pin":"7","set":"","level":"0","freq":"","out":"out","x":890,"y":220,"wires":[]},{"id":"90b6537b.05f87","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":130,"y":480,"wires":[["79d1df69.ca935"]]},{"id":"f5914ac5.4dfcf8","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":130,"y":440,"wires":[["79d1df69.ca935"]]},{"id":"3d7c4619.1d5c5a","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":540,"wires":[["ee4dc626.d20c28"]]},{"id":"ee4dc626.d20c28","type":"trigger","z":"ccd3644d.b2f4a8","name":"","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"500","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":360,"y":480,"wires":[["21345225.45c18e"]]},{"id":"79d1df69.ca935","type":"change","z":"ccd3644d.b2f4a8","name":"","rules":[{"t":"set","p":"incomeBlinking","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":380,"wires":[["a9c23d3a.f185"]]},{"id":"21345225.45c18e","type":"change","z":"ccd3644d.b2f4a8","name":"","rules":[{"t":"set","p":"timmingTrigger","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":420,"wires":[["a9c23d3a.f185"]]},{"id":"a9c23d3a.f185","type":"function","z":"ccd3644d.b2f4a8","name":"","func":"const incomeOn = flow.get('incomeOn')\nconst incomeBlinking = flow.get('incomeBlinking')\nconst timmingTrigger = flow.get('timmingTrigger')\n\nmsg.payload = incomeOn || (incomeBlinking && timmingTrigger)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":340,"wires":[["cd2a4bdb.7d90a8"]]},{"id":"6390180.84520e8","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":130,"y":340,"wires":[["5a1ce6d5.c3c458"]]},{"id":"5a1ce6d5.c3c458","type":"change","z":"ccd3644d.b2f4a8","name":"","rules":[{"t":"set","p":"incomeOn","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":340,"wires":[["a9c23d3a.f185"]]},{"id":"b7c9d4ce.362f58","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":130,"y":380,"wires":[["5a1ce6d5.c3c458"]]},{"id":"d2b1d357.fa0be","type":"change","z":"ccd3644d.b2f4a8","name":"type:1","rules":[{"t":"set","p":"incomeOn","pt":"flow","to":"true","tot":"bool"},{"t":"set","p":"incomeBlinking","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":180,"wires":[["a9c23d3a.f185"]]},{"id":"bc3f9f57.974b1","type":"change","z":"ccd3644d.b2f4a8","name":"type:2","rules":[{"t":"set","p":"incomeOn","pt":"flow","to":"false","tot":"bool"},{"t":"set","p":"incomeBlinking","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":280,"wires":[["a9c23d3a.f185"]]},{"id":"ce397cd9.14cfe","type":"change","z":"ccd3644d.b2f4a8","name":"type:3","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":190,"y":280,"wires":[["79d1df69.ca935","5a1ce6d5.c3c458"]]},{"id":"ccc0607f.836b7","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":390,"y":220,"wires":[["bc3f9f57.974b1"]]},{"id":"1cecfcb6.833133","type":"inject","z":"ccd3644d.b2f4a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":470,"y":120,"wires":[["d2b1d357.fa0be"]]}]
また、外部から POST メソッドを受け取れるように Ngrok をダウンロードし、起動します。
Ngrok のダウンロードサイトから、 Linux (ARM)
のURLをコピーし、 Raspberry Pi でダウンロードします。
pi@raspberrypi:~ $ curl -O https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12.4M 100 12.4M 0 0 1454k 0 0:00:08 0:00:08 --:--:-- 1641k
pi@raspberrypi:~ $ unzip ngrok-stable-linux-arm.zip
Archive: ngrok-stable-linux-arm.zip
inflating: ngrok
pi@raspberrypi:~ $ ls ngrok
ngrok
pi@raspberrypi:~ $ ./ngrok http 1880
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Session Expires 7 hours, 43 minutes
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://XXXXXXXXXXXX.ngrok.io -> http://localhost:1880
Forwarding https://XXXXXXXXXXXX.ngrok.io -> http://localhost:1880
起動後に表示される https://XXXXXXXXXXXX.ngrok.io
を使います。
本来なら SORACOM さんのサービス (Napterなど) で実現可能な構成だと思うのですが今回は確認までできなかったので、 Ngrok を使用しています。
SORACOM のコンソールから、 Beam へ Ngrok のURLを設定しましょう。
これで準備が整いました。
ボタンは、下記のような割り当てとしています。
1回押し | 常時点灯 |
2回押し | 点滅 |
長押し | 消灯 |
これを踏まえて、もう一度動画を確認してみてください。
きちんと動作しました!
SORACOM LTE-M Button は、手軽に使えるのでとても便利ですね。
また、Node-RED を使うことで簡単な Web サーバー + アプリケーションを作ることもできます。コーディングが苦手な方でも、色々と工夫することができそうです。
こういった道具をうまく使って、色々と遊んでみてください!