10

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?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Shamoon
  • 41,293
  • 91
  • 306
  • 570

4 Answers4

13

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.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
5

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.

Rix
  • 1,688
  • 13
  • 20
2

Haven't used it but perhaps this is what you need: https://npmjs.org/package/pi-gpio

bethesdaboys
  • 1,797
  • 7
  • 22
  • 35
1

On Linux systems, you can do a lot of fun stuff just by interacting with the files on the procfs, sysfs and configfs filesystems, mounted at /proc, /sys and /sys/kernel/config mountpoints, respectively.

These allow you to observe your system's status and configuration and in many cases also provide mechanisms to alter that configuration by writing specific data to files. No C/C++ native addons required - the standard fs module will be quite enough.

As an example, take a look at the ledctl library that allows you to control your LEDs simply by reading and writing data to the LEDs' configuration endpoints on the sysfs mountpoint (Disclaimer: I am the author of the module).

If you would like to interact with your custom devices using the GPIO pins available on Raspberry PI, there are plenty of native addons for Node.js that provide a nice JavaScript API to send and receive signals on particular pins.

So, to create a device that detects a motion and emits a beep, you connect the motion detector and beeper to the GPIO (most likely), take control of the pins using one of your chosen GPIO modules and start listening for incoming signals. When you receive a signal, you emit another signal to the beeper.

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73