HomeUsing SensorTag sensors

Using SensorTag sensors

SensorTag experiments

Sending data to an HTML5 canvas drawing context from the positional data of a TI SensorTag.

Steps for using BLE and websockets for streaming data.

Setting up the sensortag.

Install the SensorTag node module

npm install sensortag

Creating a module and Registering for events

Within lib/SensorTagStream.js a stream source point is defined.

The events generated by this stream source are piped, via websockets, to the browser canvas element.

The connection is expressed in a single line:

tag.pipe(stream);

Once loaded the sensorTag object is passed to the discover function. It is in this scope that the events and data are dealt with and pushed to an output stream. This stream source point will be piped to a stream end or surface.

var SensorTag = require("sensortag");
var stream = require('stream');
var self = this;

SensorTag.discover(function(sensorTag) {
    sensorTag.connect(function() {
        sensorTag.discoverServicesAndCharacteristics(function(){
            sensorTag.enableGyroscope();
        });

        sensorTag.on('accelerometerChange', function(x, y, z) {
            if(self._piping) {
                var data = {
                    acceleromter : {x : x, y : y, z : z }
                };
                self.push(JSON.stringify(data));
            }
        });
    });
});

Responding to events

Within the main app.js file;

var tag = require('./lib/SensorTagStream.js');

And within the main app.js

var websocket = require('websocket-stream');
var wss = websocket.createServer({server: server}, handle);
function handle(stream) {
  tag.pipe(stream);
}

Connecting a client and responding

The browser needs to establish a websocket connection and execute a response as the coordinates are received.

var ws = require('websocket-stream');

To on the connection. Simultaneously an animation loop is drawing the position of a circle onto an HTML5 canvas element. The circle position is updated continusly as events are received from the gyroscope.

stream.on('data', function(data) {
    var tagData = JSON.parse(data.toString());
    if(tagData.acceleromter) {
        circlePos.x += tagData.acceleromter.x * 100;
        circlePos.y += tagData.acceleromter.y * 100;
    }
});

Finding the temperature of a cup of tea.

With an infra-red sensor heat OSX notifications can be hooked up to trigger when a predefined temperture.