Skip to content

Instantly share code, notes, and snippets.

@benlower
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save benlower/f7e7b8251de48d12d55d to your computer and use it in GitHub Desktop.

Select an option

Save benlower/f7e7b8251de48d12d55d to your computer and use it in GitHub Desktop.
This shows how to pass messages from a Tessel to the host machine over USB. Thanks to @kevinmehal for sharing the general approach (https://gist.github.com/kevinmehall/4c98d93e16323473b78b). I've combined that code with Tessel's ambient sample code (http://start.tessel.io/modules/ambient)
var tessel = require('tessel');
// changed from './ambient-attx4' to fix Error: Could not find module "/app/ambient-attx4/index.js"
var ambientlib = require('ambient-attx4');
var ambient = ambientlib.use(tessel.port['A']);
ambient.on('ready', function () {
// Get sound data.
setInterval( function () {
ambient.getSoundLevel( function(err, sdata) {
console.log("Sound Level:", sdata.toFixed(8));
})}, 500); // The readings will happen every .5 seconds unless the trigger is hit
// Set a sound level trigger
// The trigger is a float between 0 and 1
ambient.setSoundTrigger(0.1);
ambient.on('sound-trigger', function(data) {
console.log("Something happened with sound: ", data);
// Clear it
ambient.clearSoundTrigger();
//After 1.5 seconds reset sound trigger
setTimeout(function () {
ambient.setSoundTrigger(0.1);
},1500);
});
});
ambient.on('error', function (err) {
console.log(err)
});
process.on('message', function(msg) {
console.log('reply - received: ' + JSON.stringify(msg));
process.send(msg);
});
process.ref();
var tessel = require('tessel');
var script = '/device.js';
tessel.findTessel(null, true, function(err, client) {
if (err) throw err;
client.run(__dirname + script, ['tessel', script], {
// remove this so that all our files get sent to the Tessel
//single: true,
}, function () {
client.stdout.resume();
client.stdout.pipe(process.stdout);
client.stderr.resume();
client.stderr.pipe(process.stderr);
console.info('Running script...');
var count = 0;
setInterval(function(){
console.log('ping')
client.interface.writeProcessMessage({count:count++, data: {foo: 'bar'}})
client.once('message', function (m) {
console.log('pong', m.count);
});
}, 1000);
// Stop on Ctrl+C.
process.on('SIGINT', function() {
setTimeout(function () {
logs.info('Script aborted');
process.exit(131);
}, 200);
client.stop();
});
client.once('script-stop', function (code) {
client.close(function () {
process.exit(code);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment