You are on page 1of 5

handson.dmt.fh-joanneum.

at

RASPBERRY PI
Workpackage: Description: Setup Node.js on the Raspberry Pi In this work sheet I will introduce you to some advantages of node.js. There will be also an instruction how to install node.js on your Raspberry Pi. 4 What node.js can be used for What is sudo? Install Node.js Standard-Editor nano Online-Editor PiJS Install Libraries quick2wire-gpio-admin Trouble Shooting Useful Resources Hardware: Raspberry Pi and Standard Equipment Software: can be installed with commands if you have internet access

Difficulty (1-10): Overview:

Requirements:

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

SETUP NODE.JS ON THE RASPBERRY PI


If you want to develop on the Raspberry Pi, you can choose between several languages. In this workpackage I am going to describe how to install node.js on your Raspberry Pi and how to add other packages as extension to it.

What node.js can be used for


Node.js is primary a framework for developing server-side web applications. But you can also use it to write some code which is accessing the GPIO pins of your Raspberry Pi or other applications which are not necessary server-side web applications. In the following worksheets we will use node.js simply to access and regulate connected elements which are connected to the Raspberry Pi. But the main advantages of node.js are when you use it for modern web-applications. Node.js follows a different approach then other web applications. Instead of using an own thread for every request, all requests are processed in one single thread. To prevent the thread from blocking, the work has to be delegated to other resources. Most web applications are dependent of external resources like file system or databases. Requests to this external resources are mostly time-consuming. The concept of node.js benefits from that, because node.js puts a request aside until there is a reply from the external resource that their work is done. In the meantime node.js can do the same with all other requests and the thread is never blocked by waiting for a reply of external resources. Another example for using node.js is streaming.

What is sudo?
If you want to install something or change some settings which need sudo (admin) rights you might need the sudo command to do that. sudo allows a permitted user to execute a command as the superuser (admin). If you log in with default user pi this user has sudo rights. Anyway you have to write sudo in front of some commands, but you do not need to enter your password. When using another user, which might not have sudo-rights you will need the password of a superuser. If you have problems when executing a command, try to write sudo in front of it. If there are problems when executing your code you can try to use quick2wire-gpio-admin as explained at the bottom of chapter Instructions.

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

Instructions
Installation
You should always update your installed packages before you are installing something new. To do that open your command line and type in:
(sudo) apt-get update

downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies
(sudo) apt-get upgrade

will fetch new versions of packages existing on the machine if APT knows about these new versions by way of apt-get update After that you can start installing node js. The latest version can always be found on http://nodejs.org/dist/. There always exists a special version for the Raspberry Pi. But its not always available for the latest version. Navigate through the latest versions and search for something like node-v****-linux-arm-pi.at.gz. Then follow the following steps: -

create a new directory


(sudo) mkdir /opt/node

download package (as version-number use the latest one)


(sudo) wget nodejs.org/dist/v0.10.21/node-v0.10.21-linux-armpi.tar.gz

unpack package
(sudo) tar -xvzf node-v0.10.21-linux-arm-pi.tar.grz

copy unpacked folder to /opt/node


(sudo) cp -r node-v0.10.2-linux-arm-pi/* /opt/node

Add node.js to path variable o (sudo) nano /etc/profile o add following lines to the configuration file before the 'export' command
... NODE_JS_HOME="/opt/node" PATH="$PATH:$NODE_JS_HOME/bin" export PATH ...

logout and login again so that changed path variable will work
logout

After that node should be globally installed on your Raspberry Pi and should be available from everywhere in your file system.

Start Programming Editors for node.js


Standard-Editor nano
To start programming you only have to create a file with the ending *.js:
(sudo) nano filename.js

Open this file and write your code. There are additional functions available, you can list them by using CTRL+G. To return to your code use CTRL+X, which also closes the editor when you are on main screen of nano. After you have written your code and closed the editor you can run your file by typing in:
(sudo) node filename.js

Online-Editor
If you do not want to install node.js on your Raspberry Pi you can use PiJS. You can write your javascript-code in any browser on your Raspberry Pi. It deploys automatically to your Raspberry Pi and restarts also automatically if you reboot your device. There is also a webconsole where you can watch whats going on in your program. But if you want to use libraries it is easier to develop directly on the Raspberry Pi.
Carelse, Pauger, Pilz 3

Instructions

handson.dmt.fh-joanneum.at

You can find more information of how to use that editor on http://www.tbideas.com/blog/2013/04/NodeJS-Raspberry-Pi-The-easy-way/

Desktop-Editor
To save performance it is better to use one of the other editors listed above.

Test if installation of Node.js was successful


-

create a file with filename.js insert this line in the file:


console.log(Hello World!);

save the file run the file


(sudo) node filename.js

if the output is Hello World! node.js was successfully installed

You can also type in your command line node --version. This will return you the version of your installed node.js and if it returns that, it is also sure that node.js was installed.

Install Libraries
You should always work in the same directory when you program with node.js. In this directory you should install all libraries you would need and then you can access them there. Every time you need to require something in your code, you have to install it first. Example: var Gpio = require('onoff').Gpio; Here you require the package onoff. To make it available on your Raspberry Pi, you have to install it first. To do that, go to your working directory and type in the following command:
[sudo] npm install onoff

This command will install the library onoff in the folder node_modules in your directory. From now on you can require it from every file in your working directory. If you want to install another library you can simply replace the onoff with the name of the library you want to install.

quick2wire-gpio-admin
If you have problems running your application with sudo-rights you can use quick2wire-gpioadmin. If you have not installed Git on your pi before user the following command to do so.
sudo apt-get install git-core

Then install the quick2wire package with the following command.


git clone git://github.com/quick2wire/quick2wire-python-api.git

After that navigate into the folder quick2wire-python-api and type in


make sudo make install

If you use user pi you do not have to do the following step which adds your user ($USER) to the gpio group.
sudo adduser $USER gpio

This enables you to export and unexport special pins so that you do not need sudo rights any more to access the pins. To use a pin in your code run the command with the pin-number (here 22) before running your code.
gpio-admin export 22

If you are done unexport all pins again.


gpio-admin unexport 22

Carelse, Pauger, Pilz

Trouble Shooting

handson.dmt.fh-joanneum.at

Trouble Shooting
If you have problems with the installing commands, first check if you have internet connection. Be always sure that you install a package in the same folder as you want to develop. Otherwise the require statement cannot find the package and you cant use it.

Useful Resources
Node.js Download: http://nodejs.org/dist/ Node Packaged Modules/Libraries: https://npmjs.org/ Tutorial: http://www.nodebeginner.org/ Editors: http://www.tbideas.com/blog/2013/04/NodeJS-Raspberry-Pi-The-easy-way/ http://www.raspberrypi-spy.co.uk/2013/11/quick-guide-to-nano-text-editor-on-the-raspberrypi/ http://pijs.io/

Carelse, Pauger, Pilz

You might also like