DEV Community

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

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

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
instance = dht11.DHT11(pin=14)

app = Flask(__name__)

@app.route("/metrics")
def metrics():
    dht11_data = ""
    result = instance.read()
    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__":
    app.run(host='0.0.0.0')

Prometheus configuration

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

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

Grafana Configuration

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

apiVersion: 1
datasources:
  - 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'

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

  prometheus:
    image: prom/prometheus:v2.16.0
    user: root
    volumes:
      - ./prometheus/:/etc/prometheus/
      - /var/prometheus:/prometheus
    command:
      - '--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'
    ports:
      - 9090:9090
    depends_on:
      - pihome
    restart: always

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

Results

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: https://github.com/pdambrauskas/pihome

Oldest comments (14)

Collapse
 
ben profile image
Ben Halpern

Very cool!

Collapse
 
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 πŸ‘

Collapse
 
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.

xt9ms2slxuqe2klmf9nc

ulmijv7ijggksiy6jskc

Collapse
 
pdambrauskas profile image
Paulius

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

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

Collapse
 
ewoks profile image
Beeblebrox

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

Collapse
 
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
Beeblebrox

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 veggiepi.com 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: gitlab.comwork.io/oss/elasticstack...

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 veggiepi.com prototype.

3) I also published, again on apache 2.0 opensource licence, an example of project that use those images with docker-compose here: gitlab.comwork.io/oss/covid19

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: community.elastic.co/events/detail...

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
Beeblebrox

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

=> youtu.be/BC1iSnoe15k

Collapse
 
idriss_neumann profile image
Idriss Neumann ∞ 🐧 🐳 🦊

Hi.

Finally I managed to make a ready to use image for the DHT22 (temperature and humidity sensor) here: gitlab.comwork.io/oss/veggiepi/hum...

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 README.md file that will explain everything about how to run all the stack on your raspberrypi.

Collapse
 
ewoks profile image
Beeblebrox

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

Collapse
 
pdambrauskas profile image
Paulius

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