Pimoroni Enviro+ Project: Using Grafana to visualize data via MQTT to Node-RED to Influxdb

Published On: September 12, 2021Categories: Uncategorized

If you are getting started with the Raspberry Pi you can find a lot of great projects on the Internet, and peripherals to make things interesting. One outfit, Pimoroni, has been making Pi parts for a while, including custom electronics in the form of HATs and pHATs. These well done projects have Python libraries to bring the components to life.Enviro+ image

Having a project you are interested in is the best way to learn something new. If you have just started with your Enviro+, or you are looking for something to do with it next, here is a quick recipe to visualize your Enviro+ data in Grafana, so you can end up with something pretty like this:

screenshot

Step 0

Before starting you will need to have your Raspberry Pi up and running. I chose a Pi Zero W with-headers, and a 32G SD card to host the Enviro+ pHAT, but the dashboard and other services are running on a Raspberry Pi 4 4G with a 32G SD card.

We are not going to start at the beginning, so you RPi needs to be set up. If you have followed Pimoroni’s instructions and run the examples, your Enviro+ is ready for what we will do next. The RPi 4 that will host the services will also need to be up and running. Mine is used as a desktop, and I like to VNC to it a lot of the time but all of the setup is done at the CLI.

Step 1: Install & set up the necessary software

I am not going to retread all the great guides already out there, but here is a list of what is needed:

Install on Pi Zero/Enviro+:

    • mosquitto_clients

Install on console:

    • mosquitto & mosquitto_clients
    • Node-RED
    • influxdb
    • grafana

I followed several guides found through searches to learn to install these and set them up as services. This is a good article that covers Influxdb and Grafana. It also covers topics outside this discussion (booting from SSD, which is fixed now on the Pi 4, btw) and it also installs Telegraf, which can be useful later for having the Pi 4 monitor itself. But, it is one of many articles that will show you how to set up a new repository, install the apps and, for Influxdb, get your first database created. In my setup I have created a database called ‘enviro’.

Node-Red is a great visual programming tool, and I am using it as a cheat. I was trying to get data from MQTT to Influxdb via Python, but could not figure it out quickly. Node-Red, on the other hand, can do this easily. I will eventually need to get back to that Python code, but for now, let’s focus on graphing our Enviro+ data. To install Node-RED follow their documentation. Make sure to do the Next Steps at the bottom of the install page. You will need to know how to add nodes and connect them together.

There are a ton of guides online about installing the mosquitto MQTT broker on the Raspberry Pi. Most of them will have you interacting with mosquitto at the cli by the end.

I modified the original Pimoroni example script mqtt-all.py, and created mqtt-all-no-display.py, stripping away stuff so the display was not left on all the time. A great next-project would be to use the proximity sensor on the Enviro+ to turn the screen on, and have it timeout after a while. But turning off the screen is not the most important thing we need to do with the script, we need it to run as a service when the RPi starts up. This is one way to do that.

Now that we have that mess in place, we should be able to use mosquitto_sub to follow along on our ‘enviro’ topic at the cli, and see that everything is in place to start logging and graphing the data.

Last Step: The Flow

Ok, I just went through having to set this up again and realized I could not easily find the original source that had shown me the technique that I used. It was not written down anywhere; it was on YouTube (SuperHouse #41: Datalogging with MQTT, Node-RED, InfluxDB, and Grafana). That is today’s inspiration.

Here is the Flow:

Node-Red flow

The flow consists of an “MQTT in” node, a “json” sequence node, “change” function nodes and “influxdb in” nodes.

MQTT in node

This defines the MQTT publisher and the topic. Servers must be preconfigured, but can be added through this interface.

json node

edit json node

json debug nodes

debug output

change node

In the “to” field ‘msg’ needs to be selected, and the path to the data must be added.
Here is an easy way to get the path.

debug output

This is from the debug output in the right-side column. In the first image you can see two instances of the debug output, with one of them expanded. If you mouse-over an item a menu of options will appear and you can copy the path to paste in the Change Node definition.

Before completing any other values set up the connection to the Influxdb first.

influxdb node

edit influxdb out

Like the MQTT service, the server needs to be configured first, and you can see where we bind to the “enviro” database.

edit influxdb node

Once you have the first value set up copy and paste the node-pairs and edit them to create the other values that will be sent to influxdb.

The last step is to set up Grafana. There are plenty of tutorials and guides out there, so go find one! After Grafana is installed you will connect it to Influxdb and be on your way.

Caveats

The SD card setup is not ideal, as they wear out with use, and hosting a database on it can generate a lot of activity.

Share This Story!

Subscribe to receive updates when new articles are published

    * Add notice about your Privacy Policy here.