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.


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.


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('Press "1" or "0" and "ENTER" to turn on or off the light.')

            process.stdin.on('data', function (data) {

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


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.

7 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:
    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!

      • 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’);
        io.sockets.on(‘connection’,function SocketConnection (socket) {
        //Socket Connected
        console.log(“Socket Connected”);

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

        serial.on(‘data’, function(buffer) {

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

        I’m getting this error:

        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?

      • 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.

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