DEV Community

Cover image for Raspberry Pi, InfluxDB, Grafana, Docker
Anton Karazeev
Anton Karazeev

Posted on

Raspberry Pi, InfluxDB, Grafana, Docker

This is going to be my first post here. And I decided to cover steps I've taken to implement the following:

  1. Measure Raspberry Pi's CPU temperature,
  2. Log the data into InfluxDB,
  3. Monitor it using Grafana.

To set things up faster I'm going to use docker containers.

RPi's CPU temperature

For that I decided to use Python script with installed gpiozero library:

from gpiozero import CPUTemperature
cpu = CPUTemperature()
val = cpu.temperature
Enter fullscreen mode Exit fullscreen mode

Float value of e.g. 58.1 will be stored in that variable.

Logging of the data into InfluxDB

InfluxDB is an example of time series database. Previously I've never worked with one, so I decided to give it a try instead of Prometheus.

Data Explorer inside InfluxDB

What basically I need from the database is to store pushed values in it and later return them to be visualized in Grafana dashboard.

In order to push values into database we are going to use following piece of code:

import influxdb_client
from influxdb_client import Point
from influxdb_client.client.write_api import SYNCHRONOUS

bucket = os.environ.get("INFLUXDB_BUCKET")
org = os.environ.get("INFLUXDB_ORG")
token = os.environ.get("INFLUXDB_TOKEN")
url = os.environ.get("INFLUXDB_URL")
influx_client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)

write_api = influx_client.write_api(write_options=SYNCHRONOUS)
point = (
    Point("measurement")
    .tag("source", "cpu")
    .field("temperature", val)
)
write_api.write(bucket=bucket, org=org, record=point)
Enter fullscreen mode Exit fullscreen mode

token will be generated by InfluxDB after you finish setting up of account via web UI by providing username, password, org and bucket. More on that in a part about setting up of docker containers.

This information can also be found on Get Started page of InfluxDB by accessing e.g. http://localhost:8086.

Docker containers

Before we'll be able to log the values and get anything to visualize, we need to configure docker containers with relevant services. For that I'm going to use docker-compose.yml file.

Since I want to preserve all the logged data including dashboards on Grafana even when containers were removed, persistent volumes are used:

volumes:
  influxdb_data:
  grafana_data:
Enter fullscreen mode Exit fullscreen mode

And specified accordingly in services definition part:

services:
  influxdb:
    image: influxdb
    ports:
      - "8086:8086"
    volumes:
      - influxdb_data:/var/lib/influxdb2

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"
    links:
      - influxdb
    volumes:
      - grafana_data:/var/lib/grafana
Enter fullscreen mode Exit fullscreen mode

One can also notice that I specified links property as well. This is to make things easier when referring from one container to another. E.g. when I'll be specifying URL of InfluxDB while setting up data source in Grafana, I'll simply put http://influxdb:8086. Pretty neat, right?

It was a bit tricky to get CPU temperature of a host machine (Raspberry Pi) from within the running docker container. This Issue on GitHub has helped me :

services:
  monitor:
    devices:
      - /dev/gpiomem:/dev/gpiomem
Enter fullscreen mode Exit fullscreen mode

Monitoring the data using Grafana

Once everything else is set up we can head to Grafana web UI and create a new data source which is InfluxDB in our case.

Configuring Data source in Grafana

Provided screenshot will help to make it more quickly. Creating a dashboard won't be an issue too - you can write the query yourself or just copy the one provided by Script editor inside Data Explorer page in InfluxDB web UI.

Grafana dashboard

Repository

I put everything inside one repository on GitHub: https://github.com/akarazeev/temp-monitor

  1. Just download it on Raspberry Pi with git clone https://github.com/akarazeev/temp-monitor,
  2. Go inside cd temp-monitor,
  3. And start the containers with sudo docker compose -d.

The end!


Header image was generated using Stable Diffusion with parameters /dream prompt:header image for an article with title "raspberry pi, influxdb, grafana, docker"

Top comments (0)