Skip to content

Instantly share code, notes, and snippets.

@msell
Forked from mobyjames/bb8-hub.js
Created March 13, 2017 03:40
Show Gist options
  • Save msell/6637d53fe5e2d266645878f728dc29a8 to your computer and use it in GitHub Desktop.
Save msell/6637d53fe5e2d266645878f728dc29a8 to your computer and use it in GitHub Desktop.
BB-8 Internet of Things Hub
var sphero = require('sphero-pwn'),
exec = require('exec'),
macros = require('sphero-pwn-macros'),
keypress = require("keypress");
var orb = null,
id = 'ble://##:##:##:##:##:##';
var awsIot = require('aws-iot-device-sdk');
// MACROS
var actions = {};
var src = "motor 2 80 2 80\n" +
"delay 50\n" +
"motor 1 80 1 80\n" +
"delay 50\n" +
"motor 2 80 2 80\n" +
"delay 50\n" +
"motor 1 80 1 80\n" +
"delay 50\n" +
"motor 0 0 0 0\n";
actions['fast_nod'] = macros.compile(src);
src = "rgb 0 255 0\n" +
"delay 200\n" +
"rgb 0 0 255\n" +
"delay 200\n" +
"rgb 255 0 0\n" +
"delay 200\n" +
"rgb 0 0 0\n";
actions['flash'] = macros.compile(src);
var src = "motor 2 80 1 80\n" +
"delay 200\n" +
"motor 0 0 0 0\n" +
"delay 1000\n" +
"motor 1 80 2 80\n" +
"delay 200\n" +
"motor 0 0 0 0\n";
actions['look_around'] = macros.compile(src);
// HELPERS
var playSound = function(name) {
exec('afplay "sounds/bb8 - ' + name + '.mp3"', function() {});
};
var playAction = function(name) {
var macro = actions[name];
orb.loadMacro(0xFF, new Buffer(macro.bytes)).
then(function() {
orb.runMacro(0xFF);
});
};
var onKeyPress = function(ch, key) {
console.log('pressed: ' + key.name);
if (key.ctrl && key.name === "c") {
process.stdin.pause();
process.exit();
}
if (key.name === "y") {
animateYes();
}
if (key.name === "f") {
flashColors();
}
}
var handleMessage = function(topic, action) {
ensureConnected().then(function(response) {
switch (action) {
case 'flash':
return flashColors();
case 'yes':
return animateYes();
case 'look':
return lookAround();
}
});
};
var ensureConnected = function() {
// This is an area of great frustration
// For now, closing and re-opening connection every time
orb.channel().close();
return sphero.Discovery.findChannel(id).then(function(channel) {
orb = new sphero.Robot(channel);
});
};
// ACTIONS
var animateYes = function() {
playSound('be he do');
playAction('fast_nod');
};
var lookAround = function() {
playSound('up down');
playAction('look_around');
};
var flashColors = function() {
playSound('beep beep');
playAction('flash');
};
// CONNECT!
console.log('start');
// Connect to IoT on AWS
var device = awsIot.device({
keyPath: 'private.pem.key',
certPath: 'certificate.pem.crt',
caPath: 'root-CA.crt',
clientId: '###',
region: 'us-west-2'
});
device
.on('connect', function(foo) {
console.log('connected to IoT');
device.subscribe('bb8');
});
device
.on('message', function(topic, payload) {
var action = payload.toString();
console.log('message', topic, action);
handleMessage(topic, action);
});
sphero.Discovery.findChannel(id).then(function(channel) {
// new orb
orb = new sphero.Robot(channel);
console.log('connected to bb8');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment