Create amazing Pi apps without writing code

Lead Image © Valery Kachaev, 123RF.com

Fun with Node-RED

, ,

Create super easy Pi programs to turn on lights, speak to you, or control motors.

Node-RED [1] is a visual programming environment that lets you create applications by dragging and dropping blocks (nodes) on the screen. Logic flows are created by connecting wires between the different nodes. We were amazed at how easy it was to get started creating programs. Node-RED also comes with web dashboards, so you can view data or control your projects from your smartphone.

Getting Started

Node-RED has been preinstalled on Raspbian "jessie" since the November 2015 version. The base Node-RED installation includes support for the Raspberry Pi (Rasp Pi) GPIO, but many other useful libraries can be installed, which you can find online with installation directions [2].

We had great success with the PiFace Digital and Pimoroni Explorer HAT libraries. Unfortunately, not all the libraries install cleanly (e.g., the Wiimote and Nunchuk library).

You'll need the Node.js package manager (npm) to load new libraries, so to install it, enter:

sudo apt-get update
sudo apt-get install npm

To check that Node-RED is installed and working, go to a Terminal window and enter:

node-red-start

Once Node-RED starts, you use a web browser to build applications. If you are working directly on your Rasp Pi, enter 127.0.0.1:1880 in the URL address box of your browser. Figure 1 shows the Node-RED development environment. You drop palettes from the left pane into the large flow window in the middle and wire them together in the correct order. Clicking the Deploy button in the upper right starts your program.

Figure 1: The Node-RED development environment.

As a first example, try connecting an inject node to a debug node (Figure 2). After the two nodes are dropped into the flow window, connect them together by clicking and dragging from the connection box at the end of the input element to the corresponding box on the output node. In the dashboard tab on the right, you tell the inject node what to do (here, we send a timestamp). After pressing the Deploy button, you can press the inject node, and a result should appear in the debug tab.

Figure 2: Connect an inject and debug node together.

Wake Me Up!

The goal of the "Wake Me Up" program is to set a wake-up time that will turn on the lights and say something like, "Wake Up, Wake Up." For this program, we used:

  • 1 PowerSwitch Tail II [3]
  • 1 Desk lamp
  • 1 Speaker

The PowerSwitch Tail II ($26) power cord is enabled and disabled with the Rasp Pi I/O pins. The PowerSwitch +in connects to pin 12 (labeled GPIO1 in Figure 3) and -in to GND on the Rasp Pi. A desk lamp plugs in to the PowerSwitch Tail, and speakers connect to the audio jack on the Rasp Pi (Figure 4).

Figure 3: Rasp Pi to PowerSwitch wiring.
Figure 4: Wake Me Up setup.

For this project, two Node-RED libraries are used:

  • A scheduler library – node-red-contrib-simple-weekly-scheduler
  • A text-to-speech library – node-red-contrib-say

To install these packages enter:

sudo apt-get install festival
cd $HOME/.node-red
npm install node-red-contrib-say
npm install node-red-contrib-simple-weekly-scheduler

After the packages are loaded, restart the Rasp Pi and start up Node-RED. On the Node-RED configuration web page, wire an rpi gpio and a say node to the Scheduler node (Figure 5).

Figure 5: Scheduler, rpi gpio, and say nodes.

Double-click the Scheduler node and set the wake-up times. The start/end payloads are numeric, with values of 1/0 (Figure 6). For the Rasp Pi GPIO node, set the GPIO to pin 12 (manufacturer's GPIO18; Figure 7). Next, for the say node, enter the text you want spoken (Figure 8). For the final circuit, we added inject nodes for testing. An On inject sends a 1, and an Off inject sends a 0 (Figure 9).

Figure 6: Scheduler node configuration.
Figure 7: Rasp Pi GPIO node configuration.
Figure 8: Say node configuration.
Figure 9: Final "Wake Up" logic with test nodes.

At wake-up time, the Schedule node sends a payload that triggers the say node to speak the wake-up text and the rpi gpio node to turn on the desk lamp for 15 minutes.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content