Raspberry Pi IoT: Sensors, InfluxDB, MQTT, and Grafana


Person holding a Raspberry Pi

Learn how to build a dashboard based on Grafana that visualizes data acquired by sensors.

This Raspberry Pi IoT tutorial will build an IoT system that monitors sensors using InfluxDB, MQTT, and Grafana. In other words, we will build a dashboard based on Grafana that visualizes the data acquired by sensors.

You may also like: Playing With Docker, MQTT, Grafana, InfluxDB, Python, and Arduino

With this, InfluxDB stores the values read by sensors. All the systems exchange data using MQTT. The picture below better describes the whole Raspberry Pi IoT project.

This Raspberry IoT project uses:

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

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

While the ESP8266 manages the sensors that send data using the MQTT protocol. These components run using Docker containers.

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. So, how do these components exchange data, and how are they connected? The picture below shows how to do it:

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 Docker is already installed on your Raspberry Pi.

Installing and Configuring Mosquitto on Raspberry Pi Using Docker

The first step is installing Mosquitto on Raspberry Pi. Just 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:

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 is 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:

How to Create an InfluxDB Database and User

The next step is creating the database and the user that will access this database. The user will be used by Telegraf when it accesses 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 the 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 your case, you can follow this post:


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 has 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:


You will get this 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:

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 posts of this blog. 


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 by monitoring other physical quantities (humidity, light, and so on). You can even use this project to monitor other aspects and build your dashboards.

Further Reading

Monitoring the Weather With InfluxDB and Grafana (and a Bunch of Arduinos)

Playing With Docker, MQTT, Grafana, InfluxDB, Python, and Arduino

This UrIoTNews article is syndicated fromDzone