Posts Tagged: ‘BB-8’

Star Wars BB-8 meets Texas Instruments SensorTag

27. April 2016 Posted by Stephan Kopp

My first tests in the Internet of Things world. Using node.js to communicate with a TI sensortag and a sphero BB-8. Just reading the luxometer from the sensortag and if it goes dark, switch bb-8 color to red.

Sure, this is not really an impressive demo, but to start with a new topic, I always prefer small and easy examples. Those examples can be implemented from scratch by myself, instead of using rather complex examples, I don’t understand in detail.

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

console.log("Searching for BB-8...");
bb8 = sphero("b1c5974fe1634c8ebd08f575cd3fc9e7"); // change BLE address accordingly

bb8.connect(function () {
 console.log("--> connected to BB-8");

 //The Ping command verifies that BB8 is awake and receiving commands.
 bb8.ping(function(err, data) {
 console.log(err || data);
 console.log("Searching for sensor tag...");
 SensorTag.discover(function (sensorTag) {
 console.log('--> discovered: ' + sensorTag);

 sensorTag.on('disconnect', function () {
 console.log('--> disconnected!');
 process.exit(0);
 });

 async.series([
 function (callback) {
 sensorTag.connectAndSetup(function (err) {
 console.log("--> Sensortag setup successfull!");
 callback();
 });
 },
 function (callback) {
 sensorTag.enableLuxometer(function () {
 console.log("-> Luxometer enabled");
 callback();
 });
 },
 function (callback) {
 sensorTag.enableAccelerometer(function () {
 console.log("-> Accelerometer enabled");
 callback();
 });
 },
 function (callback) {
 sensorTag.enableGyroscope(function () {
 console.log("-> Gyroscope enabled");
 callback();
 });
 }
 ],
 function (err) {
 if (err) {
 console.log("something went wrong: " + err);
 throw err;
 }
 console.log("==> Init complete, start reading sensors...");

 //start reading sensors in loop
 var interval = setInterval(function () {
 readSensor(sensorTag);
 }, 1000);
 });
 });
 });
});

function changeColor(color) {
 console.log("--> change BB-8 color to " + color);
 bb8.color(color);
}

function readSensor(sensorTag) {
 var localdata = {
 lux: 0,
 accelerometer: { x: 0, y: 0, z: 0 },
 gyroscope: { x: 0, y: 0, z: 0 }
 };
 async.parallel([
 function (callback) {
 sensorTag.readLuxometer(function (error, lux) {
 localdata.lux = lux.toFixed(1);
 callback();
 });
 },
 function (callback) {
 sensorTag.readAccelerometer(function (error, x, y, z) {
 localdata.accelerometer.x = x.toFixed(1);
 localdata.accelerometer.y = y.toFixed(1);
 localdata.accelerometer.z = z.toFixed(1);
 callback();
 });
 },
 function (callback) {
 sensorTag.readGyroscope(function (error, x, y, z) {
 localdata.gyroscope.x = x.toFixed(1);
 localdata.gyroscope.y = y.toFixed(1);
 localdata.gyroscope.z = z.toFixed(1);
 callback();
 });
 }
 ], function (err) {
 if (err) {
 console.log("Something went wrong: " + err);
 throw err;
 } else {
 //print collected sensor data
 printData(localdata);
 console.log(localdata.lux.toString());
 if (localdata.lux < 0.5) {
 changeColor("red");
 } else {
 changeColor("green");
 }
 }
 });
}

function printData(data) {
 console.log("lux=%d t acc(x/y/z)=(%d/%d/%d) t gyro(x/y/z)=(%d/%d/%d)", data.lux, data.accelerometer.x, data.accelerometer.y, data.accelerometer.z, data.gyroscope.x, data.gyroscope.y, data.gyroscope.z);
}

Filed under: Development, Internet of Things