Raspberry Pi GPIO and Node.js

My last post talked about JavaScpipt and how exciting it is to be programming in this language. With this post I thought I would continue and explain a little bit more why it is exciting. A few months ago at Linear Blue the web team began looking into Node.js and in particular it’s ability to achieve socket programming. At the time we were involved in a project that was using a service that uses sockets to talk back to the web server. We were looking at wether it was possible for us to produce the same and that is when we came across Node.js.

Since then I purchased a Raspberry Pi a little computer device that has programmable I/O pins or Input / Output pins. Doing some research on programming the pins I came across a module for Node.js to allow access to program the pins using Node.js, so here I am picking up Node.js. Where  there are some advantages to using Node.js there are some pit falls that need to be addressed, for instance to access the I/O pins and turn them on or off you need to have root access but running your application as root is not practical and if it is to be connected to the internet can leave your Pi open to hacking. There is a work around and it took me a while to get my head around this in terms of programming but sure enough I have managed to blink two LED’s independently.

Problems and Solutions

The best way to learn is to jump in the deep end. That said the first step is installing node itself for the Raspberry Pi, at present the package installer did not work for me so downloading the correct version at first was a little problematic. A google search for Raspberry Pi install node js brought up http://joshondesign.com/2013/10/23/noderpi the instructions were spot on but I at first made an error. Looking at the date of the blog post I decided to go to www.nodejs.org and download the latest version except that I had forgotten that the Raspberry Pi is not like my Mac Laptop and the source files did not work as expected.

What I needed to do was download the version for ARM processors. Going to http://nodejs.org/dist listed all the versions so what I did was to visit the folder versions until I found the latest one which happend to be v0.10.22 I know this as v0.10.23 had no arm version so must still be in development  or testing.

After installing and making sure npm Node Package Manager was installed, I also installed the module pi-gpio which is the main module for accessing the I/O pins on the Raspberry Pi. On the Node Package Manager page for pi-gpio module has some sample code and documentation for using the functions. Diving straight in I was hit by an issue which after much googling turned out to be a permissions issue with my program accessing the pins as a standard user and using sudo or super user do did not allow me to run the node engine as the super user and node was not excessible from running it as root not to mention that it is not wise to do so. After googling this issue I came across quick2wire which pointed out that I needed to export the pin or pins for use with my program this then allows me to then open and write to the pin turning it on or off.

After running the command gpio-admin export 18 as pin 18 was what I was using my program begun running with out error. I began trying to set the pin to on and off but ran into a problem I kept getting an error message gpio-admin: could not flush data to /sys/class/gpio/export: Device or resource busy and the program would halt execution. It was not clear from the documentation how to call the functions but after many tries I found the right method and I began to make an LED blink on a breadboard.

All this was fine until I stopped the program and then tried to re-run it again, which reported the same gpio-admin: could not flush data to /sys/class/gpio/export: Device or resource busy like before only this time the program did not completely stop. The pin was still turning on and off it was just telling me that something else may be using this pin as well so the results might not be what you are after. Trouble was I know who was or did use the pin it was my programs last run. So it’s like someone using something and not putting back as they found it. To reset the pin back to a usable state again the commands gpio-admin unexport 18 doing this might return gpio-admin: could not flush data to /sys/class/gpio/unexport: Invalid argument if so don’t worry just run gpio-admin export 18 and you will be able to run the program again.

To resolve the problem when stopping our program we look to node’s internal process object. All node app’s have this predefined object call process which has a link back to the host operating system. This means that it can receive signals from the OS and as such our programs can listen for these signals, the final program code below shows how I achieved access to the Raspberry Pi GPIO pins using Node.js.

var gpio = require(“pi-gpio”);

var status = 0;

var timerId =0;

gpio.open(18, “output”, function(err) {        // Open pin 18 for output

timerId =setInterval(function(){

gpio.read(18,function(err,value){

console.log(value);

 

gpio.write(18, 1, function(error) {            // Set pin 18 high (1)

console.log(error);

// do something else or not

});

 

 

});

setTimeout(function(){

gpio.read(18,function(err,value){

console.log(value);

gpio.write(18,0,function(error){

console.log(error);

// do something else or not

});

});

},500);

},1000);

});

process.on(‘SIGINT’, function() {

console.log(‘Got SIGINT. Now closing the pin and stopping the program’);

gpio.close(18);

clearTimeout(timerId);

});

The next step is to put all this into client server program so I can turn on the LED’s using my iPhone’s Accelerometer which leads me into the overall project of creating a Remote Control car, for this I will need motors and I am going to use LEGO for the car’s chassis. I will follow up this post and explain how using web based technologies it is possible to control the motors to control a car.

Warren Tucker

While studying for a bachelors degree in Internet Technology, and working for Bucks New University as a Senior Technician Warren also taught CISCO CCNA short courses for Bucks New University. After leaving in 2008 to work for a Digital Web agency as a web developer but with knowledge of networking and computer support he soon became the companies computer support for both internal staff and external clients. Staying close to networking he moved on to work for a telecoms company as a PHP Team Leader Developer working on ERP systems with iPhone integration. 2012 Warren worked most of it as a contractor expanding on his skills as a Developer which he has extensive knowledge of PHP / HTML / Javascript / CSS he is currently expanding on his iPhone/iPad and Android development.

More Posts - Website

1 thought on “Raspberry Pi GPIO and Node.js”

  1. Hi Warren, Great post! I have been using node.js for some time, (creating real time trading systems) it is an awesome project. Your work here on interfacing to hardware is really cool. I hope you keep posting in this area, I am certainly watching closely for the ‘good oil’. Thanks Lance

Leave a Reply