Fun with Node-RED
Create super easy Pi programs to turn on lights, speak to you, or control motors.
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.
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.
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.
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:
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).
For this project, two Node-RED libraries are used:
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).
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).
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.
Pages: 6
Price $15.99
(incl. VAT)