Skip to content

Instantly share code, notes, and snippets.

@benlower
Created August 21, 2014 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benlower/7dd797707be1d93cc1f7 to your computer and use it in GitHub Desktop.
Save benlower/7dd797707be1d93cc1f7 to your computer and use it in GitHub Desktop.
Get data from three Tessel ambient modules into Redis via Node.js. Used this as my starting point: http://blog.technical.io/post/93254241807/usb-communication-between-tessel-and-node-on-your-pc
var tessel = require('tessel');
var redis = require('redis');
var redisClient = redis.createClient();
var script = require.resolve('./device/index.js');
var opts = {
// Stop existing script, if any
stop: true,
// Serial number (`undefined` picks the first one)
serial: process.env.TESSEL_SERIAL,
};
var args = [];
var startTime = new Date();
// `tessel.findTessel` finds a Tessel attached to this computer and connects.
tessel.findTessel(opts, function(err, device) {
if (err) throw err;
// Once we've found a Tessel, we tell it to run our script. This works just
// like `tessel run` and bundles the `device/` directory. It bundles only
// `device/` and not the host code because `device/` has its own
// `package.json`.
device.run(script, args, {}, function () {
// Connect the stdout and stderr of the process running on Tessel to
// the console, so that our `console.log` messages show.
device.stdout.resume();
device.stdout.pipe(process.stdout);
device.stderr.resume();
device.stderr.pipe(process.stderr);
// `device.on('message', function (msg) { ... })` receives an event
// when an object is received from Tessel.
device.on('message', function (m) {
var now = new Date();
console.log(startTime.getTime() + ' - [PC] Message from Tessel at: ' + now.getTime());
redisClient.publish(m.ambientSensor, m.soundLevel);
});
// Exit cleanly on Ctrl+C.
process.on('SIGINT', function() {
// Try to stop the process on the Tessel
device.stop();
setTimeout(function () {
// But if that fails, just exit
logs.info('Script aborted');
process.exit(131);
}, 200);
});
// When the script on Tessel exits, shut down
// USB communications and exit
device.once('script-stop', function (code) {
device.close(function () {
process.exit(code);
});
});
});
});
// The `tessel` module built-in to the Tessel firmware for access to hardware
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var ambientA = ambientlib.use(tessel.port['A']);
var ambientB = ambientlib.use(tessel.port['B']);
var ambientC = ambientlib.use(tessel.port['C']);
console.log('starting up the device script...');
// Make sure our ambient sensor(A) is ready
ambientA.on('ready', function() {
ambientA.on('sound', function(sData) {
process.send({ ambientSensor: "ambientA", soundLevel: sData });
});
});
// Make sure our ambient sensor(B) is ready
ambientB.on('ready', function() {
ambientB.on('sound', function(sData) {
process.send({ ambientSensor: "ambientB", soundLevel: sData });
});
});
// Make sure our ambient sensor(C) is ready
ambientC.on('ready', function() {
ambientC.on('sound', function(sData) {
process.send({ ambientSensor: "ambientC", soundLevel: sData });
});
});
// Keep the event loop alive
process.ref();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment