Compiling open-zwave on the Raspberry Pi

After setting up the Raspberry Pi according to my previous post, it is now time to compile open-zwave onto it.

 

Installing Node.JS

We will download the ARM binary package from Node.JS and create the directory /opt/node where we want Node.JS installed. We will not place Node.JS in ‘/usr/local’, instead we use a separate directory for Node.JS, this way it is much easier to update a manually installed package.

 

sudo mkdir /opt/node

 

The next step is to dowload the Node.JS ARM binary package, unpack it and copy the content to our node directory /opt/node.

 

wget http://nodejs.org/dist/v0.10.22/node-v0.10.22-linux-arm-pi.tar.gz
tar xvzf node-v0.10.22-linux-arm-pi.tar.gz
sudo cp -r node-v0.10.22-linux-arm-pi/* /opt/node
rm node-v0.10.22-linux-arm-pi.tar.gz
rm -rf node-v0.10.22-linux-arm-pi/

 

Finally we have to add Node.JS to our path variable. For that you have to edit the ‘/etc/profile’ configuration file.

 

nano /etc/profile

 

Add the following lines to the configuration file before the ‘export’ command.

 

...
NODE_JS_HOME="/opt/node"
PATH="$PATH:$NODE_JS_HOME/bin"
export PATH
...

 

You need now to logout and log in again, so that the changed path variable will work.

 

logout

 

After logging in, we first install ‘forever’. When you want to use ‘npm’ or ‘node’ as root with sudo you have to add the ‘-i’ option, so that the root user’s environment is acquired. For example when you want to install the package ‘forever’ globally with ‘npm’.

 

sudo -i npm install forever -g

 

Configure Node.JS
We need now a start script for Node.JS. Our script will start the Node.JS with the user ‘pi’ and look for a server.js file in the /home/app directory. The output stream from Node.JS will be stored in the file nodejs.log. Just create the file nodejs.sh in your current directory with the following content:

 

#!/bin/bash
APP_DIR="/home/pi/app"
SERVER_JS_FILE="$APP_DIR/server.js"
FOREVER="/opt/node/bin/forever"
USER="pi"
OUT="/home/pi"
case "$1" in
start)
    echo "starting node.js..."
    sudo -i -u $USER $FOREVER start -o $OUT/nodejs.out.log -e $OUT/nodejs.err.log -w --watchDirectory $APP_DIR $SERVER_JS_FILE
    ;;
stop)
    echo "stopping node.js..."
    sudo -i -u $USER $FOREVER stop $SERVER_JS_FILE
    ;;
*)
    echo "usage: $0 (start|stop)"
esac

exit 0

 

Note: When yo want to use a privileged port (TCP/IP port numbers below 1024) you have to start the script as root.

 

Make the script executable with ‘chmod’ and copy it to ‘/etc.init.d’. The last step is to register the script as service with ‘update-rc.d’. Then our Node.JS server will automatically start up when the Raspberry Pi is powered on.

 

sudo chown root:root nodejs.sh
sudo chmod 755 nodejs.sh
sudo mv nodejs.sh /etc/init.d
sudo update-rc.d nodejs.sh defaults

 

The last step is to create a Node.JS server.js file in our /home/pi/app directory. The directory ‘/home/pi/app’ will be our Node.JS project directory.

 

mkdir /home/pi/app

 

The ‘server.js’ file is just a simple Node.JS server which will listen on port 8080 and display ‘Hello World’ and write some output to the console. Just place the following content as ‘server.js’ file in directory ‘/home/pi/app’.

 

var http = require('http');
http.createServer(function(req,resp) {
    resp.writeHead(200, {"Content-Type": "text/plain"});
    resp.write("Hello World");
    resp.end();
    console.log("sample output to console");
}).listen(8080);

 

Starting and stopping Node.JS

It’s time to start Node.JS with our start script and see if it all fits together.

 

sudo /etc/init.d/nodejs.sh start

 

Open a browser with the url http://10.0.0.40:8080. You should now see a page which displays ‘Hello World’ and the output from the console.log() statement in the /home/pi/node.log file.

 

To stop Node.JS just enter

 

sudo /etc/init.d/nodejs.sh stop

 

Using openzwave with Node.JS:

I currently use the module node-openzwave from GitHub. This can be installed using the instructions provided there, but if you’d want to recompile openzwave you’ll first have to install some additional packages.

 

sudo apt-get install subversion libudev-dev make build-essential git-core python2.7 pkg-config libssl-dev
sudo -i npm install node-gyp

 

Source (modified for my Raspberry Pi):

http://blog.rueedlinger.ch/2013/03/raspberry-pi-and-nodejs-basic-setup/

 

maybe not needed:

http://thomasloughlin.com/z-wave-controller-setup-on-my-raspberry-pi/

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply