Intel Edisonで iotkit-commを使う:サーモスタットサンプルによるGetting Started

IoTIoT言うとりますけども.
という感じでIntel Edisonに提供されているiotkit-comm.
いかにも!という感じがするライブラリ名だけど,解説記事が全然ないので,ひとまずGettingStartedを訳しながら試してみた.
というわけで,公式ドキュメントを参考にして,話を進めます.

iotkit-comm Getting Started

概要

iotkit-commは以下の様な機能を提供します.これを使うと,クライアント,サーバによって構成される分散アプリケーションの開発ができるようになります.

  • ネットワークに接続されたデバイスの発見
  • ネットワークに接続されたデバイス同士での通信
  • ネットワークに接続されたデバイスとクラウド間での通信

Intel Edison向けのプラットフォームとして設計されていますが,他のプラットフォームでも同様に動作しています.
iotkit-commは,CとNode.js向けのライブラリが存在しますが,以下ではNode.js向けの説明をします.

このドキュメントの読み方

2つのセクションがあるので,目的に応じて利用します.

  • APIリファレンスを閲覧するためのセクションは,”Modules”セクションです.

    • “main”モジュールから読み始めることをおすすめします
  • APIの利用方法についてのチュートリアルを閲覧するためのセクションは,”Tutorial”セクションです.

※ このドキュメントは,Modulesが,Classes, Methods, Variablesのどのような論理構成により実現されるのかを示したものであり,あくまでドキュメントであることに注意してください.コードベースでiotkit-commを説明したものではありません.

このドキュメントでの前提条件

  • Edisonがインターネットアクセス可能であること
    • 前回のエントリを参考にWiFi設定を行いましょう.
  • SampleプログラムやコマンドがEdison上で動作可能であること.
    • 後で記述する,iotkit-commにおけるHelloWorldを試してみましょう.

iotkit-comm ドキュメント(Tutorial編)

Start Here

iotkit-commライブラリを使って開発を行うために,以下のように学習していきます.

  1. iotkit-commのインストール
  2. hello world”プログラムを書いて実行する(オプション)
  3. 付属のサンプルプログラムを実行する(オプション)
  4. もっと詳しいことを学ぶ(オプション)

1. iotkit-commライブラリのインストール

  • Intel Edisonデバイスにはもうインストール済みだからこの手順は必要ないです.

    • 他のプラットフォームへのインストール方法は後日公開予定.
  • ただし,以下の方法でIoT Developer Kit librariesに関するアップデートだけしておきましょう.

$ echo "src intel-iotdk http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf
$ opkg update
$ opkg upgrade

2. Write “hello world”プログラムを書いて実行する

  • “helloworld.js”というJavascriptファイルを作成し,以下のプログラムを記述します.
var iotkit = require(‘iotkit-comm’);
console.log(iotkit.sayhello());
  • 記述した”helloworld.js”の動作をターミナルで確認します.
$ node helloworld.js
# 以下の文字が出力される
>Hello Edison user!

3. サンプルプログラムの実行

  • ※ 公式ページのディレクトリ構成は最新ファームと異なります.以下では,2014/10/26現在での最新ファームである,edison-weekly_build_68_2014-09-08_13-49-07 でのディレクトリ構成を基に話を進めます.
  • この章ではサーモスタットプログラムを試してみましょう.

3-1. 利用するサンプルの概要と補足情報

いくつかの温度センサからのデータを受信するサーモスタットプログラムを試してみます.このプログラムは公式リファレンスでは以下のディレクトリにあるとされていますが,

  • /usr/share/iotkit-comm-js/examples

現在のファームでは以下のディレクトリに移動しています.

  • /usr/share/iotkit-comm/examples/node/distributed-thermostat

3-2. 温度センサの実行

$cd /usr/share/iotkit-comm/examples/node/distributed-thermostat
# 温度センサの起動
$ node temperature-sensor.js
# ターミナル上には特に何も出力されません.

3-3. サーモスタットの実行

  • 3-2とは異なるターミナルを開き,サーモスタットを実行します.
    • 1つのEdison上で実行する場合は,2つのターミナルを開き,それぞれのターミナルからsshでEdisonへ接続すると良いです.
$cd /usr/share/iotkit-comm/examples/node/distributed-thermostat
# サーモスタットの起動
$ node thermostat.js 

3-4. 動作の確認

サーモスタットと温度センサを同時に実行すると,温度センサ(temperature-sensor.js)から送信される温度データを,サーモスタット側(thermostat.js)で受信し,ターミナル上で以下の様な出力が得られます.

# 温度センサ1台数の時
Found new temperature sensor - 127.0.0.1:20408
Received sample temperature 90 from 127.0.0.1:20408
New average ambient temperature (cumulative): 45
Received sample temperature 79 from 127.0.0.1:20408
New average ambient temperature (cumulative): 56.333333333333336
Received sample temperature 67 from 127.0.0.1:20408
New average ambient temperature (cumulative): 59
Received sample temperature 83 from 127.0.0.1:20408
New average ambient temperature (cumulative): 63.8
Received sample temperature 72 from 127.0.0.1:20408
New average ambient temperature (cumulative): 65.16666666666667
Received sample temperature 62 from 127.0.0.1:20408
New average ambient temperature (cumulative): 64.71428571428571

新しいセンサが追加された際も,発見と通信が自動で行われます.

# 途中から温度センサを追加した時
# Found new temperature sensor という所で新しいセンサが検出されています
eceived sample temperature 79 from 127.0.0.1:20408
New average ambient temperature (cumulative): 74.21212121212122
Received sample temperature 63 from 127.0.0.1:20408
New average ambient temperature (cumulative): 73.88235294117646
Received sample temperature 71 from 127.0.0.1:20408
New average ambient temperature (cumulative): 73.8
Found new temperature sensor - 127.0.0.1:54187
Received sample temperature 73 from 127.0.0.1:20408
New average ambient temperature (cumulative): 73.77777777777777
Received sample temperature 61 from 127.0.0.1:54187
New average ambient temperature (cumulative): 73.43243243243244
Received sample temperature 78 from 127.0.0.1:20408
New average ambient temperature (cumulative): 73.55263157894737
Received sample temperature 88 from 127.0.0.1:54187
New average ambient temperature (cumulative): 73.92307692307692
# 温度センサとサーモスタットプログラムをそれぞれ別のEdisonから実行した時.
Received sample temperature 85 from 192.168.11.13:13304
New average ambient temperature (cumulative): 73.75862068965517
Received sample temperature 85 from 192.168.11.13:13304
New average ambient temperature (cumulative): 73.94915254237289

# さらにサーモスタットが動作するEdison上でも温度センサを動作させる
Found new temperature sensor - 127.0.0.1:54453
Received sample temperature 90 from 192.168.11.13:13304
New average ambient temperature (cumulative): 74.21666666666667
Received sample temperature 64 from 127.0.0.1:54453
New average ambient temperature (cumulative): 74.04918032786885
Received sample temperature 87 from 192.168.11.13:13304
New average ambient temperature (cumulative): 74.25806451612904
Received sample temperature 71 from 127.0.0.1:54453
New average ambient temperature (cumulative): 74.2063492063492

4. もっと詳しいことを学ぶ

以下の様な手順で学んで行こう,とされています.後日追記予定.

  1. server の書き方
  2. client の書き方
  3. Service Specifications と queriesについて(重要)
  4. iotkit-commを使って分散アプリケーションを書く方法
  5. cloudにデータをPublishする方法
  6. iotkit-commを利用したソースのUnitテスト方法(未公開)
  7. iotkit-commアーキテクチャについて(未公開)