Wirelessly control an Arduino with NodeJS over Bluetooth

I wanted to control my Arduino via Bluetooth using NodeJS but I could not find a Node module to do it. That is why I decided to build my own. This post describes how to use it.

Arduino setup

First, lets take a look at the Arduino setup I am using. It is a simple Arduino Uno with breadboard. For Bluetooth connectivity I’ve added a Bluetooth shield. For testing purposes I’ve configured a simple layout on the breadboard that allows me to control a LED. The picture below shows the configuration.

arduinosetup

I wrote a simple schema to control the LED. The program can change the status of the LED according to the value that is read from the serial Bluetooth connection. The program also allows to read the current state of the LED.

Bluetooth-serial-port

On the NodeJS side I have created a module that allows a script to communicate via a Bluetooth serial connection. The module can be used to communicate via Bluetooth as well as to search for Bluetooth devices and serial port channels.

Currently the module only supports the Bluez Bluetooth stack on Linux. I might add OS X support in the future. supports both Linux, Mac OS X and Windows (thanks Elmar!).

The module is available on npm and can be installed by issuing:

$ npm install bluetooth-serial-port

Using the module

To use the module you'll have to import it into your script. Below is a simple example program that controls the Arduino configuration described above.

var BTSP = require('bluetooth-serial-port');
var serial = new BTSP.BluetoothSerialPort();

serial.on('found', function(address, name) {

    // you might want to check the found address with the address of your
    // bluetooth enabled Arduino device here.

    serial.findSerialPortChannel(address, function(channel) {
        serial.connect(bluetoothAddress, channel, function() {
            console.log('connected');
            process.stdin.resume();
            process.stdin.setEncoding('utf8');
            console.log('Press "1" or "0" and "ENTER" to turn on or off the light.')

            process.stdin.on('data', function (data) {
                serial.write(data);
            });

            serial.on('data', function(data) {
                console.log('Received: ' + data);
            });
        }, function () {
            console.log('cannot connect');
        });
    });
});

serial.inquire();

Open issue

Currently the module works quite well. The only thing not working is when a script wants to reconnect the Bluetooth connection.

When a connection is ended, for example when the Arduino is switched off, and the scripts starts a new Bluetooth inquiry the module will find the Bluetooth serial channel again but does not connect to it.

My current work around for this issue is to terminate my script when a connection has ended and than restart the script again. To achieve this I'm using forever.

For example...

I hope this post helps you to build cool stuff using using NodeJS and Bluetooth. I'm curious about the applications you'll come up with. Please drop me a note ;-)

I've used the above configuration to make a UPnP controlable Bluetooth lightbulb prototype. For the UPnP side of the prototype I used the upnp-device module. The prototype will be part of the Figaro demonstrator that will demonstrate how IP-based and non-IP based home networks can be converged (PDF). This demonstrator is shown in the IEEE booth on the CES coming January.

All sources from this post are available as gist.

Happy programming!

This entry was posted in programming and tagged , , , , , , , , , . Bookmark the permalink.

9 Responses to Wirelessly control an Arduino with NodeJS over Bluetooth

  1. Ivan Ro says:

    Hi my Name is Ivan , I was working with you great module.
    I made this:

    But now I want to pass data to the arduino, but I’m geting some errors.
    I doing this:
    serial.write(data);
    And I’m getting some errors with the module, like I should pass mode parameters to the function.
    I don’t have my project here, so I will ask you late for this, with the issue and the real application.

    Anyways, thanks a lot for this great module

    Good luck, Ivan.

    • Eelco says:

      Hi Ivan,

      Great to see the cool stuff you’ve created with the module!

      Version 1.0.0 breaks the API of previous version so when you upgrade you should also update your code accordingly. The key change is that you should pass a Buffer object instead of a string.

      I hope this helps!
      Eelco

      • Ivan Ro says:

        This is my code, I use socket too.

        serial.on(‘found’, function(address, name) {
        serial.findSerialPortChannel(address, function(channel) {
        serial.connect(“00:00:00:00:00″, channel, function() {
        //Robot Connected
        console.log(‘Robot Connected’);
        //socket
        io.sockets.on(‘connection’,function SocketConnection (socket) {
        //Socket Connected
        console.log(“Socket Connected”);
        process.stdin.resume();
        process.stdin.setEncoding(‘utf8′);

        /*process.stdin.on(‘data’, function (data) {
        serial.write(data);
        });*/
        serial.write(new Buffer(‘Hello Robot’, ‘utf-8′));

        serial.on(‘data’, function(buffer) {
        console.log(buffer.toString(‘utf-8′));
        });

        //Cuando recibo info del Robot
        serial.on(‘data’, function(data) {
        buf+=data.toString(‘utf-8′); //Fix para el bluetooth
        if(buf.length >= 2){
        datos.push(buf);
        buf=””;
        if(datos.length == num){
        procesarData(datos,socket,dist,dir,robot); //cuando ya tengo los 4 datos
        datos=[];
        }
        }
        });
        });
        }, function () {
        console.log(‘cannot connect’);
        });
        });
        });

        I’m getting this error:

        /node_modules/bluetooth-serial-port/lib/bluetooth-serial-port.js:95
        this.connection.write(buffer, this.address, cb);
        ^
        TypeError: Third argument must be a function
        at BluetoothSerialPort.write (/node_modules/bluetooth-serial-port/lib/bluetooth-serial-port.js:95:29)

      • Eelco says:

        Thanks for the report. I will look into it. You are using Linux right? What version of node.js and what version of the bluetooth module are you using?

      • Ivan Ro says:

        Hi , thanks a lot for hear it :)
        Yes I’m using linux, and this is my node version v0.10.20
        And the version of the bluetooth module I think I’m using the last , how I can check that?
        Thanks

      • Eelco says:

        Hi Ivan,

        There is an error in the code example that I’ve provided in the README. The write function should have a callback function as the second argument (it is described in the API section of the README). If you change your call to the write to the code below it should work fine:


        serial.write(new Buffer(‘Hello Robot’, ‘utf-8′), function(err, count) {
        if (err) console.log('Something went wrong');
        });

        I've updated the documentation and the example will be fixed in the next release. Thanks for pointing this out!

      • Ivan Ro says:

        Ohhh men thanks you so much!!!!! , I don’t know for some reason I had never read that line , but you’re right , I change that and all works fine.

        Thanks you so much for this great module, and for your answers, I can continue with my little project now.

        Thanks, Ivan.

  2. meg says:

    hi eelco,

    very excited to see this module, but i’m having some trouble installing it. i’m new to node.js, so it is likely my fault, but i thought you should know. this is the error message:

    152 error Exit status 1
    153 error Failed at the bluetooth-serial-port@1.1.4 install script.
    153 error This is most likely a problem with the bluetooth-serial-port package,
    153 error not with npm itself.
    153 error Tell the author that this fails on your system:
    153 error node-gyp configure build
    153 error You can get their info via:
    153 error npm owner ls bluetooth-serial-port
    153 error There is likely additional logging output above.
    154 error System Darwin 11.4.2
    155 error command “node” “/usr/local/bin/npm” “install” “bluetooth-serial-port”
    156 error cwd /Users/meg/Documents/Files/nodejs
    157 error node -v v0.10.29
    158 error npm -v 1.4.14
    159 error code ELIFECYCLE

    sorry to bug you about this, but i’d love to get this up and running and start to explore node.js!

    cheers,
    meg

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s