The Internet of Things is all the rage right now. There are so many ideas we can put into action in the realm of physical computing, it is easy to be drawn into the idea of programming the world we live in! Once you have a Raspberry Pi and a breadboard, what's next?
In this article, we will explore how to access the GPIO pins on a Raspberry Pi using Node.js. With the GPIO pins, you can program the hardware directly. JavaScript APIs make this seamless. The APIs are abstractions to common techniques, and they are available from anywhere. The Node.js interpreter runs in a single process which opens up ways to write this code in a way that it is testable. The most exciting part for me is that you can write unit tests, hit breakpoints and examine code just like any other JavaScript program, all from your computer.
Let's get started.
What Is GPIO?
GPIO stands for General Purpose Input / Output. They are the pins found on the side of the Raspberry Pi, next to the yellow video out socket. Below is what they look like.
[caption id="attachment_134480" align="aligncenter" width="1000"] Source: Raspberry Pi[/caption]
Think of them as the way you connect to the outside world from the Pi. This enables you to write programs that do not run on a computer screen. Each pin acts like a switch that you turn on or off. You can receive input from the physical world, or send output. Basic boards come with 26 pins, and 9 of those pins are power or ground pins. The ground pins are at the end of each circuit that the current has to flow through. The more recent Raspberry Pi boards come with an extra set of 14 pins.
If you are interested in more details on the GPIO pins, this online diagram gives you all you need to understand what each pin is for. There are a myriad number of pins for input / output and ground. These pins are the foundation of physical computing. Depending on your goal, you can use as many as you need.
Mock the fs
!
I know what you are thinking, what the heck is fs
and why do I care? In Unix-like operating systems, a device file is a driver that looks like a file. In laymenʼs terms, a device driver is a file! Guess what? GPIO APIs are wrappers that read or write to a device file. The file system APIs are concepts that may already be familiar to you. If you have never worked with files in Node.js, I recommend going over the fs
module and file systems in Node.js. fs
is shorthand for "file system" and enables you to read or write to a plain old file. There is nothing fancy here, all we do is writeFile()
, for example, and let GPIO handle the rest. The trick is to know what to write on which file.
There is a handy little npm package called mock-fs that will help us with unit tests. With this library, one can dream up any file on the file system and mock it in memory. What is so radical is we are only dealing with files, thatʼs all we need to do. In a Unix-like system, GPIO behaves like any other plain old file. This gives us freedom on how we can approach this solution.
The crux of the mock-fs
library is the mock({})
function. It takes in a single parameter which is a JavaScript object. Inside this parameter, one can dream up whatever file you want. The beauty here is that this all runs in memory, so you can go crazy with unit tests. The interpreter runs in a single process, this means one can override the fs
module at runtime. JavaScript is a dynamic language, so we are free to mock any module available to the current process.
Continue reading %Getting Started with the Raspberry Pi GPIO Pins in Node.js%
by Camilo Reyes via SitePoint
No comments:
Post a Comment