Raspberry PIとSensor Tag

Raspberry Pi2にBluetoothのドングルを指して、Broadcom WICED SenseとTI SensorTagに同時にBluetooth経由でNodeJSでアクセスする方法について、備忘録で残しておく。

– 現物
右 : Broadcom WICED Sense
左 : TI Sensor Tag
IMG_8841

Broadcom WICED Sense
搭載されているセンサーとしては、加速度、ジャイロ、電子コンパス、気圧、温度、湿度が取れますね。BluetoothはBCM20737S。スイッチサイエンスさん、マクニカオンラインさんとかでゲットできるですね。

TI Sensor Tag
こちらは、CC2650が搭載された激しめの端末。加速度、ジャイロ、3軸磁力計(というかMPU 9250)、照度、湿度、気圧、磁気、IRサーモパイルセンサといった感じで盛り積み。

– Raspberry PI2上での作業
NodeJSとbluezを入れるだけ。

uname -a 
# nodejsとbluezのインストール
# Linux raspberrypi 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux
# RASPBIAN JESSIEを使っています。
# 
sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
node -v 
npm -v
sudo apt-get install bluez

– WICED Senseにつなぐ

npm install cylon
npm install cylon-ble
npm install cylon-wiced-sense

test.js

var Cylon = require('cylon');
// UUIDはhcitool lescanで見れるMacアドレス
// 00:10:18:01:4A:FA WICED Sense Kit
// こんな感じで
Cylon.robot({
  connections: {
    bluetooth: { adaptor: 'ble', uuid: '001018014afa' }
  },

  devices: [
    {name : 'wiced',  driver: 'wiced-sense' },
  ],

  display: function(err, data) {
    if (err) {
      console.log("Error:", err);
    } else {
      console.log("Data:", data);
    }
  },

  work: function(my) {
    my.wiced.getData(function(err, data) {
      if (!!err) {
        console.log("Error: ", err);
        return;
      }
      console.log("Data: ", data);
    });
  }
}).start();

結果

sudo node test.js     
2015-10-15T07:22:39.092Z : [Robot 1] - Starting connections.
2015-10-15T07:22:39.152Z : [Robot 1] - Starting connection 'bluetooth'.
2015-10-15T07:22:43.960Z : [Robot 1] - Starting devices.
2015-10-15T07:22:43.963Z : [Robot 1] - Starting device 'wiced'.
2015-10-15T07:22:43.963Z : [Robot 1] - Working.
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: -50, y: -247, z: -222 },
  magnetometer: { x: 548, y: 653, z: -1464 } }
Data:  { accelerometer: { x: 1, y: 0, z: 88 },
  gyroscope: { x: 6, y: -249, z: -220 },
  magnetometer: { x: 535, y: 656, z: -1463 } }
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: -19, y: -219, z: -217 },
  magnetometer: { x: 553, y: 651, z: -1460 } }
Data:  { accelerometer: { x: 0, y: 0, z: 88 },
  gyroscope: { x: 24, y: -250, z: -208 },
  magnetometer: { x: 558, y: 643, z: -1451 } }
Data:  { accelerometer: { x: 1, y: 0, z: 89 },
  gyroscope: { x: 30, y: -243, z: -215 },
  magnetometer: { x: 558, y: 648, z: -1448 } }
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: -11, y: -243, z: -204 },
  magnetometer: { x: 565, y: 671, z: -1460 } }
Data:  { accelerometer: { x: 1, y: 0, z: 90 },
  gyroscope: { x: 9, y: -246, z: -225 },
  magnetometer: { x: 562, y: 646, z: -1440 } }
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: 10, y: -227, z: -206 },
  magnetometer: { x: 557, y: 651, z: -1462 } }
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: -12, y: -251, z: -223 },
  magnetometer: { x: 556, y: 642, z: -1451 } }
Data:  { humidity: 701, pressure: 10120, temperature: 285 }
Data:  { accelerometer: { x: 0, y: 0, z: 89 },
  gyroscope: { x: 3, y: -248, z: -196 },
  magnetometer: { x: 554, y: 655, z: -1455 } }

– Sensortagにつなぐ

npm install async
npm install bluebird
npm install sensortag 

test.js

var SensorTag = require('sensortag');

// UUIDはhcitool lescanで見れるMacアドレス
// 68:C9:0B:06:E4:8D CC2650 SensorTag
// こんな感じで
SensorTag.discoverByAddress("68:C9:0B:06:E4:8D", function(tag) {
    tag.on('disconnect', function() {
        console.log('disconnected!');
        process.exit(0);
    });

    function connectAndSetUpMe() {
        console.log('connectAndSetUp');
        tag.connectAndSetUp(enableTempMe);
    }

    function enableTempMe() {
        console.log('enableSensor');
        tag.enableHumidity(notifyMe);
        tag.enableLuxometer(notifyLux);
    }

    function notifyLux() {
    }

    function notifyMe() {
        tag.notifyHumidity(listenForHumidity);

        tag.setHumidityPeriod(10000, function(error) {
            if (error != null)
                console.log(error);
        });
    }

    function listenForHumidity() {
        tag.on('humidityChange', function(temperature, humidity) {
            tag.readLuxometer(function(error, lux) {
                console.log('\tlux = %d', lux.toFixed(1));
                console.log('\thumidity Temp = %d deg. C', temperature.toFixed(1));
                console.log('\tthumidity  = %d', humidity.toFixed(1));
            });
        });
    }

    connectAndSetUpMe();
});

結果

sudo node test.js 
connectAndSetUp
enableSensor
	lux = 134185
	humidity Temp = 27.2 deg. C
	thumidity  = 64.9
	lux = 140.2
	humidity Temp = 27.2 deg. C
	thumidity  = 64.9
	lux = 140.6
	humidity Temp = 27.2 deg. C
	thumidity  = 64.9

めちゃ簡単。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください