Control the Sphero with the Leap Motion using Node.js


A few months ago, I wrote a tutorial on how to control the Sphero with the Leap Motion using Cylon.js but I actually forgot to write one about using Node.js (which is what I started with).

If you’d like to go straight to the source code, you can find it on my github.

When I started working on this project, I used the Spheron module but I think something is not working with it anymore so I changed to the official sphero.js library.

If I start by explaining the setup of the app in Node.js, here is a snippet of my app.js file with comments to explain what everything does:

// require the relevant modules
var express = require('express'),
    path = require('path');

//create web server
var app = express();

//require the custom module
var test = require('./my_modules/sphero');

// Everything in public will be accessible from '/'
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views')));

app.all('*', function(req, res){

console.log("server running on port 3001");

As you can see, this is pretty straight forward. The most interesting part of the code is inside my custom module (index.js file).


module.exports = function() {

  var Leap = require('leapjs');
  var sphero = require("sphero");

  // Set this to the device Sphero connects as on your computer.
  var device = sphero("/dev/tty.Sphero-RBR-AMP-SPP");

  var safeMode = true;

  var controlSphero = function(spheroBall) {

      var controller = Leap.loop({frameEventName:'deviceFrame',    enableGestures:true});

      controller.on('frame', function(frame) {
            var g = frame.hands[0];

      var handleSwipe = function(g) {
            var previousFrame = controller.frame(1);
            var movement = g.translation(previousFrame);
            var direction = '?';

            if(movement[0] > 4){
              direction = 'RIGHT'
            } else if(movement[0] < -4){  
              direction = 'LEFT'             

            if(movement[1] > 4){
              direction = 'UP'
            } else if(movement[1] < -4){               
              direction = 'DOWN'             

            if(movement[2] > 4){
              direction = 'REVERSE'
            } else if(movement[2] < -4){
              direction = 'FORWARD'

          switch (direction) {
            case 'LEFT':
              spheroBall.roll(70, 270, 1);
            case 'RIGHT':
              spheroBall.heading = 90;
              spheroBall.roll(70, 90, 1);
      	console.log('Direction: %s', direction);


  // Stops the Sphero from rolling.
  var stopSphero = function(spheroBall) {

  device.connect(function() {
      console.log('connected to Sphero');


You start by requiring the modules you need, so here, ‘leapjs’ and ‘sphero’.

You then need to create an instance of the Sphero by using the reference of the ball on your computer.

To find that, turn your Sphero on as well as the bluetooth on your computer, connect to the Sphero, and once this is done, execute this command in your terminal: ‘ls /dev/tty.Sphero*’. The reference of the Sphero should appear and you just need to copy it in the code (it will probably be the same as mine).

Then, create an instance of the Leap Motion controller and start slowly by just tracking movement over it. Once you can track your hand, here is how you track the direction of your movement:


tracks the first frame of your movement.


gets you the difference between your first frame and the current frame, so you can see which coordinate is the most impacted by your movement and therefore get the direction.

These coordinates are in the format “x,y,z” so horizontal axis, vertical axis and depth.

Once you got that working, it’s time to hook up the Sphero and the Leap Motion together.

To make the ball roll, you simply need to call “.roll()” on the sphero, like this:

spheroBall.roll(70, 270, 1);

The attributes passed in are the following:

sphero.roll(speed, heading, state, option)

To finish, all you need to do is call the “device.connect” function to connect with your Sphero.

And that’s it!

Once again, don’t hesitate to fork the repo if you’d like to play with it yourself and improve it 🙂

Also, feel free to let me know if any of this is not working!

Have fun!

Controlling the Parrot AR Drone with the Myo armband in Node.js


Following the last tutorial I wrote on how to control the Sphero with the Myo armband, here is another one on how to control the Parrot AR Drone with the Myo using Node.js.

If you want to follow along, you can find the repo on my github.

To begin with, we need to setup the server configuration. After requiring the necessary modules, here is what we need to write:

var env = process.env.NODE_ENV || 'development';
if('development' == env){
  app.set('port', process.env.PORT || 3000);
  app.use(express.static(__dirname + '/public'));
  app.use("/node_modules", express.static(path.join(__dirname, 'node_modules')))

server = require('http').createServer(app);

var bayeux = new faye.NodeAdapter({
    mount: '/faye',
    timeout: 50


  client = new faye.Client("http://localhost:" + (app.get("port")) + "/faye", {});

  client.subscribe("/drone/move", function (d) {
    console.log("drone move?")
    return drone[d.action](d.speed);

  client.subscribe("/drone/drone", function (d) {
    console.log("drone stuff")
      return drone[d.action]();

  server.listen(app.get('port'), function () {
    console.log("Express server listening on port " + app.get("port"));

We use express to set up the server and Faye & Bayeux for the communications between the Drone and your app.

We also use the module ‘dronestream’ to be able to stream video from the drone to the browser that is listening on port 3001.

The rest of the code deals with the commands sent from the Myo to the drone. First of all, we need to create an instance of the Myo and then define some commands.

myo = Myo.create();


myo.on('fingers_spread', function(){

myo.on('wave_in', function(){

var takeoff = function () {
    flying = true;
    return faye.publish("/drone/drone", {
      action: 'takeoff'

var goLeft = function(){
    stopped = false;
    setTimeout(function (){
      return faye.publish("/drone/move", {
        action: 'left'
        // speed: adjustXspeed
    }, timeout);

Once the instance of the Myo is created, it can detect certain types of movements and run functions accordingly.

The ‘myo.unlock()’ command should allow the Myo to be unlocked while you use the app. However, I noticed I still needed to execute the unlock gesture ‘double tap’ between each command… Then the ‘faye.publish’ allows to send the commands and make the drone move. You can adjust the action and speed as you like.

This is just a snippet so once again, if you wanna have a look at the rest of the code or if you want to play around with it, everything is available on my Github 🙂

Hope it helps and enjoy!

Controlling the Sphero with the Myo armband in Node.js


I received my Myo armband a few months ago and I have been working on a few projects to control my other devices with it. To start with, I thought I would explain how I managed to control the Sphero ball using the movements of my arms and Node.js.

To follow along, you can view all the code on my github repo.

The setup of the server side is pretty straight-forward, here’s what is in my app.js file:

var express = require('express'),
    path = require('path');

var app = express();

var lab = require('./my_modules/myo_sphero');

// app.use(express.static(path.join(__dirname, 'views')));

//.all('*', function(req, res){
// res.sendfile('views/index.html');

console.log("Server running on port 3001");

As you can see, you start by requiring ‘express’ and ‘path’ to be able to start your server and serve your different files.

You then require the module you created and you call it so it can be executed when you start your app.

The commented part of the code gives you the ability to serve something in your browser as well by calling the files in your ‘views’ folder but I am not using it at the moment as I just make the connection between the Myo and the Sphero without building anything in the front-end. The first part calls all the files in your folder whereas the second part allows you to serve a single file.

Finally, the app uses the port 3001 that you can check in your browser by visiting http://localhost:3001.

Then, you can write your commands in your module; here’s a snippet of my index.js:

module.exports = function() {

  var spheron = require('spheron');
  var myo = require('myo');

  myo = Myo.create();

  // Set this to the device Sphero connects as on your computer.
  var device = '/dev/tty.Sphero-RBR-AMP-SPP';

  var safeMode = true;

  var controlSphero = function(sphero) {
    myo.on('wave_out', function(){
      sphero.heading = 90;
      sphero.roll(70, 90, 1);

Wrap your code into a ‘module.exports’ function to access it from other files.

Then, require the modules you need, create an instance of the Myo and find the reference of the Sphero on your computer by entering ‘ls /dev/tty.Sphero*’ in your command line once your Sphero and your bluetooth connection are on.

The controlSphero function checks if you are doing the ‘wave out’ movement with the Myo and if so, makes the Sphero roll to the right.

The myo.setLockingPolicy() command stops your Myo from being locked after a few seconds so you can keep on making movements and tracking them.

Myo updated their firmware and API recently and introduced an automatic locking of the armband to avoid accidental tracking of gesture but for this app, we need it to be turned off. You can usually pass in the value ‘none’ or ‘standard’ but I made it default to none. If you want to change this, you can find the myo.js file inside the myo module and find the function ‘setLockingPolicy’. You can learn more about this command on the API reference.

With the update of the firmware, new gestures have been added such as ‘index to thumb’ or ‘click’ and the ‘thumb to pinky’ has been removed so there is a little bit more you can play with.

Once again, if you’re interested, you can find the full code on my github.

Let me know if you have any question 🙂 Good luck!