Raspberry Pi ZeroにNode-REDを入れてlittleBits Arduinoモジュールと連携するメモ

私も登壇した SORACOM UG #0 で古城さんがNode-REDの件で登壇していてとてもやりたくなりまして、Raspberry Pi ZeroにNode-REDを入れてlittleBits Arduinoと連携するメモです。

古城さんのはこのような登壇内容でした。
 

 
こちらの資料も勉強になりました。
 

 
もちろん自力でコード書くのもいいですが、やはりGUIで直感的にやれる機構というのは手にしておきたいので、ぜひやってみたい。

インストールしていく

Raspbian JessieにNodeJSが動く前提で、インストールしていきます。私の場合は、nodebrewを入れてv0.10で行っています(結構古い)

こちらのとおりに。

Node-RED : Raspberry Pi

sudo apt-get update

Node-REDをいれます。

sudo apt-get install nodered

あら。最新版でした。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_2

Menu>プログラミング>NodeREDで起動します。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_3

コンソールが開いて起動します。あっさり。すごい。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_4

Raspberry Pi Zero内では localhost::1880 無事起動しました。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_5

host名.local:1880でネットワーク内からもアクセス可能です。こちらのほうが通常のPCから行えるので便利です。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_6

littleBits Arduinoモジュールとの連携

ごくナチュラルにserial出力 ノードが入っていたので、つないでみます。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_7

littleBits Arduinoモジュールの動作はそろそろlittleBits Arduinoモジュール使用時のルールを決めて使いやすくしておくメモの動きをコンパイルしておきます。

このように、littleBitsとRaspberry Pi Zeroをつないでみます。littleBitsのかたまりよりZeroちっちゃい!

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_8

しくみ

outputというURLを叩いたら、serial出力に対してmsg.payload = “2”を送信しています。2の文字列で出力がつきます。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_9

Node-REDだと繋いだツリーがフロー図としてそのまま使えるのですばらしい。

/output でHTTP受信するようにする設定

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_10

/output でHTTP受信するようにする設定は以下のとおりです。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_11

serial出力に対してmsg.payload = “2”を送信する設定

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_12

serial出力に対してmsg.payload = “2”を送信する設定は function ノードを使います。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_13

serial出力する設定

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_14

serial出力する設定は以下のとおりです。serial ノードをクリックして鉛筆ボタンを押して編集します。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_15

設定は、Raspberry Piが認識しているシリアルポートを探しつつ、

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_16

入力します。

Settingはnode-serialpotなどで使う設定を踏襲しました。

raspberry-pi-zero-meets-littlebits-arduino-with-nodered_17

serial出力の具体的な使い方については、最初よくわからなかったのですが、以下の記事がとても参考になりました。

Using Node-Red with Ciseco LLAP devices « Andy's Life

早速動かしてみます

では早速動かしてみます。

localhost:1880/output あるいは host名.local:1880/output をたたいてみると・・・

無事動きました!

おわりに

ということで、Raspberry PiとlittleBitsをNode-REDでつないでみました。

Node-REDよいですね。

最初からシリアル接続が普通に使えるのに驚きましたが、なんといってもインストールの簡単さと、直感的に機能が作れるので、プロトタイプ制作や、IFTTTよりもより突っ込んだ形でいろいろなAPIとデバイスの連携をするときに効果を発揮しそうです。

また、littleBitsのパーツをつないで作っていく考え方とNode-REDのGUIの考え方の相性がとてもよさそうです!

それでは、よき littleBits & Node-RED Life を!