Grafana Raspberry dashboard

This Raspberry Pi IoT tutorial will build an IoT system that monitors sensors using InfluxDB, MQTT and Grapana. In other words, we will build a dashboard based on Graphana that visualizes the data acquired by sensors. Moreover, InfluxDB stores the values read by sensors. All the systems exchange data using MQTT. The picture belows describes better the whole Raspberry Pi IoT project.

Monitor sensors using influxdb, grafana and mosquitto

This Raspberry IoT project uses:

  • Raspberry Pi 3
  • ESP8266 (one or more)
  • Sensors (such as BMP280, DHT11 and so on)

The Raspberry Pi acts a central serveral that runs the following components:

while the ESP8266, that manages the sensors, sends data using the MQTT protocol. These components run using docker container.

Raspberry Pi IoT Project Overview: InfluxDB, Grafana, Mosquitto and Telegraf

The picture above shows the components that will build this IoT project that are InfluxDB, Grafana and Mosquitto. How these components exchange data and how are they connected? The picture below shows how to do it:

Raspberry Pi IoT using Grafana with InfluxDB and Telegraf and Mosquitto

Let us start describing how this IoT system will work:

  • Mosquitto acts as MQTT broker accepting data coming from sensors (ESP8266 that acts as a publisher)
  • Telegraf subscribes to the MQTT topic, where sensors publish data and store this information into InfluxDB
  • Grafana reads the data in InfluxDB and manages the dashboard to visualize such information

Now, we know all the components and the role they play we can build the system . First, we start building and configuring all these components.

During this tutorial, we will assume that the docker is already installed on your Raspberry Pi.

Installig and configuring Mosquitto on Raspberry Pi using Docker

The first step is installing Mosquitto on Raspberry Pi. Just to remember, Mosquitto is the MQTT broker. To do it, we will use docker so that we can install all we need easily:

sudo docker pull eclipse-mosquitto

Wait until the download complete and then you can start the MQTT broker:

sudo docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto

That’s all. The MQTT server is up and running:

Raspberry Pi Mosquitto Docker

Installing and configuring InfluxDB

Once the Mosquitto is up and running, we can install and configure InfluxDB. As you may already know, InfluxDB is a time-series database where we can store data time-dependant.

sudo docker pull influxdb

once the installation complete, it is possible to start InfluxDB:

sudo docker run -d -p 8086:8086 
     -v influxdb:/var/lib/influxdb --name influxdb influxdb

Just a few things to notice. In this case, we start the database as deamon and we create a volume to store the data in /var/lib/influxdb:

Raspberry Pi with InfluxDB on docker container

How to create an InfluxDB database and user

The next step is creating the database and the user that will access to this database. The user will be used by Telegraf when it access to the database to store the data coming from the MQTT channel.

First, start the InfluxDB CLI:

docker exec -it influxdb influx

Next, let us create the database and the user:

create database sensors

create user "telegraf" with password "telegraf"

grant all on sensors to telegraf

With these few lines, we have created a database named sensors and a user with username telegraf and password telegraf.

Installing and configuring Telegraf

It is time to install and configure Telegraf, the component that connects to MQTT broker subscribing to the channel where sensor data is published and store this information into the InfluxDB.

sudo docker pull telegraf

Before using Telegram it is necessary to configure it. The first thing is creating a default configuration that we will modify to adapt it to our scenario:

sudo docker run --rm telegraf telegraf config > telegraf.conf

Now, it is possible to configure Telegraf. Open telegraf.conf and looks for mqtt_consumer and add/modify these lines:

servers = ["tcp://raspberry_pi_ip:1883"]
topics = [
  "sensors"
]
data_format = "influx"

Then we need to modify the output section. Look for outputs.influxdb and add/modify the following lines:

urls = ["http://raspberry_pi_ip:8086"]
database = "sensors"
skip_database_creation = true
username = "telegraf"
password = "telegraf"

Now we can run Telegraf:

 sudo docker run  -v /home/pi/:/etc/telegraf:ro telegraf

Installing and configuring Grafana

The last component we will install and configure is Grafana, the tool that creates the dashboard .

sudo docker pull grafana/grafana

When you run the grafana using Docker, there could be an error. If this is you case, you can follow this post:

https://github.com/grafana/grafana/issues/19585#issuecomment-545016209

Testing the connection between InfluxDB, Mosquitto and Telegraf

Now that we have configured all the components, it is time to test if the connections are working. To do it let us start all the components if they aren’t already running. Now, download MQTT.fx and install it. We will use MQTT.fx as a client that publishes data to the sensors channel:

  • run MQTT.fx
  • connect it to the MQTT Broker running on Raspberry Pi
  • subscribe to sensors channel

write in the message part the following message:

temp,site=room1 value=28

Using this message we are adding a measurement of the temperature called temp with a tag name site equals to room1 and the value is 28. In this way, we are emulating an ESP8266 client that sends data to our MQTT broker:

Move to Raspberry Pi and check if the message is arrived and if the data is stored in the InfluxDB sensors database:

Everything is working!!!! Let’s go the build our client using an ESP8266.

Creating the dashboard using Grafana

The last step is creating the dashboard using Grafana. The first thing is connecting to the web interface of grafana using this link:

http://<your_raspberry_ip&gt;:3000

You will get this page:

The grafana login page

Now follow these steps:

  • Login to Grafana using (admin/admin)
  • Configure the data source selecting InfluxDB
  • Create your dashboard with graphs as you prefer

An example of Grafana Dashboard using Temperature and pressure is shown below:

Grafana Raspberry dashboard

Connecting ESP8266 to MQTT

If you want to know more about connecting the ESP8266 to MQTT publishing temperature and pressure you use one of the post of this blog. You can contact me too, if you like to have the ESP8266 source code.

Conclusion

At the end of this post, you hopefully know how to build a Raspberry Pi IoT system by yourself. You can further expand this project monitoring other physical quantities (humidity, light and so on). You can even use this project to monitor other aspects and build your dashboards.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here