DIY

Interfacing Leap Motion with Arduino thanks to Node.js

What about controlling physical things by waving your hands?

Thanks to the Leap Motion, Arduino and a bit of Node.js magic it's pretty simple!

Let's check that!

Leap Motion

There is a nice feature in the SDK of the Leap Motion: the websocket server.

When activated, the Leap Motion software streams the tracking data over it. You then just need to connect your software to it and you're ready to analyse these data.

Go to the Leap Motion controller settings, and then in the WebSocket tab and activate it.

The websocket server is now accessible at the following adress: ws://127.0.0.1:6437

Connect your Node.js application to the websocket server

Node.js is amazing, and connecting your app to the Leap Motion websocket server is just a matter of 2 lines.

In your project, install the ws library with npm install ws --save

And then you can check everything is working with this simple code:

var webSocket = require('ws'),
    ws = new webSocket('ws://127.0.0.1:6437');

ws.on('message', function(data, flags) {
    console.log(data);
});

When running this script, it should log the data coming from the websocket. If so, you are ok!

It is then just a matter of parsing the data and take what you need in it!

Here you can find an example of a frame: frame.json

Arduino

So you have now all the data you need from the Leap Motion, how can you pass it to the Arduino? Node.js is again the answer!

Thanks to the marvelous johnny-five library, you can talk to the Arduino directly from Node.js! To achieve that, you'll just need to upload the Standard Firmata on your Arduino.

Open the Arduino IDE, open the File>Examples>Firmata>StandardFirmata and upload it to your Arduino.

You can now use johnny-five to communicate with the Arduino.

Install it: npm install johnny-five --save. Below is a snippet to connect your board and make the led that is tied to the pin 13 blink.

var five = require('johnny-five'),
    board = new five.Board(),
    led;

board.on('ready', function() {
    led = new five.Led(13);
    led.strobe();
});

Easy isn't it?

Plug all this together

It's now really easy to plug all this together. Let's try to make a simple example that turns on the led when the Leap Motion sees 2 hands, and turns it off when not. You should take a look to the sample frame once more: frame.json.

var webSocket = require('ws'),
    ws = new webSocket('ws://127.0.0.1:6437'),
    five = require('johnny-five'),
    board = new five.Board(),
    led, frame;

board.on('ready', function() {
    led = new five.Led(13);    
    ws.on('message', function(data, flags) {
        frame = JSON.parse(data); 
        if (frame.hands && frame.hands.length > 1) {
            led.on();
        }
        else {
            led.off();
        }
    });
});

That's it! (I hope so, it's not tested :))

Going further

Here is the demo I made few weeks ago:

You can find the code on github: leapLamp, feel free to use it and/or ask for some help.

Happy coding.

Plug your Minitel on your Raspberry Pi

Hi,

So what is a Minitel? According to Wikipedia :

The Minitel was a Videotex online service accessible through telephone lines, and is considered one of the world's most successful pre-World Wide Web online services.¹

This service was accessible through particular devices. They had a screen, a keyboard and a modem.

A screen and a keyboard are just what we need for our Pi, so let's plug them together!

Minitel and serial communication

The Minitel have a serial port. It's goal is to communicate to peripherals such as a printer or whatever.

The socket is a classic 180° DIN with 5 pins :

Here is the description of the pins:

  • 1: Rx: data reception
  • 2: Ground
  • 3: Tx: data transmission
  • 4: Ready to work signal
  • 5: 8.5v - 1A power supply

So pins 1,2 and 3 are what we need to communicate through serial with the Pi.

Please note that not all Minitels have this kind of sockets. To find a compatible one, the Minitel must have this socket AND two special keys on the keyboard Fnct and Ctrl. They are usualy called Minitel 1B.

TTL levels and the Pi

The UART on the Pi works with 0v and 3.3v. But a lot of old stuff use 0v and 5v. This is the case of the Minitel, so we need to adapt the voltage levels :

  • Lower the Tx level of the Minitel from 5v to 3.3v
  • Raise the Tx level of the Pi from 3.3v to 5v

To achieve that, I used the following schema based on the recommendation of @lhuet35 (thanks!). You can check its Devoxx presentation (in french) here : 3615 Cloud

Be careful, the unused pin between the 5v and the GND of the Pi is not depicted on this schema!!

Here is the stuff mounted on a breadboard :

Configure a tty on the UART

You then need to configure a tty that will communicate through the UART.

The following configuration is based on a Raspbian, but it should be the same on other distros.

  • You may need to install getty :
    • sudo apt-get install getty
  • Backup the /boot/cmdline.txt file just in case :)
    • sudo cp /boot/cmdline.txt /boot/cmdline.bak.txt
  • Edit the file:
    • sudo vim /boot/cmdline.txt
    • and remove everything related to the serial port ttyAMA0, i.e. : console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
  • Add a getty conf on /etc/inittab :
    • 7:2345:respawn:/sbin/getty ttyAMA0 4800v23
    • also check there is no other getty conf for this tty on the file
  • Then you need to create a gettydefs file (or edit it)
    • sudo vim /etc/gettydefs
    • and add the following 4800v23# B4800 CS7 PARENB -PARODD GLOBAL # B4800 ISTRIP CS7 PARENB -PARODD GLOBAL BRKINT IGNPAR ICRNL IXON IXANY OPOST ONLCR CREAD HUPCLISIG ICANON ECHO ECHOE ECHOK #@S login: #4800v23 on one line!
    • this will configure the tty on UART

You can now plug the Pi to the Minitel and reboot the Pi.

Configure the Minitel

You need to switch the Minitel mode to be able to communicate through the serial port.

  • Power on the Minitel
  • Press Fnct+T then A : the Minitel will switch to the serial mode
  • Press Fnct+P then 4 : the Minitel now communicate through serial at 4800bps (the max speed)
  • Press Fnct+T then E : to deactivate the local echo
  • Press and you should now see the login prompt (maybe with some white squares), put your login and you're done!

Be aware that you'll need to do this Minitel configuration everytime you power it up.

Here is a pic of my Minitel :

Happy coding!