DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Building a Reverse Image Search System Based on Milvus and VGG
  • Monitoring Apache Ignite Cluster With Grafana (Part 1)
  • Connecting the Dots: Unraveling IoT Standards and Protocols
  • Harnessing the Power of MQTT for the Future of IoT

Trending

  • Enforcing Architecture With ArchUnit in Java
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  1. DZone
  2. Data Engineering
  3. IoT
  4. Raspberry Pi IoT: Sensors, InfluxDB, MQTT, and Grafana

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

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

By 
Francesco Azzola user avatar
Francesco Azzola
·
Nov. 21, 19 · Tutorial
Likes (23)
Comment
Save
Tweet
Share
91.9K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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:

  • InfluxDB (a time-series database)
  • Mosquitto (the MQTT broker)
  • Grafana (a platform used to create dashboards)

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:

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

http://<your_raspberry_ip>:3000


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. 

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.

Further Reading

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

MQTT raspberry pi InfluxDB Grafana IoT Database Data (computing) Docker (software) Dashboard (Mac OS) Build (game engine)

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Reverse Image Search System Based on Milvus and VGG
  • Monitoring Apache Ignite Cluster With Grafana (Part 1)
  • Connecting the Dots: Unraveling IoT Standards and Protocols
  • Harnessing the Power of MQTT for the Future of IoT

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!