Interfacing with a Raspberry Pi using NodeJS

Well…. I’ve been gone waaaaayyyy too long.  I’ve been on an extended hiatus while my personal life self-destructed, then began to rebuild, then had a serious car accident, then ended up battling major health issues (chemo is a bitch), and then the road to recovery.  The good news is, my hands have been anything but idle.  During all that I pushed out another release of my MonoPi framework, a joystick library for the Arduino, another release of my CrystalFontz LCD library for Arduino, and finally: a NodeJS framework for the Raspberry Pi, which is essentially a port of my MonoPi framework to NodeJS.  I call it jsRPi.  Yes, there are a couple other Node modules that exist for interfacing with a GPIO, but with all due respect to their authors, I find them lacking for my purposes.  MonoPi and pi4j offer a wide breadth of functionality that the existing modules for Node simply don’t have.  All of the existing modules I found offered only the most basic read/write capability for the GPIOs.  jsRPi, on the other hand offers that plus various system utilities, hardware abstractions for relays, motors, LEDs, etc and device interfaces for PiFace, PiCamera, PiBrella, sprinkler control, etc.

The reasons I decided to take on such an ambitious project were:

  • I wanted to teach myself NodeJS (and become more proficient with JavaScript in the process).  Porting a large framework like MonoPi seemed like a good way to do this.
  • No other modules/libraries/frameworks as extensive as MonoPi exist for Node.
  • I ultimately intend to write a web app using NodeJS and Express and React.  Being able to write the entire thing in JavaScript would be a huge win.

Getting Started

Head over to my github repo and download the source or clone the repository, then from a command line, navigate to the jsRPi folder (as of this writing, the framework is still an Alpha so I have not published an npm module yet).  Then follow the instructions in the README to install dependencies and build the source.  I should note that the term “build” when in comes to jsRPi really just means check all the syntax, generate API documentation, run unit tests, and produce an optional version of the framework “compiled” into a single file.  Build tasks are executed using Grunt.  If you don’t already have Grunt and grunt-cli, it will be installed when you run the appropriate install_deps script.  You can also run just the unit tests by entering the following command from the command line (from within the jsRPi root directory):

 

1
grunt nodeunit

 

Blink Example

The following example shows how you can blink an LED.  This example assumes you have an LED and appropriate resistor connected to GPIO-00:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'use strict';
var jsrpi = require("jsrpi");
var GpioPins = jsrpi.IO.GpioPins;
var PinMode = jsrpi.IO.PinMode;
var PinState = jsrpi.IO.PinState;
var GpioStandard = jsrpi.IO.GpioStandard;
var LEDComponent = jsrpi.Components.Lights.LEDComponent;
 
// Setup the pin.
var ledPin = new GpioStandard(GpioPins.GPIO00, PinMode.OUT, PinState.Low);
 
// Create an LED and attach it to the pin.
var led = new LEDComponent(ledPin);
 
// Blink the LED once per second, for 5 seconds. This is an asynchronous operation.
led.blink(1000, 5000);
 
// Destroy the LED. Note: You do not need to call dispose() on the pin. It gets disposed by LEDComponent.
setTimeout(function() {
   led.dispose();
}, 6000);

Again, this framework is currently an Alpha release, so I expect there are probably bugs, particularly in the parts I have not yet been able to test for lack of required hardware. Future plans include, refactoring to be ES6-compliant, include callbacks as optional parameters for asynchronous methods (WHY ON EARTH I didn’t do this in the first place is beyond me. Hence, disposing the LED using setTimeout() shown above), and hopefully native RS-232 serial support (present in MonoPi, but not yet ported to jsRPi). I should also mention that I’ve already begun porting to PHP as well. So keep your eyes peeled for “PHPi”, coming soon!

All of this is part of an even larger project that I’ve been prototyping with quite a bit of success:

A home automation controller using an Arduino Mega 256 w/Ethernet Shield to control power outlets (which mostly just control lights), and a REX (Request to EXit sensor) , which is just a PIR (Proximity Infra-Red) sensor that triggers an onboard relay.  The Arduino has its own built-in web interface for configuring settings like the network interface (static IP or DHCP, etc), and ultimately communicates with a Command And Control server running on a Raspberry Pi with Mosquitto (an MQTT service), which I use to send commands to and get status from, the Arduino.  Then a small web app also runs on the Pi that communicates with Mosquitto to provide a GUI that can run from a phone, tablet, laptop, whatever and control the lights. It also has an “away” mode which makes use of the REX. When you leave, you put it in “away” mode and then walk out the front door.  When you come back through the door, the REX sees this and switches on the lights for you automatically and switches “away” mode back off.  Pretty slick.  All of the pieces of this project will eventually be detailed here and published to my github page as I complete them.  Stay tuned!

Facebooktwittergoogle_plusredditpinterestlinkedinby feather
Tuesday, November 24th, 2015 Arduino, JavaScript, NodeJS, Raspberry Pi

Leave a Reply

You must be logged in to post a comment.