2015-04-17

[.tested] TI CC2541 Sensor TagのデータをIBM Bluemixで処理するためのセンサーゲートウェイ マルチデバイス対応 [BX1]

今回は、以前ご紹介したTI CC2541 Sensor TagのデータをIBM Bluemixで処理するためのセンサーゲートウェイのマルチデバイス対応の実装方法についてご紹介します。


OBDN技術ブログによる動作検証は、該当するデバイスやソフトウェアの動作について、保証およびサポートを行うものではありません。
内容に関するご指摘などありましたら、ブログ記事の担当までご連絡下さい。

<検証環境>
OpenBlocks IoT BX1 Debian wheezy 7.8 / kernel 3.10.17-poky-edison

1. 事前準備


ネットワークへの接続、bluetoothの設定、開発環境および関連ツールのインストール、node.jsのインストールにつきましては前々回の記事を参照してください。

2. センサーゲートウェイソフトウェアの構築


ソフトウェアを構築するディレクトリを作成します。

# mkdir multi_device
# cd multi_device

今回必要なモジュールを導入します。
libbluetooth-devパッケージも必要なため、導入していない場合は合わせてインストールしてください。

# aptitude update
# aptitude install libbluetooth-dev
# npm install async
# npm install getmac
# npm install mqtt
# npm install sensortag

次に、ゲートウェイプログラムを作成します。
今回は温度データを取得し、Bluemixへ送信するサンプルです。

# vi multi_st2b.js

multi_st2b.js


var SensorTag = require('sensortag');
var async = require('async');
var mqtt = require('mqtt');
var getmac = require('getmac');

// constants
var u_port = "1883";
var pub_topic = "iot-2/evt/sample/fmt/json";
var qs_org = "quickstart";
var qs_host = "quickstart.messaging.internetofthings.ibmcloud.com";
var qs_type = "iotsample-ti-bbst";

// globals
var org = qs_org;
var type = qs_type;
var host = qs_host;
var deviceId;
var clientId;

getmac.getMac(function(err, macAddress) {
  if (err) throw err;
    console.log(macAddress);
    deviceId = macAddress.replace(/:/g, '').toLowerCase();
    clientId = "d:" + org + ":" + type + ":" + deviceId;
    console.log('MQTT clientId = ' + clientId);
    client = mqtt.createClient(u_port, host, {
    clientId : clientId,
    keepalive : 30
  });
  console.log('MAC address = ' + deviceId);
  console.log('Go to the following link to see your device data;');
  console.log('http://quickstart.internetofthings.ibmcloud.com/#/device/' + deviceId + '/sensor/');
});

function onDiscover(sensorTag) {
  console.log('discovered: ' + sensorTag.uuid + ', type = ' + sensorTag.type);

  sensorTag.on('disconnect', function() {
    console.log('disconnected! ' + sensorTag.uuid);
  });

  async.series([
      function(callback) {
        console.log('connectAndSetUp');
        sensorTag.connectAndSetUp(callback);
      },
      function(callback) {
        console.log('readDeviceName: ' + sensorTag.uuid);
        sensorTag.readDeviceName(function(error, deviceName) {
          console.log('\tdevice name = ' + deviceName);
          callback();
        });
      },
      function(callback) {
        console.log('enableIrTemperature');
        sensorTag.enableIrTemperature(callback);
      },
      function(callback) {
        setTimeout(callback, 2000);
      },
      function(callback) {
        sensorTag.on('irTemperatureChange', function(objectTemperature, ambientTemperature) {

          var tagData = {};
          tagData.d = {};

          tagData.d.deviceid = deviceId;
          tagData.d.uuid = sensorTag.uuid;
          tagData.d.rssi = sensorTag.rssi;
          tagData.d.proximity = sensorTag.proximity;
          tagData.d.objectTemperature = objectTemperature.toFixed(1);
          tagData.d.ambientTemperature = ambientTemperature.toFixed(1);

          tagData.toJson = function() {
            return JSON.stringify(this);
          };

          console.log(pub_topic, tagData.toJson());
          console.log(tagData.toJson());
          client.publish(pub_topic, tagData.toJson() );

          console.log(sensorTag.uuid);
            console.log('\tobject temperature = %d °C', objectTemperature.toFixed(1));
            console.log('\tambient temperature = %d °C', ambientTemperature.toFixed(1))
        });

        console.log('setIrTemperaturePeriod');
        sensorTag.setIrTemperaturePeriod(1000, function(error) {
          console.log('notifyIrTemperature');
          sensorTag.notifyIrTemperature(function(error) {
          });
        });
      },
      function(callback) {
        console.log('disableIrTemperature');
        sensorTag.disableIrTemperature(callback);
      },
    ]
  );
}

SensorTag.discoverAll(onDiscover);

3. センサーゲートウェイソフトウェアの起動


TI CC2541 SensorTagを複数用意し、上記で作成したJavascriptプログラムを起動します。

root@bx1:~/multi_device# node multi_st2b.js
34:95:db:28:41:0e
MQTT clientId = d:quickstart:iotsample-ti-bbst:3495db28410e
createClient is deprecated, use connect instead
MAC address = 3495db28410e
Go to the following link to see your device data;
http://quickstart.internetofthings.ibmcloud.com/#/device/3495db28410e/sensor/

ここでは2台で試してみました。それぞれのセンサータグのサイドボタンを押すと、以下のようなメッセージが表示され、BluemixへMQTTによるデータ送信を開始します。

discovered: 5c313ec119a5, type = cc2540
connectAndSetUp
discovered: 5c313ebff12e, type = cc2540
connectAndSetUp

4. Bluemix側の設定


Bluemix側は、前々回前回と同じテンプレートが利用できます。



今回は温度データのみですが、データを追加したい場合はインストールしたsensortagモジュール(node_modules/sensortag)にあるtest.jsなどを参照して、必要なデータを追加してください。

5. 最後に


今回は、TI CC2541 Sensor Tagを複数接続する例を紹介しました。
さらに詳しい情報が知りたい方は
https://github.com/sandeepmistry/node-sensortag
を参照してください。