Temperature Sensor Dashboard

Build a temperature monitoring dashboard with a Raspberry Pi and DHT22 sensor. Visualize data with Grafana and InfluxDB.

Andreas · April 12, 2026 · 8 min read

Introduction

A DHT22 sensor gives you precise temperature and humidity readings. Combined with InfluxDB and Grafana, you can build a long-term monitoring system that stores historical data and displays it on a professional dashboard. This setup scales from a single room to a multi-room homelab.

Prerequisites

  • Raspberry Pi 4/5 with at least 2GB RAM
  • DHT22 temperature/humidity sensor
  • 10kΩ pull-up resistor
  • Jumper wires and breadboard
  • Docker and Docker Compose installed
  • Python 3.9+
  • pip install adafruit-circuitpython-dht

Wiring DHT22 to GPIO4

Connect your DHT22 sensor to GPIO 4 (pin 7):

DHT22 Pin 1 (VCC) ──[+3.3V]
DHT22 Pin 2 (Data) ──[10kΩ pull-up]──[+3.3V]
                     │
                  GPIO 4 (pin 7)

DHT22 Pin 4 (GND) ──[GND]
(Pin 3 is unused)

The 10kΩ resistor pulls the data line high and stabilizes communication.

Step 1 — Install DHT22 Library and Test Sensor

pip3 install adafruit-circuitpython-dht
sudo apt install python3-libgpiod-pep8 -y

Test the sensor with test_dht.py:

import adafruit_dht
import board
import time

# DHT22 on GPIO 4
sensor = adafruit_dht.DHT22(board.D4)

try:
    while True:
        temp = sensor.temperature
        humidity = sensor.humidity
        print(f"Temp: {temp:.1f}°C, Humidity: {humidity:.1f}%")
        time.sleep(2)
except KeyboardInterrupt:
    sensor.deinit()

Run it:

python3 test_dht.py

You should see temperature and humidity readings every 2 seconds.

Step 2 — Set Up InfluxDB and Grafana with Docker Compose

Create docker-compose.yml:

version: "3.8"
services:
  influxdb:
    image: influxdb:2.7-alpine
    container_name: influxdb
    ports:
      - "8086:8086"
    environment:
      INFLUXDB_DB: sensors
      INFLUXDB_ADMIN_USER: admin
      INFLUXDB_ADMIN_PASSWORD: homelab123
      INFLUXDB_HTTP_AUTH_ENABLED: "false"
    volumes:
      - influxdb_data:/var/lib/influxdb2
    restart: unless-stopped

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      GF_SECURITY_ADMIN_PASSWORD: admin
      GF_INSTALL_PLUGINS: grafana-piechart-panel
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      - influxdb
    restart: unless-stopped

volumes:
  influxdb_data:
  grafana_data:

Start the services:

docker-compose up -d

Check they're running:

docker-compose ps

InfluxDB is at http://localhost:8086, Grafana at http://localhost:3000.

Step 3 — Python Script to Read Sensor and Write to InfluxDB

Create sensor_writer.py:

import adafruit_dht
import board
import time
from influxdb import InfluxDBClient

# InfluxDB connection
client = InfluxDBClient(host='localhost', port=8086, database='sensors')

# DHT22 sensor
sensor = adafruit_dht.DHT22(board.D4)

def write_sensor_data():
    try:
        temp = sensor.temperature
        humidity = sensor.humidity
        
        json_body = [
            {
                "measurement": "environment",
                "tags": {
                    "location": "living_room",
                    "sensor": "dht22"
                },
                "fields": {
                    "temperature": float(temp),
                    "humidity": float(humidity)
                }
            }
        ]
        
        client.write_points(json_body)
        print(f"Wrote: {temp:.1f}°C, {humidity:.1f}%")
    except RuntimeError:
        print("DHT22 read failed, retrying...")

try:
    while True:
        write_sensor_data()
        time.sleep(60)  # Write every 60 seconds
except KeyboardInterrupt:
    sensor.deinit()
    client.close()

Install the InfluxDB Python client:

pip3 install influxdb

Run the script:

python3 sensor_writer.py

Step 4 — Create Grafana Dashboard

Open http://localhost:3000 in your browser (login: admin/admin).

  1. Add InfluxDB as data source: Settings → Data Sources → Add InfluxDB
  2. Set URL to http://influxdb:8086 and database to sensors
  3. Create a new dashboard with panels:
    • Temperature line graph (query: SELECT temperature FROM environment)
    • Humidity line graph (query: SELECT humidity FROM environment)
    • Gauges showing current values

Step 5 — Systemd Timer for Continuous Readings

Create /etc/systemd/system/dht-logger.service:

[Unit]
Description=DHT22 Sensor Logger
After=network.target docker.service

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/dht
ExecStart=/usr/bin/python3 /home/pi/dht/sensor_writer.py
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable and start:

sudo systemctl daemon-reload
sudo systemctl enable dht-logger
sudo systemctl start dht-logger

Check status:

sudo systemctl status dht-logger

Troubleshooting

DHT22 read errors — The sensor is finicky with timing. If you see repeated failures, add a try-catch and retry loop. Bad wiring or missing pull-up resistor will cause consistent failures.

InfluxDB connection refused — Ensure Docker containers are running. Check with docker logs influxdb for errors. Port 8086 must be accessible from the Pi.

Grafana shows no data — Verify the InfluxDB connection in Grafana settings. Check that data is actually being written: query the InfluxDB API directly with curl http://localhost:8086/query?db=sensors&q=SELECT%20*%20FROM%20environment.

Missing timestamps — InfluxDB automatically timestamps writes. If you query and see nulls, the measurement name in your Python script must match exactly what you query in Grafana.

Summary

This setup creates a scalable temperature monitoring infrastructure. The DHT22 gives accurate readings; InfluxDB stores time-series data efficiently; Grafana visualizes it beautifully. From here, add more sensors (DHT22s in other rooms), alert rules in Grafana, or export data to remote storage for long-term trending analysis.

Comments