Professional Documents
Culture Documents
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
Requirements:
Instructions
handson.dmt.fh-joanneum.at
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.
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: -
unpack package
(sudo) tar -xvzf node-v0.10.21-linux-arm-pi.tar.grz
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.
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.
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
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
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/