• Energy monitoring – the Node-RED way

Energy monitoring – the Node-RED way

We released this week the new current-meter, a basic carrier board for panStamp made to interface to up to three current transformer. We explained in the wiki the nature of the readings provided by this board: three current readings in milliamps and the temperature on the board itself. We then commented at some point that transforming current into energy consumption in KWh needed some kind of calculation on the receiver side, so we are here to show an example of this transformation from Node-RED.

Node-red is a fantastic open source visual tool developed and released by IBM for IoT programming. If you already know Node-RED then you probably agree with me. If you don’t know it then I recommend you to install Node-RED on any computer and start playing with some of the available examples. Being Node-RED a web-based tool, you can also try with some of the Node-RED environments served from the Internet. This tool is becoming very popular and has already been integrated into hundred of commercial products for IoT and M2M monitoring.

The hardware

For this example we are going to install Node-RED on a Raspberry PI and made it run along with lagarto-SWAP, the whole connected via MQTT.

rpi_00

 

We are using our panStamp shield for Raspberry PI in this case but remember that you can also use a panStick connected to a USB port. The above picture refers to an old Raspberry board version 1 but the current example is run on a Raspberry PI 3. We’ll post an updated picture next week.

A panStamp NRG module assembled onto a current-meter board has been installed into our electric closet. RPi shield and current-meter communicate at 868 MHz, 38 Kbps, under the SWAP protocol.

 

current-meter_04

You are maybe wondering about that case enclosing the current-meter board… Well, it will be made available for purchase anytime soon. In the meantime, you can use current-meter with almost any plastic enclosure that you have around. Also pay attention to the power source. The following picture shows this more in detail:

current-meter_06

We decided to power the board from an external 5 VDC power supply in order to configure the device for reading AC current and transmitting every 10 seconds. Running this config from 4 AAA batteries would  give us an autonomy of just some weeks.

Installation

After installing Raspbian or Noobs on the Raspberry board simply enter the board via SSH:

Once into the Raspberry install the necessary packages to run Node-RED and lagarto-swap:

Install git. This is useful if you want to get the latest version of our Python tools, including lagarto-swap:

Install Node-RED:

 

Install npn (node-js installer) and node-red-contrib-ui, a fantastic open source GUI node for Node-RED developed by Andrei Tatar. This node will let us create beautiful user interfaces from the flow editor:

 

Install mosquitto, the MQTT broker where Node-RED and lagarto will connect to:

And finally install lagarto-SWAP. First clone the python_tools project into the /home/pi folder and then install the packages:

Now Node-RED and lagarto-swap are ready to run. However, we want to set the right serial port for the wireless shield so we need to edit python_tools/lagarto/lagarto-swap/config/serial.xml:

and set <port> to /dev/ttyAMA0:

We also want Node-RED and lagarto to “autostart” on boot. For Node-RED simply run the following command:

 

And paste this content into the new file:

Press Ctrl+x and save the file with “Y”.

Thant’s all. After this you can reboot your Raspberry PI. Node-RED and lagarto-SWAP should automatically start.

lagarto-swap

Open lagarto-swap from a web-browser (http://rpi_address:8001). In a network with just a current-meter you should see something like this:

lagarto-swap_curent-meter_01

Remember that you need to restart the current-meter board at least once in the presence of a new lagarto-swap running in the network. This step will make the board identify itself as a current meter. We suggest at this point to configure Location and Name for each endpoint in order to correctly identify the endpoints from Node-RED. After this, take a look at the MQTT/HTTP settings page. There is no need to change anything. Simply notice the location of the MQTT server (localhost) and the process name (Lagarto-SWAP):

lagarto-swap_curent-meter_03

You can also open the Device panel and configure the current-meter board to transmit at your desired interval (txInterval). We set this value to 10 seconds, as explained in a precedent section:

lagarto-swap_curent-meter_02

The flow

Open the Node-RED URL (http://rpi_address:1880) from your web-browser and import this flow into your blank work space:

node-red_current-meter_01

Blue nodes are from node-red-contrib-ui. They do not functionally impact the flow. Instead, they simply add a graphic widget to the user interface. Purple nodes are MQTT inputs. Each purple block represents an endpoint from lagarto-swap. The first MQTT input points to the temperature sensor of the current-meter board. Double-clicking the block opens this dialog:

node-red_current-meter_02

Server is the address of the MQTT broker. 192.168.1.64 is the local address of our Raspberry PI, as well known as “localhost”. Keep the default port. You also need to enter the MQTT topic for the endpoint and a name for the block. Remember when we checked the process name in the precedent section (“Lagarto-SWAP“) is this process name but we also need to use the endpoint location and endpoint name to build the topic. Following the format described in the wiki:

[lagarto-swap process name]/simple/status/[endpoint location]/[endpoint name]

We only need to enter our information in the above fields. For our temperature endpoint:

Lagarto-SWAP/simple/status/current-meter/temperature

And the same applies for all three current channels:

node-red_current-meter_03

At this point we could simply connect every MQTT input to a UI widget but here we want to take the RMS current readings and transform then into a total KWh value. Taking the first channel as a reference:

node-red_current-meter_04

There are three function blocks per current channel. “mA to Amps” is a basic converter between milliamps and Amps. Double-clicking the block will open the contained code:

msg is the object passed to the function by its preceding block. In this case msg is the MQTT message received from lagarto-swap. msg.payload is the value of the RMS current so we need to divide this value by 1000 in order to get he value in Amps. “mA to KWh” is a bit more complicated:

Here we need to play with global variables, transform current into active power by multiplying the RMS current value by the theoretical AC voltage (230 VAC in most EU countries) and an estimation of the power factor (0.8). After getting the KW value, we need to multiply by the number of hours elapsed since the last reading. Then this partial KWh value is accumulated into a global variable (kwh). In the above example we are assuming that RMS current readings are received every 11 seconds (10 seconds of transmission interval plus 1 second for the delay due to the ADC work in the current meter board). We can replace this assumption by some work with the JavaScript Date object.

“this month” is our last function block into each channel flow:

This function takes different variables from global space and detects when the month has changed. This event is used by the block to convert total KWh consumed into a partial monthly reading. When the month changes the monthly KWh value is then returned to zero.

There is a last function block taking partial KWh readings of each channel from global space and making a total value of the energy consumption:

All these function blocks are passing partial and total results to different UI nodes. Each UI node will add a graphic widget to the panel after “deploying” the flow.

The UI

After importing the flow and customizing the MQTT inputs you can press “Deploy” and start checking the UI outputs from a web browser (http://rpi_address:1880/ui). This is a screen shot of our graphic panel after running the Node-RED flow for some minutes:

nodered-ui_01

 

Andrei has done a fantastic work with this node library. UI’s are perfectly responsive and adapt themselves to smaller screens (smart phones and tablets) without problems. Graph blocks buffer data for preconfigured periods of time so you don’t need to store endpoint data into a database prior to its representation. Numeric values can be displayed on gauges or text/numeric widgets and binary controls can take form of buttons or switches. But the most important is that you can fully integrate your GUI with powerful data flows and lots of endpoint sources, including MQTT, HTTP and RS232.

 

Comments are closed.