Monday, April 24, 2017

Node Red Dashboard for Raspberry Pi

What is Node Red?




Node-RED is a programming tool for wiring together hardware devices, APIs and online services. It was developed as a visual programming tool for the Internet of Things. It also allows you to produce and publish a funky web based dashboard with one click.


Node-RED includes a browser-based editor that makes it easy to wire together flows using the selection of nodes in the side menu. Flows can be then be deployed to run in a single-click. JavaScript functions can be created within the editor to customise the messages passed between nodes. A built-in library allows you to save useful functions, templates or flows for re-use.

The light-weight runtime is built on Node.js, taking full advantage of its event-driven, non-blocking model. This makes it ideal to run on low-cost hardware such as the Raspberry Pi as well as in the cloud.

Nodes can be anything from a timer to trigger events to a Raspberry Pi GPIO output used to turn on a LED (or salt lamp in our example). With over 225,000 modules in Node's package repository, it is easy to extend the range of nodes to add new capabilities. As we will demonstrate there are packages available for the Raspberry Pi and Sense Hat. The flows created in Node-RED are stored using JSON.

Node-RED was developed by IBM and in 2016, they contributed Node-RED as an open source JS Foundation project.

The Himalayan Salt Lamp Project




My wife likes salt lamps. Salt lamps allegedly remove dust, pollen, cigarette smoke, and other contaminants from the air. How effective this is I don't know and it is really irrelevant, as I said my wife likes them! Salt is very hygroscopic, that is it absorbs water - this is the basis of the claimed health benefits, the salt also absorbs any foreign particles the water may be carrying. The water then evaporates when the lamp is switched on leaving the contaminants behind entrapped in the salt.

Salt is so hygroscopic that it readily dissolves in the water it absorbs: this property is called deliquescence. It is of course a problem if your expensive Himalayan salt lamp dissolves into a puddle of salty water, especially if it is connected to 240VAC. In our house this melting process starts at relative humidities above 70%.

The solution is to turn your lamp on if humidity gets above 70%. This seemed like a good excuse to introduce the start of our home automation hub and learn about node_RED. Turning a lamp on and off based on humidity and time (lamp goes on at 5pm and off at 10pm) is trivial using Python so we wont cover that. What we will look at is manually controlling the lamp via our node-RED dashboard and other associated data we display.

Node-RED and the Raspberry Pi




If you are running Raspbian Jessie on your Pi then you should already have node-RED installed. Before starting the node-RED server it is worth installing a few packages that you will need. Type the following at the CLI:

sudo apt-get update
sudo apt-get install npm
cd $HOME/.node-red
npm install node-red-dashboard
npm install node-red-node-snmp
npm install node-red-contrib-os

Node-RED is started by running the following command in the terminal:

node-red-start
Once started, you use a browser (either on the Pi or remotely) to build your applications and configure your dashboard. I used my Macbook Air, to do this point your browser at the ip address of your Pi:1880. If you do it on your Pi, the URL would be 127.0.0.1:1880 or localhost:1880. The associated dashboard URL is <IP Address>:1880/ui. So for example my Raspberry Pi dashboard is at http://192.168.0.18:1880/ui.

Most of the Raspberry Pi information charted in the dashboard shown above is from the node-red-contrib-os package. For example information on the SD Card is from the Drives node. You use this node to query the hard drives. Values for size, used and available are expressed in KB (1024 bytes). Value for capacity is a number between 0 and 1. Capacity*100 is also known as percentage used.

Some of the flows are shown below. The first step is to drag across a timer which you can use to poll the Drive node. Our timer sends a timestamp every minute.

Connect the timer to a Drive node and it will start pumping out messages with the size, used, available and capacity values for every drive on your target system. You can use a Debug node to see messages being sent out by any node. This is very useful in debugging your flows. On the Raspberry Pi there will be a few different file systems on your SD Card so you have to be specific about which area you want information about.


You can add a Function node to include custom JavaScript to process the messages passed between the nodes. The JavaScript used to extract the various Drive information that I use is shown below. The topic variable is used as the name for charts with multiple inputs.

var msg1,msg2,msg3;

if (msg.payload.filesystem === '/dev/root') {

    msg1 = { payload: msg.payload.used };
    msg2 = { payload: msg.payload.available };
    msg3 = { payload: msg.payload.capacity * 100 };

    msg1.topic = "used"
    msg2.topic = "available"
    msg3.topic = "capacity"

}

return [ msg1, msg2, msg3 ];

CPU Temperature




To display CPU temperature we use a different technique. On the Raspberry Pi you can display the current CPU temperature by typing:

/opt/vc/bin/vcgencmd measure_temp
You can use an Exec node to run OS commands. So connect our same timer node to an Exec node and input the command above. We then have to do a bit of processing to extract the temperature as a number. Use another function node with the following code.

msg.payload = msg.payload.replace("temp=","").replace("'C\n","");

return msg;



There are also nodes available for the Sense Hat. You need to use functions similar to those above to extract the various sensor data values.


Controlling GPIO using the Dashboard



Manual control of a GPIO is fairly straight forward. The one trick is that the Switch node outputs true/false and the Raspberry Pi GPIO out node expects a 1/0 input. So we include another Function node to mediate. The relevant code is:

msg.payload = msg.payload ? 1 : 0;

return msg;

Of course our Raspberry Pi outputs 3.3VDC which wont turn on a 240VAC lamp so we use a PowerSwitch Tail kit as an intermediary.





Saturday, April 8, 2017

STEMTera (Arduino Breadboard) Tutorial

What is the STEMTera?



STEMTera was the first project that I have supported on KickStarter and the experience has been overwhelmingly positive. So what is STEMTera?

At its simplest the STEMTera is a breadboard with an embedded Arduino UNO. Most shields will plug straight in. But it is more than just a simple Arduino prototyping platform, it also includes:
  • a LEGO® compatible bottom which allows you to mount it directly on your LEGO creation.
  • An ATmega32U2 microprocessor which is exposed, users can develop native USB projects with an extra 21 IO pins. These extra IO pins can work directly with the LUFA framework. More on this below.
  • Multiple IDE support including Atmel® Studio, Arduino IDE, AVR-GCC, AVR-GCC with LUFA, Scratch, etc.
  • Embedded LED's to indicate Power on, Tx, Rx and and one connected to D13 for your own use.
The Arduino functionality is the same as for an UNO, plug the USB port into your computer and away you go. The ATmega32U2 functionality is new and deserves a bit more explanation.

ATmega32U2


The newer Arduino Uno boards have two programmable microcontrollers: one is ATmega328, which is the Arduino processor that you usually upload your sketches to, and the second is the ATmega16U2, which is flashed to operate as a USB to Serial converter.

The ATmega16U2 chip on the Arduino board acts as a bridge between the computer's USB port and the main processor's serial port. Previous versions of the Uno and Mega2560 had an Atmega8U2. It runs firmware that can be updated through a special USB protocol called DFU (Device Firmware Update).

As part of the STEMTera KickStarter campaign there was a stretch target which if met would result in the ATmega16U2 being upgraded to the ATmega32U2. This target was met and so the upgrade was incorporated into the finished product. Even better, the ATmega32U2 pins have been brought out to the breadboard so that you can utilise them.

By updating the ATmega32U2 firmware, your STEMTera can appear as a different USB device (MIDI controller, HID, etc.).

DFU Programmers




To update the firmware on the STEMTera ATmega32U2 you will need a DFU Programmer.

Windows: Download Atmel's flip programmer.

Mac: Install MacPorts: Once MacPorts is installed, in a Terminal window, type

sudo port install dfu-programmer
NB: If you've never used sudo before, it will ask for your password. Use the password you login to your Mac with. sudo allows you to run commands as the administrator of the computer

Linux: from a command line type

sudo apt-get install dfu-programmer

Enter DFU mode


To enter program (DFU) mode you need to short the ATmega32U2 ICSP reset pin to ground until the red LED starts to flash.

Flash the chip


Windows: use flip to upload the hex file to your board

Mac & Linux: from a terminal window, change directories to get into the folder with the firmware. If you saved the firmware in your downloads folder on OSX, then you might type:

cd Downloads/
Once there, type:

sudo dfu-programmer atmega32u2 erase
When this command is done and you get a command prompt again, say you want to reflash the original Arduino firmware (Arduino-usbserial-uno.hex), then you would type:

sudo dfu-programmer atmega32u2 flash Arduino-usbserial-uno.hex
Finally:

sudo dfu-programmer atmega32u2 reset