Node.js, GPIO and the Raspberry Pi

This post is an excerpt of the Chapter 4 “Getting Started with Embedded Systems” in Building the Web of Things, a Manning book by Dominique Guinard and Vlad Trifa.

In a previous post we talked about the increasing importance of Node.js for embedded systems in the IoT but we didn’t have a concrete look at what using Node.js on an embedded device looked like. In this post we’ll look at what it means for the Raspberry Pi.

Installing Node.js on a Raspberry Pi

The first step is to install Node on your Pi. The latest versions of Raspbian (the Pi OS) come bundled with Node.js already however the installed version is usually quite outdated. Getting the latest version is pretty straightforward but you’ll need to install a special version of Node. “Why can’t I just use the standard Node.js version?” we hear you say. Well, the Pi — and generally most of the embedded devices out there —run on CPU architectures that are not compatible with the ones your PCs run on (x86 or x64). More precisely a lot of embedded devices run on ARM processors (which is what’s on your Pi). As a consequence the node binaries that you need on your Pi are not the same as the ones you need on your PC. Fortunately enough, since Node version 4.0.0, ARM binaries are also available from the official Node web site. To install the ARM version of Node.js go to the Node download page and select the right version for your Pi (or other embedded device). As shown in the figure below the archive to download depends on the version of the ARM architecture your device uses. As an example, the Pi 2 runs on a ARMv7 architecture while the Pi Zero runs on a ARMv6 architecture.

Node.js Downloads for Raspberry Pi

Node.js downloads for Raspberry Pi

Once downloaded you can install it via:

Sensors, Actuators and GPIOs

With Node.js installed, your Pi is now ready to conquer the World Wide Web of Things, but it doesn’t have much to work with in the real world yet. To make it more real-world connected you’ll connect some sensors (for example, a humidity sensor) and actuators (for example, an LED) to the Pi.

The way to do this on most embedded platforms, Pi included, is to connect the sensors and actuators using the general-purpose input/output (GPIO) ports. Essentially, a GPIO is a pin on which current can be read or outputted. GPIOs have two modes: an input mode and an output mode. When the output mode is selected, the pin can be set to HIGH, which means it outputs 3.3 volts; when the pin is set to LOW, it is off and does not output any voltage. With the input mode you essentially can read a value on the pin. Unlike other embedded platforms (such as Arduino), the Pi supports only digital input. What this means concretely is that you can work only with components that supply series of 0s (LOW, ~0 volts) or 1s (HIGH, 3.3 volts) to the input pins, that is, with digital components. As an example, an LED is a digital actuator and a button is a digital sensor.

Back to our GPIOs: the numbering and type of each GPIO differs depending platform you’ll choose to use. For instance this is the layout for a Pi Zero or a Pi B, Pi 2 and Pi 3.

GPIO layout for the Raspberry Pi

Figure 1 – GPIO layout for the Raspberry Pi

or the layout for the Intel Edison when using the mini prototyping board:

GPIO layout of Intel Edison board

Figure 2 – GPIO layout of Intel Edison board

Your first circuit using GPIOs: LEDs

If you’ve never worked with the GPIOs of embedded devices let’s start with the Hello World of embedded : blinking LEDs! For the sake of simplicity we’ll assume you have a Pi in the rest of the post but you can also implement this with an Intel Edison, Beaglebone, Samsung ARTIK or any Linux-based embedded device you can think of!

Start by placing the elements on the breadboard, as shown in the figure below. If it’s the first time you work with electronics, we recommend you use an anti-static mat or a grounding strap to avoid damaging your embedded device.

Connect an LED to the Pi GPIOs

Figure 3 – Connect an LED to the Pi GPIOs

GPIO and Node.js

The hardware is now ready, so you can start working with GPIOs! You can find a dozen Node.js GPIO libraries for the Pi, offering different abstraction layers and functionality. We decided to use one we really like called onoff. Create a new folder and install onoff with NPM
npm install onoff --save

You’re now ready to interface the Pi with sensors and actuators. In software engineering, the simplest piece of code one can write—the famous Hello World—is a piece of code that displays “Hello World” in the console. The Hello World equivalent of the IoT is to make a real LED blink, so let’s build exactly that as shown in the next listing.

As mentioned before, you’ll use pin 7, corresponding to GPIO 4 (see Figure 1 above). You’ll then create a function that opens the pin in output mode (which means you’ll “push” current on it). It then either activates the pin or deactivates it (depending on the result of the modulo function) and launches itself again at a specified interval. Finally, you listen for SIGINT, which corresponds to pressing Ctrl-C, and ensure that you release the pin and turn the LED off before exiting.

That’s it for this introduction but should you want to learn more our latest book: Building the Web of Thing covers how to integrate other sensors and actuators such as a passive infrared sensor (aka PIR) or a temperature and humidity sensor. Furthermore, you’ll discover how to integrate these sensors to Web with protocols such as HTTP, WebSockets, CoAP and MQTT.

You may also like...

16 Responses

  1. Wonderful beat ! I wish to apprentice while you amend your web site, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear concept

  2. Your place is valueble for me. Thanks!…

  3. Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it

  4. What i don’t understood is in truth how you’re not really a lot more well-liked than you might be right now. You are very intelligent. You realize therefore significantly in the case of this matter, produced me in my view consider it from so many various angles. Its like men and women don’t seem to be involved until it is one thing to do with Girl gaga! Your personal stuffs outstanding. At all times take care of it up!

  5. QQ188 says:

    I haven¦t checked in here for some time as I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

  6. turn dispos says:

    Hi my friend! I want to say that this post is awesome, nice written and come with almost all significant infos. I’d like to peer extra posts like this.

  7. You made certain fine points there. I did a search on the matter and found mainly persons will agree with your blog.

  8. Very interesting subject, appreciate it for posting. “Nothing great was ever achieved without enthusiasm.” by George Ellis.

  9. I have recently started a web site, the information you provide on this website has helped me greatly. Thanks for all of your time & work.

  10. I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again!

  11. It’s a shame you don’t have a donate button! I’d definitely donate to this brilliant blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this blog with my Facebook group. Chat soon!

  12. rodaslot88 says:

    great post, very informative. I wonder why the other experts of this sector don’t notice this. You should continue your writing. I am confident, you have a great readers’ base already!

  13. I’m curious to find out what blog platform you have been utilizing? I’m experiencing some minor security issues with my latest site and I would like to find something more safeguarded. Do you have any solutions?

  14. I dugg some of you post as I thought they were very helpful very useful

  15. I think this internet site holds very wonderful written content material content.

  16. I like this blog very much, Its a real nice situation to read and incur info . “The superior man is modest in his speech, but exceeds in his actions.” by Confucius.

Leave a Reply

Your email address will not be published. Required fields are marked *