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