Setting up a local weather station using DHT11, Docker, Prometheus, and Grafana on a Raspberry Pi and visualizing your data through your web browser

Guilherme Müller
Level Up Coding
Published in
6 min readSep 9, 2020

--

This article will guide you through creating a simple weekend project to read data from a DHT11 sensor and broadcast it to a Grafana instance, so you can visualize it and access it through your network from other devices.

Summary

For this project, we’ll need the following components:

Hardware components

  • Raspberry Pi 3B with Ubuntu Server 20.04 LTS
  • DHT11 module (3-pin version)

Software components

For this project, we’ll need Python 3, Flask webserver, Docker, Portainer, Prometheus, and Grafana.

Structure

Our project will be structured using a Raspberry Pi 3B running Ubuntu Server 20.04 as our host. We will plug the DHT11 to our GPIO interface and read from it using Python 3. While reading, we’ll broadcast our readings with a Flask web server to the port 5000. Using Docker, we’ll run Prometheus at port 5001 and Grafana at port 5002. Our data will flow through the pipeline, being collected, broadcasted, stored, and displayed on a modern and cool graphical interface.

The project structure.

First steps

If you want to start afresh, flash your SD card with Ubuntu, and install the system. Now you should have a clean install with Python 3 in your Raspberry Pi. Do a ‘sudo apt-get update’ and a ‘sudo apt-get upgrade’ to be up to date with the latest software. Since our application will be accessible from our network, it should have a static IP address. Set it up using the following guide.

Installing DHT11 on Raspberry Pi

First of all, let’s install the DHT 11 on our Raspberry Pi. If you’re using the module version, this is pretty simple. Hook up the 5V and GND to the GPIO of the Raspberry and choose an analog port for your data pin. The following schematic by Circuit Basics might help. If you’re using the 4-pin version, check this schematic.

Wiring schematic by Circuit Basics.

Reading the DHT11 sensor

As most tutorials state, you can use the Adafruit Python DHT library to easily connect and read from the sensor. You can follow the installation steps from the documentation above to install it in your python environment.

Setting up a simple Flask webserver

Since we want to read from our GPIO and be able to serve it to Grafana, we can use Prometheus. To send data to Prometheus, we can create a simple webserver using Flask. In the snippet below, we’ll read the data from our sensor (with PIN 2 from GPIO) and format it to Prometheus syntax whenever the endpoint “/metrics” is called (which it’s the default endpoint for Prometheus).

To run our webserver, run the following commands from your terminal:

$ export FLASK_APP=app.py
$ flask run

More info on starting up a Flask app you can find here.

Optional

If you want your service to run as a daemon task (a background task), you can create a shell script and add it to your services. Your script should be saved on a folder and set as executable by using the chmod command. After that add your service to the systemctl. For a step by step version, view this post.

After all that, your service should be running as a daemon service, so if you reboot your system, it will start again automatically.

It’s Docker time

Since we want to use multiple services as Grafana and Prometheus, the easiest way is to use them through Docker. To install Docker, follow the instructions from the Docker documentation. Remember to use the ARM64 variant when needed. It’s a pretty straight forward process. At the end of the process, you should have a Docker installation ready to go.

Setting up Portainer

Managing containers through the command line is fine, but sometimes, using a web interface (especially on your local network) it’s way more awesome. Here comes Portainer to the rescue. Portainer is a simplified, but powerful, docker management layer, with a Web GUI. To install it, follow the instructions on its installation page.

A screenshot of Portainer’s container management screen.

Setting up Prometheus

Since we’re using Docker and Portainer, spinning up Prometheus it’s easy. Prometheus allows us to scrap metric information from our webserver in a time-series fashion. Simply create a new container in the “Add Container” in Portainer and let’s create a new instance of Prometheus.

Creating a new container on Portainer

Name your container and use Prometheus:latest as your image. You can change the default port for Prometheus (which is 9090) to something closer to our webserver port, like 5001 on our host (leave the container port as 9090). For more information about Prometheus on docker, you can check the docs.

The configuration file

Prometheus uses a file called “prometheus.yml” to set its configurations and define the scrapped services. You can use this base configuration file and change the “your.raspberry.ip.address” with your Raspberry Pi IP Address and the webserver port, eg. 192.168.0.103:5000. For more on configuration, check Prometheus’ documentation.

Now save it on the host machine and bind it to our container, so it can be consumed by Prometheus.

Add a new bind to /etc/prometheus/prometheus.yml

Now, click on “Deploy the container” and we should be ready to go.

Give Portainer a few minutes to spin the container up and it should appear as running on your Container panel.

Setting up Grafana

Now it’s time for Grafana. Let’s use the same strategy used for Prometheus. Create a new container on Portainer, using grafana/grafana:latest and map it to a port like 5002 on your host. Grafana doesn’t require a configuration file, so it’s pretty simple to start it up. Deploy the container and wait a few minutes. The container should appear as “Running” on your Portainer.

Creating a Grafana container.

Visit http://your.raspberry.ip.address:5002 (change your.raspberry.ip.address with your Raspberry Pi IP Address) to access Grafana. You should be greeted with a login screen. Use “admin” for the username and password, and you will be in.

Setting up the Data Source

Now it’s time to set up our Prometheus data source. After entering in Grafana, on the sidebar, you will find a gear icon, inside it, you will find the Data Sources item. Click on it, and then on Add data source.

Grafana’s screen.

Choose Prometheus and let’s configure it. Choose a name, enter the URL of your Prometheus instance (your Raspberry Pi IP address) and the port of the service. Click on Save and Test and if everything is working fine, you’ll see a green notification indicating success.

Data source configuration.

Creating the dashboard

After we’ve set the data source, we can create a new dashboard, add a new panel, and add the measurements to it. Our first panel will be responsible for showing the current temperature using the field “local_temp”, as defined in our Flask webserver snippet.

Creating a new panel inside the dashboard.

Our second panel will be responsible for showing the humidity level, by reading the field “local_humidity” set in our Flask snippet. After both panels are finished, you’ll be able to see the values changing over time.

Your simple dashboard with temperature and humidity.

The first steps on IoT and Data Visualization

This is a simple weekend project that may help you familiarize yourself with reading data from sensors and broadcasting it to data visualization tools over the internet. Much more can be improved over this project in terms of security, functionality, and reliability. I hope it gets you motivated to start tinkering with it and give me a shout in case of any doubts.

--

--

A Brazilian software developer and devops engineer, who also likes communication and research.