Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I program a Raspberry Pi with Node.js?

I want to learn to program Raspberry Pi's and I'm pretty good with Node.js. I haven't touched C++ in almost half a decade. I understand that I can load Linux on the Pi, but how do can I do my programming in Node?

If so, how do I handle things like input / output? If I wanted to make a simple device that detected motion and emitted a beep, for example, is this doable via Node.js on the Pi?

like image 297
Shamoon Avatar asked Aug 25 '13 02:08

Shamoon


2 Answers

Like Dave Swersky said in a comment, yes you can, there s a complete tutorial here: http://blog.rueedlinger.ch/2013/03/raspberry-pi-and-nodejs-basic-setup/

I would add it work well, but you ll need to use Leafpad (if GUI) or nano to edit your code, they are good text editor, but no syntax coloration.

EDIT: For thoses who don t want to see the link, here a quick resume of it:

Creating a new directory for node:

sudo mkdir /opt/node

Get the package for Raspbian: (vX.XX.X is to be replaced by latest one)

wget http://nodejs.org/dist/vX.XX.X/node-vX.XX.X-linux-arm-pi.tar.gz
tar xvzf node-vX.XX.X-linux-arm-pi.tar.gz
sudo cp -r node-vX.XX.X-linux-arm-pi/* /opt/node

Add node.js to the PATH:

nano /etc/profile

Add this before 'export'

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

It is a rip off of the basic installation of node.js as explained in the link, I didn t writed it, but tested it successfully on two Raspberry.

For more information about why thoses command, and how to properly configure the RPi, go to the link, the real author deserve the credit.

EDIT 3 (Inserted before EDIT2 since more related to the question)

For the hardware io with the RPi, you can use the popular socket.io package, or some speciallized module as pi-gpio.

EDIT 2: For nano syntax coloration, copy this in a file named js.nanorc, at ~/ for this example Then use this command:

cp /etc/nanorc ~/.nanorc
nano ~/.nanorc

To create a user nano config file and edit it.

Read all option and uncomment those you want, I reccommend to activate:

set autoindent
set tabspace 4
set tabstospace
set whitespace " °"

So you have auto indent, and tabs are made of 4spaces, and by typing alt + P, you see all whitespace replaced by ° (only visual, they aren t replaced in the file)

Then, at the end of the file, type

include "~/js.nanorc"

So you now have coloration for javascript too.

like image 195
DrakaSAN Avatar answered Sep 28 '22 21:09

DrakaSAN


I think you need some C ported modules to control the hardware, but I don't know if there is any.

However you can take a look at Tessel which is an embedded development hardware specialized for JavaScript, so it's possible to run Node.js applications on your Pi to program it.

like image 31
Rix Avatar answered Sep 28 '22 19:09

Rix