26
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(ほぼ)スマホ(だけ)を使ってIoTを始めてみよう~応用アプリを作る~

Last updated at Posted at 2016-06-13

#概要
「(ほぼ)スマホ(だけ)を使ってIoTを始めてみよう」の続きです
Node-REDのサンプルアプリをインポートして、IoTアプリをもう少し高度にしてみました。

//なんか意地になってスマホだけで操作しています。

// 20160616 datapalooza@秋葉原で紹介したデモアプリはここの手順で作れます。

// 201610 画面キャプチャを修正しました。

#背景(大事なことなので毎回言います)
面白そうな話を聞いて「へー、やってみたい」と思っても、家に帰って実際にやるかは気分次第。
気持ちが盛り上がっているうちに、スマホで!今すぐ!タダで!始めましょう。

#前提

  • BluemixのIoTアプリを作っていること。まだの人はここを実施してください
  • Twillioの無料アカウントを作っていること(+1で始まる米国番号をとってください)まだの人はhttps://www.twilio.com/try-twilioとかから作成してください。

#用意するもの

  • スマホ(センサーとして使います)
  • BluemixアカウントとBluemix IoT アプリ
  • Twillio電話番号(+1 で始まる米国番号じゃないとだめ
  • ガラケー(この電話番号をTwilioに登録。上記スマホと兼用でもOK)
  • パソコン(複雑な操作とか実行結果の確認とか)

#全体の手順

  1. サンプルコードをインポートする(スマホまたはパソコンで実施)
  2. Twillioアカウント情報を設定する(スマホまたはパソコンで実施)
  3. スマホをIoTセンサーに仕立てる(スマホでURLにアクセスするだけ)
  4. スマホセンサー情報を流し込む(スマホまたはパソコンで実施)
  5. スマホを振ってみる

#細かな手順

サンプルコードをインポートする

  1. 画面の三本線アイコンをクリックして、「Import」>「Clipboard」を選択し、ダイアログ画面に以下のフロー(プログラム)をコピペします。
    ここにも詳しい手順があります。

フロー(プログラム)(datapaloozaで紹介したものです。):

[{"id":"96a0694f.dbc0e8","type":"twilio-api","z":"dd7792a3.471f1","sid":"AC79edbcb542a2f88c8341493f625a9d7c","from":"+17201112222","name":"tel01"},{"id":"95f749e.26d6eb8","type":"ibmiot in","z":"dd7792a3.471f1","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"465711112222","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT App In","service":"quickstart","allDevices":false,"allApplications":false,"allDeviceTypes":true,"allEvents":true,"allCommands":false,"allFormats":false,"x":140,"y":120,"wires":[["3a1b5143.5b8cbe"]]},{"id":"3a1b5143.5b8cbe","type":"function","z":"dd7792a3.471f1","name":"X方向の加速度を抽出","func":"return {payload:msg.payload.d.accelX};","outputs":1,"noerr":0,"x":360,"y":120,"wires":[["407819d.71c45e8"]]},{"id":"407819d.71c45e8","type":"function","z":"dd7792a3.471f1","name":"直近10件の情報に注目","func":"if ( !context.global.array ) {\n    context.global.array = new Array (10);\n}\nvar shifted = context.global.array.shift();\n\ncontext.global.array.push(msg.payload);\n\nmsg.array = context.global.array;\nmsg.length = context.global.array.length;\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":180,"wires":[["783f89bb.be8348"]]},{"id":"783f89bb.be8348","type":"function","z":"dd7792a3.471f1","name":"移動平均","func":"var sum = 0, average = 0;\n    for (i=0; i<msg.length; i++) {\n        sum = sum + msg.array[i];\n}\nmsg.sum = sum;\naverage = sum / msg.length;\nmsg.average = average;\nreturn msg;\n","outputs":1,"noerr":0,"x":320,"y":240,"wires":[["89fb04c3.8b0e28"]]},{"id":"95789723.1e1978","type":"twilio out","z":"dd7792a3.471f1","service":"_ext_","twilio":"96a0694f.dbc0e8","from":"","number":"+818012345678","name":"保守担当者携帯にSMS","x":620,"y":300,"wires":[]},{"id":"4b2f8afe.d04f04","type":"comment","z":"dd7792a3.471f1","name":"センサーからのデータ受信","info":"","x":130,"y":60,"wires":[]},{"id":"6362539c.c0339c","type":"comment","z":"dd7792a3.471f1","name":"データ解析し異常振動を検知する","info":"","x":390,"y":60,"wires":[]},{"id":"8b94af1e.18b08","type":"comment","z":"dd7792a3.471f1","name":"解析結果の利用","info":"","x":620,"y":60,"wires":[]},{"id":"89fb04c3.8b0e28","type":"switch","z":"dd7792a3.471f1","name":"異常な振動が発生","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"100","vt":"str"}],"checkall":"true","outputs":1,"x":350,"y":300,"wires":[["95789723.1e1978"]]}]

こんなかんじ
import01.png
simple_flow.png

Twillioアカウント情報を設定する

  1. Twillioにログインし、「Account SID」と「Auth Token」をメモする。
    twillio.png

  2. Node-REDのTwillioフローをダブルタップして、設定画面のTwillioID欄の鉛筆アイコンをタップする。
    simple_flow02.png

  3. SMSの送信先電話番号を指定し、(Twillioトライアルの場合は申込時の電話番号のみ指定可能です)鉛筆アイコンを押す。
    twillio_auth.png

  4. 認証情報に、1. の情報を設定して「Update」を押す
    twillio_token.png

スマホをIoTセンサーに仕立てる

ここの手順と同じ。

  1. スマートフォンで、ここにアクセス:http://phonesensor.mybluemix.net/
    QRcode_senser.gif

  2. スマホのセンサーIDをメモしておく
    「d:quickstart:phone-sensor:[スマホのセンサーID]」
    sensor03.png

スマホの送ったセンサーデータをIoTアプリに流し込む

ここの手順と同じ。

  1. IoTアプリのURLにアクセスし、Node-REDのフロー編集画面を開きます。
    「"アプリ名".mybluemix.net」

  2. 青い入力フロー「IBM IoT App In」をダブルタップして、設定画面を開きます。設定画面のDeviceIDに、前項で確認したスマホのセンサーIDを記入し、OKを押して設定画面を閉じます。
    simple_flow03.png
    flow_edit_device.png

  3. フロー編集画面右上の「Deploy」 ボタンを押して、アプリケーションを更新します。
    simple_flow04.png

動かしてみる

スマホをガシガシ振ってみよう。

何も起こらなかったら?

黄色のノードをダブルタップすると、異常判定の閾値が変更できます。実機の振動レベルに合わせて調整してみましょう。
simple_flow05.png

もっと細かく挙動を見たい方はこっちのフローもどうぞ。

フロー(プログラム)

[{"id":"60232d75.006ac4","type":"twilio-api","z":"6d1fa5a.3378c5c","sid":"AC79edbcb542a2f88c8341493f625a9d7c","from":"+17201112222","name":"tel01"},{"id":"7849ddc3.9ca304","type":"ibmiot in","z":"6d1fa5a.3378c5c","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"465711112222","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT App In","service":"quickstart","allDevices":false,"allApplications":false,"allDeviceTypes":true,"allEvents":true,"allCommands":false,"allFormats":false,"x":105,"y":123,"wires":[["606759e6.4430f8","e8117059.2ce46"]]},{"id":"e8117059.2ce46","type":"debug","z":"6d1fa5a.3378c5c","name":"device data","active":false,"console":"false","complete":"true","x":219,"y":187,"wires":[]},{"id":"606759e6.4430f8","type":"function","z":"6d1fa5a.3378c5c","name":"X方向の加速度を抽出","func":"return {payload:msg.payload.d.accelX};","outputs":1,"noerr":0,"x":344,"y":113,"wires":[["bf51b264.252"]]},{"id":"bf51b264.252","type":"function","z":"6d1fa5a.3378c5c","name":"直近10件の情報に注目","func":"if ( !context.global.array ) {\n    context.global.array = new Array (10);\n}\nvar shifted = context.global.array.shift();\n\ncontext.global.array.push(msg.payload);\n\nmsg.array = context.global.array;\nmsg.length = context.global.array.length;\nreturn msg;","outputs":1,"noerr":0,"x":451,"y":190,"wires":[["5c86e402.c695ec","289e5916.6eaf56"]]},{"id":"5c86e402.c695ec","type":"debug","z":"6d1fa5a.3378c5c","name":"","active":false,"console":"false","complete":"array","x":672,"y":190,"wires":[]},{"id":"289e5916.6eaf56","type":"function","z":"6d1fa5a.3378c5c","name":"移動平均","func":"var sum = 0, average = 0;\n    for (i=0; i<msg.length; i++) {\n        sum = sum + msg.array[i];\n}\nmsg.sum = sum;\naverage = sum / msg.length;\nmsg.average = average;\nreturn msg;\n","outputs":1,"noerr":0,"x":442,"y":247,"wires":[["61c9b961.c3b0f8","d4f3e455.6a7228","3e8d7ba7.8215d4"]]},{"id":"61c9b961.c3b0f8","type":"debug","z":"6d1fa5a.3378c5c","name":"","active":false,"console":"false","complete":"average","x":684,"y":248,"wires":[]},{"id":"d4f3e455.6a7228","type":"function","z":"6d1fa5a.3378c5c","name":"分散","func":"var variance = 0;\nfor (i=0; i<msg.length; i++) {\n    variance = variance + Math.pow(msg.array[i] - msg.average, 2);\n}\nvariance = variance / msg.length;\nmsg.variance = variance;\nreturn msg;\n","outputs":1,"noerr":0,"x":638,"y":307,"wires":[["1b44a62a.bf1cba","5316a80d.307dd8"]]},{"id":"1b44a62a.bf1cba","type":"debug","z":"6d1fa5a.3378c5c","name":"","active":false,"console":"false","complete":"variance","x":818,"y":308,"wires":[]},{"id":"5316a80d.307dd8","type":"function","z":"6d1fa5a.3378c5c","name":"標準偏差","func":"var standard_deviation = 0;\nstandard_deviation = Math.sqrt(msg.variance);\nmsg.std = standard_deviation;\nreturn msg;","outputs":1,"noerr":0,"x":648,"y":365,"wires":[["c6fe0020.0f524"]]},{"id":"c6fe0020.0f524","type":"debug","z":"6d1fa5a.3378c5c","name":"","active":false,"console":"false","complete":"std","x":798,"y":365,"wires":[]},{"id":"eb358ae8.2bc138","type":"twilio out","z":"6d1fa5a.3378c5c","service":"_ext_","twilio":"60232d75.006ac4","from":"","number":"+818012345678","name":"会社携帯にSMS","x":739,"y":591,"wires":[]},{"id":"8238ff41.450d8","type":"comment","z":"6d1fa5a.3378c5c","name":"センサーからのデータ受信","info":"","x":167,"y":69,"wires":[]},{"id":"73688c45.cc67e4","type":"comment","z":"6d1fa5a.3378c5c","name":"データ解析し振動を検知する","info":"","x":544,"y":72,"wires":[]},{"id":"8bc0c739.116ac8","type":"comment","z":"6d1fa5a.3378c5c","name":"解析結果の反映","info":"","x":734,"y":494,"wires":[]},{"id":"3e8d7ba7.8215d4","type":"switch","z":"6d1fa5a.3378c5c","name":"継続して振動が発生","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"30","vt":"str"}],"checkall":"true","outputs":1,"x":535,"y":486.5,"wires":[["8f282437.7aa858","eb358ae8.2bc138"]]},{"id":"8f282437.7aa858","type":"debug","z":"6d1fa5a.3378c5c","name":"","active":true,"console":"false","complete":"payload","x":730,"y":540,"wires":[]}]

flow2_01.png

26
25
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
26
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?