DEV Community

Cover image for Monitoring Apartment temperature & humidity with Raspberry Pi, Prometheus & Grafana

Posted on

Monitoring Apartment temperature & humidity with Raspberry Pi, Prometheus & Grafana

For quite some time, I had a spare Raspberry Pi lying around in my place. And one weekend I came up with idea to make my apartment "smarter". What I mean by saying "smarter" is tracking some metrics of my surroundings.

I have some experience in working with Prometheus and Grafana, so I decided to incorporate those tools into my solution. Yes, it does sound like overengineering simple task, you can probably get same results in much simpler way : ). But it was fun weekend project for me.

In this post I'll describe my setup for monitoring room temperature & humidity.

Hardware components

These are all the component, I used in my project:

Connecting Sensor to Raspberry Pi

I connected Ground pin to the Ground of Raspberry PI, Data Pin to GPIO 14 pin, Vcc pin to 3.3V power supply pin.

Reading sensor data

For reading sensor data and feeding it to Prometheus, I chose DHT11_Python library, which is quite unstable, and sometimes does not return valid results, so you might get some gaps in your graphs.

Also I've created simple Flask API to serve metrics for Prometheus:

from flask import Flask
import dht11
import RPi.GPIO as GPIO

instance = dht11.DHT11(pin=14)

app = Flask(__name__)

def metrics():
    dht11_data = ""
    result =
    if result.is_valid():
        dht11_data = f"""pihome_temperature {result.temperature}
pihome_humidity {result.humidity}"""

    return f"{dht11_data}", 200, {'Content-Type': 'text/plain; charset=utf-8'}

if __name__ == "__main__":'')

Prometheus configuration

To scrape metrics from my Flask API, I've added configuration to prometheus.yml:

    scrape_interval: 30s
    - job_name: 'pihome'
        - targets: [pihome:5000]

Grafana Configuration

Then, in /etc/grafana/provisioning, I've added datasource configuration:

apiVersion: 1
  - name: Prometheus
    type: prometheus
    url: http://prometheus:9090/
    access: proxy
    isDefault: true

It is also possible to add Grafana dashboards to provisioning folder as json files, so that you don't need to create new dashboard each time you re-deploy Grafana.

Connecting everything together

To make everything portable and easy to install, I packed my Flask API to Docker image and configured all services in docker-compose.yaml:

version: '3'

    image: pihome
    build: .
    restart: always
      - "/dev/mem:/dev/mem"
    privileged: true
      - 5000:5000

    image: prom/prometheus:v2.16.0
    user: root
      - ./prometheus/:/etc/prometheus/
      - /var/prometheus:/prometheus
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=30d'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      - 9090:9090
      - pihome
    restart: always

    image: grafana/grafana:6.6.2
      - prometheus
      - 80:3000
      - ./grafana/:/etc/grafana
    restart: always


I left my stack running for some time, to collect some historical data, and dashboard looked like this:

Git project

You can find my full configuration and code on Github:

Top comments (14)

idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊 • Edited

Great article :) We did use the same sensor couple weeks ago (and bunch of other sensors) but with AdaFruit and ElasticSearch and Kibana instead that are directly hosted on the raspberry and run on docker arm based containers. It worked like a charm.



ewoks profile image

Cool... Would you consider writing more about those containers running on RPi?

idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊

Yeah I think to write a complete article about that (docker+ElasticStack+elastalertΒ±gitlab-runner and one sensor on a raspberry pi 4).

Maybe I'll choose the same sensor that work directly with the raspberry gpio inputs whereas we had to use an arduino for the others (which send the data to the rpi after reading the analog inputs) as you can see it on my previous pictures.

Thread Thread
ewoks profile image

Hi Idriss,

did you manage to publish it already? Let me know.. :)

Thread Thread
idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊

Hi Vladi Beeblebrox.

Yes and no ^^

1) I did not published an article on how I handle to convert analogical sensor data using arduino or esp32 and read numerical data using AdaFruit library with Pyhton in order to push those data on ElasticSearch. The project has been paused since the COVID19.

2) I did published, on apache 2.0 opensource licence, ARM-based docker images for Elasticsearch, Kibana and Elastalert on dockerhub in order to provide freely, images that can be easily used by everyone on their raspberrypi projects.

The Dockerfiles are here:

You'll find the dockerhub links and docker pull commands on the README.

Those images are built using raspberrypi as gitlab runners and are successfully used by the prototype.

3) I also published, again on apache 2.0 opensource licence, an example of project that use those images with docker-compose here:

This project aims to provide a docker container that handle to get the covid19 worldwide opendata (on multiple datasources) and push them on Elasticsearch in order to be able to make some graphs, timeseries visualizations and dashboards on Kibana (or Grafana instead if you like).

The "covid-stat" image is built for x86 servers and raspberrypi and you'll find docker-compose files for both architectures (the ARM one is using the sames Elasticsearch and Kibana that I was talking about before).

I also use gitlab-runner both on a x86 dedicated server and a raspberrypi in order to build the "covid-stat" image and deliver to dockerhub.

4) I will give a talk on this project in the next Elastic FR Meetup:

This talk will probably be recorded and published on youtube and I'll push the slides on the repo after this session.

Unfortunately it will be in French because this meetup is french-speakers oriented but It's possible that I will give others online talks somewhere else about that. And I'll translate the slides before pushing it on the repository.

Thread Thread
ewoks profile image

wow, great stuff here... My french got pretty rusty but I will for sure take a look at docker images and the project. Thanks for sharing and contributing to the open source man πŸ‘πŸ‘πŸ‘

Thread Thread
idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊

Anyway I give the youtube link just in case for french speakers ^^


idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊


Finally I managed to make a ready to use image for the DHT22 (temperature and humidity sensor) here:

You can directly use the docker-compose here (the images are available on docker hub and distributed in OpenSource). In this repo, there is a file that will explain everything about how to run all the stack on your raspberrypi.

pdambrauskas profile image

Nice setup! I'm thinking about connecting some kind of noice sensor to measure sound levels, maybe you have some recommendations on that?

idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊 • Edited

No sorry. For now I only focus on sensors that are used for monitoring hydroponic and aquaponic systems (electrical conductivity, ph, temperature and humidity, water temperature, etc) wich doesn't require to monitor the noise. But it can be interesting for other projects so let's keep in touch :)

daviddalbusco profile image
David Dal Busco

A nice read for my Sunday's morning breakfast πŸ˜‰

Thanks for the share and congratulations for the cool pet project πŸ‘

ewoks profile image

Looks nice.. maybe you could add CO2 sensor too?

pdambrauskas profile image

Yes, CO2 senor addition would be nice, also I'm thinking about some kind of movement sensor.

ben profile image
Ben Halpern

Very cool!