node.jsによるTIセンサーの受信

OpenBlocks IoT BX1とインテルEdison KitでTI社製のCC2541 SensorTagを使用するためにnode-sensortagを使用します。

node-sensortagを使用することで以下の情報が取得できます。

IR Temperature Sensor(温度)

Accelerometer(加速度)

Humidity Sensor(湿度)

Magnetometer(磁気)

Barometric Pressure Sensor(気圧)

Gyroscope(ジャイロ)

 

1. node-sensortagのインストール

# npm install node-sensortag

 

2. 受信プログラム

var async = require('async');
var SensorTag = require("sensortag");

SensorTag.discover(function(sensorTag) {
  sensorTag.on('disconnect', function() {
    console.log('disconnected!');
    process.exit(0);
  });
  console.log('connect');
  sensorTag.connect(function() {

    console.log('discoverServicesAndCharacteristics');
    sensorTag.discoverServicesAndCharacteristics(function() {
      console.log('enableIrTemperature');
      sensorTag.enableIrTemperature(function() {
        console.log('enableAccelerometer');
        sensorTag.enableAccelerometer(function() {
          console.log('enableHumidity');
          sensorTag.enableHumidity(function() {
            console.log('enableMagnetometer');
            sensorTag.enableMagnetometer(function() {
              console.log('enableBarometricPressure');
              sensorTag.enableBarometricPressure(function() {
                console.log('enableGyroscope');
                sensorTag.enableGyroscope(function() {

                  setInterval(function() {

                    async.series([
                      function(callback) {
                        console.log('readDeviceName');
                        sensorTag.readDeviceName(function(deviceName) {
                          console.log('\tdevice name = ' + deviceName);
                          callback(null, deviceName);
                        });
                      },
                      function(callback) {
                        console.log('readSystemId');
                        sensorTag.readSystemId(function(systemId) {
                          console.log('\tsystem id = ' + systemId);
                          callback(null, systemId);
                        });
                      },
                      function(callback) {
                        console.log('readSerialNumber');
                        sensorTag.readSerialNumber(function(serialNumber) {
                          console.log('\tserial number = ' + serialNumber);
                          callback(null, serialNumber);
                        });
                      },
                      function(callback) {
                        console.log('readFirmwareRevision');
                        sensorTag.readFirmwareRevision(function(firmwareRevision) {
                          console.log('\tfirmware revision = ' + firmwareRevision);
                          callback(null, firmwareRevision);
                        });
                      },
                      function(callback) {
                        console.log('readHardwareRevision');
                        sensorTag.readHardwareRevision(function(hardwareRevision) {
                          console.log('\thardware revision = ' + hardwareRevision);
                          callback(null, hardwareRevision);
                        });
                      },
                      function(callback) {
                        console.log('readSoftwareRevision');
                        sensorTag.readHardwareRevision(function(softwareRevision) {
                          console.log('\tsoftware revision = ' + softwareRevision);
                          callback(null, softwareRevision);
                        });
                      },
                      function(callback) {
                        console.log('readManufacturerName');
                        sensorTag.readManufacturerName(function(manufacturerName) {
                          console.log('\tmanufacturer name = ' + manufacturerName);
                          callback(null, manufacturerName);
                        });
                      },
                      function(callback) {
                        console.log('readIrTemperature');
                        sensorTag.readIrTemperature(function(objectTemperature, ambientTemperature) {
                          console.log('\tobject temperature = %d °C', objectTemperature.toFixed(1));
                          console.log('\tambient temperature = %d °C', ambientTemperature.toFixed(1));

                          var obj = Object();
                          obj['objectTemperature'] = objectTemperature;
                          obj['ambientTemperature'] = ambientTemperature;
                          callback(null, obj);
                        });
                      },
                      function(callback) {
                        console.log('readAccelerometer');
                        sensorTag.readAccelerometer(function(x, y, z) {
                          console.log('\tx = %d G', x.toFixed(1));
                          console.log('\ty = %d G', y.toFixed(1));
                          console.log('\tz = %d G', z.toFixed(1));

                          var obj = Object();
                          obj['Accelerometer_x'] = x;
                          obj['Accelerometer_y'] = y;
                          obj['Accelerometer_z'] = z;
                          callback(null, obj);
                        });
                      },
                      function(callback) {
                        console.log('readHumidity');
                        sensorTag.readHumidity(function(temperature, humidity) {
                          console.log('\ttemperature = %d °C', temperature.toFixed(1));
                          console.log('\thumidity = %d %', humidity.toFixed(1));

                          var obj = Object();
                          obj['temperature'] = temperature;
                          obj['humidity'] = humidity;
                          callback(null, obj);
                        });
                      },
                      function(callback) {
                        console.log('readMagnetometer');
                        sensorTag.readMagnetometer(function(x, y, z) {
                          console.log('\tx = %d μT', x.toFixed(1));
                          console.log('\ty = %d μT', y.toFixed(1));
                          console.log('\tz = %d μT', z.toFixed(1));

                          var obj = Object();
                          obj['Magnetometer_x'] = x;
                          obj['Magnetometer_y'] = y;
                          obj['Magnetometer_z'] = z;
                          callback(null, obj);
                        });
                      },
                      function(callback) {
                        console.log('readBarometricPressure');
                        sensorTag.readBarometricPressure(function(pressure) {
                          console.log('\tpressure = %d mBar', pressure.toFixed(1));

                          var obj = Object();
                          obj['pressure'] = pressure;
                          callback(null, obj);
                        });
                      },
                      function(callback) {
                        console.log('readGyroscope');
                        sensorTag.readGyroscope(function(x, y, z) {
                          console.log('\tx = %d °/s', x.toFixed(1));
                          console.log('\ty = %d °/s', y.toFixed(1));
                          console.log('\tz = %d °/s', z.toFixed(1));

                          var obj = Object();
                          obj['Gyroscope_x'] = x;
                          obj['Gyroscope_y'] = y;
                          obj['Gyroscope_z'] = z;
                          callback(null, obj);
                        });
                      }
                    ],
                    function(err, results) {
                      console.log('results');
                      console.log(results);
                    }
                  );
                }, 1000*10);
              });
            });
          });
        });
      });
    });
  });
 });
});

※setIntervalの設定値(10秒)に一度データを取得します

※node-sensortagのサンプルプログラムを修正して使用

3. 動作確認

# node プログラム名
connect
discoverServicesAndCharacteristics
enableIrTemperature
enableAccelerometer
enableHumidity
enableMagnetometer
enableBarometricPressure
enableGyroscope
readDeviceName
	device name = TI BLE Sensor Tag
readSystemId
	system id = XX:XX:XX:XX:XX:XX:XX:XX
readSerialNumber
	serial number = N.A.
readFirmwareRevision
	firmware revision = 1.4 (Jul 12 2013)
readHardwareRevision
	hardware revision = N.A.
readSoftwareRevision
	software revision = N.A.
readManufacturerName
	manufacturer name = Texas Instruments
readIrTemperature
	object temperature = 13.4 °C
	ambient temperature = 17.8 °C
readAccelerometer
	x = 0.1 G
	y = -1 G
	z = 0 G
readHumidity
	temperature = 18.3 °C
	humidity = 56 %
readMagnetometer
	x = -8.5 μT
	y = 36.2 μT
	z = 11.4 μT
readBarometricPressure
	pressure = 1024.9 mBar
readGyroscope
	x = -8 °/s
	y = 0.4 °/s
	z = 0.8 °/s
results
[ 'TI BLE Sensor Tag',
  'XX:XX:XX:XX:XX:XX:XX:XX',
  'N.A.\u0000',
  '1.4 (Jul 12 2013)\u0000',
  'N.A.\u0000',
  'N.A.\u0000',
  'Texas Instruments\u0000',
  { objectTemperature: 13.44605725804405,
    ambientTemperature: 17.8125 },
  { Accelerometer_x: 0.078125,
    Accelerometer_y: -0.953125,
    Accelerometer_z: 0.03125 },
  { temperature: 18.30496826171875, humidity: 55.97357177734375 },
  { Magnetometer_x: -8.514404296875,
    Magnetometer_y: 36.163330078125,
    Magnetometer_z: 11.3525390625 },
  { pressure: 1024.8881179531002 },
  { Gyroscope_x: -8.04901123046875,
    Gyroscope_y: 0.3509521484375,
    Gyroscope_z: 0.77056884765625 } ]

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中