This Raspberry Pi IoT tutorial will build an IoT system that monitors sensors using InfluxDB, MQTT, and Grafana. In more detail, we will build a system that reads data from sensors. They send data to Moqsuitto, the MQTT broker.
Next, InfluxDB reads from Mosquitto and stores these readings. Then Grafana connects to InfluxDB and produces charts that visualize the data acquired by sensors. All the systems exchange data using MQTT. The picture below describes better 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:
- InfluxDB (a time-series database)
- Mosquitto (the MQTT broker)
- Grafana ( a platform used to create dashboards)
while the ESP8266, that manages the sensors, sends data using the MQTT protocol. InfluxDB, Mosquitto, and Grafana run using docker containers.
Integrating InfluxDB, Grafana, Mosquitto, and Telegraf using MQTT and Docker
The picture above shows the components that will build this IoT Raspberry project: InfluxDB, Grafana, and Mosquitto. How 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 manages sensors and acts as a publisher)
- Telegraf subscribes to the MQTT topic, where sensors publish data. Telegraf stores this information into InfluxDB. In other words, InfluxDB uses MQTT to acquire data
- 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.
Installing 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
Code language: Bash (bash)
Then, wait until the download complete and when complete, you can start the MQTT broker:
sudo docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
Code language: Bash (bash)
That’s all. The MQTT server is up and running:

Installing and configuring InfluxDB
Now to build this Raspberry Pi IoT project, 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
Code language: Bash (bash)
once the installation completes, it is possible to start InfluxDB:
sudo docker run -d -p 8086:8086
-v influxdb:/var/lib/influxdb --name influxdb influxdb
Code language: Bash (bash)
Just a few things to notice. In this case, we start the database as a daemon. 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
Code language: Bash (bash)
Next, let us create the database and the user:
create database sensors
create user "telegraf" with password "telegraf"
grant all on sensors to telegraf
Code language: Bash (bash)
With these few lines, we have created a database named sensors
and a user with username telegraf
and password telegraf
.
Installing and configuring Telegraf
To make the InfluxDB acquire data using MQTT we will use Telegraf. Telegraf is the component that connects to the MQTT broker subscribing to the channel where sensor data is published and stores this information into the InfluxDB. In this Raspberry IoT project, Telegraf acts as a bridge:
sudo docker pull telegraf
Code language: Bash (bash)
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
Code language: Bash (bash)
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"
Code language: JavaScript (javascript)
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"
Code language: JavaScript (javascript)
Now we can run Telegraf:
sudo docker run -v /home/pi/:/etc/telegraf:ro telegraf
Code language: Bash (bash)
Note: This tutorial uses simple password, if you want to use it in your production environment be sure to use safer password
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:
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 arrives 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>:3000
Code language: HTML, XML (xml)
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. This is the source code:
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "your ssid";
const char* password = "wifi_password";
const char* mqtt_server = "mqtt_server";
const char* channel = "sensors";
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi connected");
client.setServer(mqtt_server, 1883);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
float temp = bmp.readTemperature();
float press = bmp.readPressure();
String v1 = ("temp,site=room1 value=" + String(temp));
client.publish(channel, v1.c_str(), true);
v1 = ("press,site=room1 value=" + String(press));
client.publish(channel, v1.c_str(), true);
delay(60000);
}
Code language: C++ (cpp)
To know more about the code, read how to connect ESP8266 to Raspberry using MQTT.
More interesting articles:
How to Build a Surveillance System with Raspberry Pi 3 and camera
How to Deploy OpenCV on Raspberry Pi enabling machine vision
Connect Raspberry Pi to Google Cloud IoT (GCP IoT) using NodeJS
How to use Machine Learning with Raspberry PI with Tensorflow Lite
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 by monitoring other physical quantities (humidity, light and so on). You can even use this project to monitor other aspects and build your dashboards. Moreover, during this project, you learned how to use MQTT to connect Grafana and InfluxDB through Telegraf.
APPROVED!
Wonderful Article.. Nicely written
I tried these steps and got it working at the first attempt
Thank you very much!
This is an excellent article. The Internet of Things (IoT) is the next generation of internet technology. The Internet of Things (IoT) will profoundly alter how businesses and manufacturing are conducted around the world. It continues to expand throughout the house and the workplace, transforming how we live and work on a daily basis.