Raspberry Pi

Raspberry Pi Grafana InfluxDB Setup for MQTT Sensor Data

Install InfluxDB 2, Telegraf and Grafana on a 64-bit Raspberry Pi, subscribe Telegraf to an MQTT broker, store sensor readings as time series and chart them in Grafana.

Grafana dashboard on a monitor connected to a Raspberry Pi running a raspberry pi grafana influxdb stack fed by MQTT sensors
Run this on your own MQTT brokerGet a private, managed MQTT broker on the Free plan and start from ready-made code samples.
Try it free Code samples No card required

A raspberry pi grafana influxdb stack is one of the most practical ways to keep months of sensor history on a small, quiet computer you control. In this tutorial you will install InfluxDB 2, Telegraf and Grafana on a 64-bit Raspberry Pi, let Telegraf subscribe to an MQTT broker, and build a Grafana dashboard that updates as new readings arrive. Every command and config file is complete, so you can follow along on a fresh Pi.

Architecture: Who Talks to Whom

Before installing anything, it helps to be clear about the data path, because this is where most setups go wrong:

  1. Sensors (ESP32 boards, Pico W nodes, other Pis) publish JSON to an MQTT broker.
  2. Telegraf on the Pi connects to that broker as an ordinary MQTT client, subscribes to the sensor topics and parses the JSON.
  3. Telegraf writes the parsed fields into an InfluxDB 2 bucket on the same Pi.
  4. Grafana queries InfluxDB and draws the panels.

The broker itself does not write to a database. It only routes messages. If you want a refresher on that role, read what an MQTT broker does. Here the broker lives in the cloud, which means sensors in other buildings can publish to it, and your Pi only needs an outbound connection. No port forwarding, no dynamic DNS.

Component Role Default port on the Pi
MQTT broker (cloud) Receives and routes sensor messages None on the Pi
Telegraf MQTT subscriber and JSON parser None (outbound only)
InfluxDB 2 Time-series storage 8086
Grafana Dashboards and alerting UI 3000

Requirements

  • Raspberry Pi 4 or 5 with at least 2 GB of RAM. A Pi 3 can work for very light loads but is tight.
  • Raspberry Pi OS 64-bit (Bookworm). InfluxDB 2 packages are built for arm64, so the 32-bit image will not do.
  • An SSD over USB 3 is strongly recommended. Time-series databases write constantly and SD cards wear out.
  • A sensor already publishing over MQTT. If you do not have one, the Raspberry Pi system stats tutorial turns the Pi itself into a data source in five minutes.

Confirm the architecture first:

uname -m          # should print aarch64
cat /etc/os-release | grep VERSION_CODENAME

Install InfluxDB 2 and Telegraf

Both come from the InfluxData package repository. Add the signing key and repository, then install:

sudo apt update && sudo apt install -y curl gpg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://repos.influxdata.com/influxdata-archive.key \
  | sudo gpg --dearmor -o /etc/apt/keyrings/influxdata-archive.gpg
echo "deb [signed-by=/etc/apt/keyrings/influxdata-archive.gpg] https://repos.influxdata.com/debian stable main" \
  | sudo tee /etc/apt/sources.list.d/influxdata.list
sudo apt update
sudo apt install -y influxdb2 influxdb2-cli telegraf
sudo systemctl enable --now influxdb

Run the one-time setup. This creates the organisation, the first bucket and the admin user. Choose a retention period that fits your SD or SSD capacity; 90 days is a reasonable starting point for room sensors.

influx setup \
  --org home \
  --bucket sensors \
  --username admin \
  --password 'change-this-long-password' \
  --retention 90d \
  --force

Now create a dedicated token for Telegraf instead of reusing the admin token. Least privilege matters even on a home network:

influx auth create --org home --read-buckets --write-buckets \
  --description "telegraf writer"

Copy the token from the output. You will put it into the Telegraf config next.

Configure Telegraf to Subscribe to MQTT

Telegraf ships with a huge sample config. Replace it with a focused one so you can see exactly what it does. Use a unique client ID and a unique topic prefix; during testing the public MQTT test broker at broker.connectmq.com works without an account.

sudo mv /etc/telegraf/telegraf.conf /etc/telegraf/telegraf.conf.sample
sudo nano /etc/telegraf/telegraf.conf
[agent]
  interval = "10s"
  flush_interval = "10s"
  omit_hostname = true

[[inputs.mqtt_consumer]]
  servers = ["tcp://broker.connectmq.com:1883"]
  client_id = "telegraf-pi-4b21c9"
  topics = ["connectmq/demo/4b21c9/+/climate"]
  qos = 1
  persistent_session = false
  data_format = "json_v2"

  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "connectmq/demo/4b21c9/+/climate"
    tags = "_/_/_/room/_"

  [[inputs.mqtt_consumer.json_v2]]
    measurement_name = "climate"
    [[inputs.mqtt_consumer.json_v2.field]]
      path = "t"
      rename = "temperature"
      type = "float"
    [[inputs.mqtt_consumer.json_v2.field]]
      path = "rh"
      rename = "humidity"
      type = "float"

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "PASTE_THE_TELEGRAF_TOKEN_HERE"
  organization = "home"
  bucket = "sensors"

The topic_parsing block turns the fourth topic level into a room tag, so a message on connectmq/demo/4b21c9/kitchen/climate becomes a point tagged room=kitchen. Tags are indexed in InfluxDB, which makes per-room queries fast. For more parser options see the Telegraf MQTT consumer guide.

Start the service and follow its log. Telegraf prints connection and parsing errors there, which is far easier than guessing:

sudo systemctl enable --now telegraf
sudo journalctl -u telegraf -f

Publish a test reading from any machine:

mosquitto_pub -h broker.connectmq.com -p 1883 -i tester-4b21c9 -q 1 \
  -t connectmq/demo/4b21c9/kitchen/climate \
  -m '{"t":21.7,"rh":48.2}'

Then query InfluxDB directly to confirm the point landed:

influx query --org home \
  'from(bucket:"sensors") |> range(start:-10m) |> filter(fn:(r) => r._measurement == "climate")'

If you see rows with room=kitchen, the ingestion half is done.

Install Grafana on the Raspberry Pi

Grafana publishes its own arm64 repository:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://apt.grafana.com/gpg.key \
  | sudo gpg --dearmor -o /etc/apt/keyrings/grafana.gpg
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" \
  | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install -y grafana
sudo systemctl enable --now grafana-server

Open http://<pi-ip>:3000, log in with admin / admin and set a new password when prompted.

Connect Grafana to InfluxDB

In Grafana go to Connections, Data sources, Add data source, InfluxDB, then:

  1. Set Query language to Flux.
  2. URL: http://localhost:8086.
  3. Under InfluxDB Details, set Organization to home, paste a token (create a read-only one with influx auth create --org home --read-buckets for least privilege), and set Default Bucket to sensors.
  4. Click Save and test. You should see the bucket count reported.

Build a Raspberry Pi Grafana InfluxDB Dashboard

Create a new dashboard, add a Time series panel and paste this Flux query:

from(bucket: "sensors")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "climate" and r._field == "temperature")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "temperature")

Grafana draws one line per room automatically because room is a tag. A few dashboard choices that make a real difference on a Pi:

  • Use aggregateWindow. Without it, a 30-day range pulls every raw point and the Pi struggles. With it, Grafana asks only for as many points as the panel can display.
  • Set the dashboard refresh to 30 seconds or more. Faster refreshes mostly burn CPU on the Pi for data that changes slowly.
  • Add a Stat panel with last() instead of mean for the current temperature in each room.
  • Use Grafana alerts sparingly. They are evaluated on the Pi, so keep the evaluation interval reasonable.

If you would rather skip the database and chart live MQTT values directly, the Grafana MQTT data source guide shows that approach. It is simpler but has no history.

Moving the Broker to Private Credentials

The public test broker is readable by everyone, so production sensors should publish to a private broker. With ConnectMQ, a cloud MQTT broker, you create a broker in the portal and receive a username, password and client ID. Change three things in telegraf.conf:

[[inputs.mqtt_consumer]]
  servers = ["ssl://broker.connectmq.com:8883"]
  client_id = "YOUR_CLIENT_ID"
  username = "YOUR_USERNAME"
  password = "YOUR_PASSWORD"
  topics = ["home/+/climate"]
  qos = 1
  data_format = "json_v2"

ConnectMQ uses a Let's Encrypt certificate, so the Pi's system CA bundle validates it and no tls_ca line is needed. Remember that client IDs are bound to issued credentials: Telegraf and each sensor need their own client ID. Update topic_parsing to match your new topic shape.

A clean split of responsibilities emerges: ConnectMQ handles connectivity, authentication and routing for every device, while the Pi handles storage and visualisation for the data you care about keeping. ConnectMQ does not store message history beyond retained messages, so InfluxDB is the right place for long-term data.

Keeping the Stack Healthy

Problem Cause Fix
Telegraf log shows not authorized Wrong credentials or client ID Copy all three from the portal again
No points in InfluxDB, no errors Topic filter does not match Compare topics with a live mosquitto_sub
Points arrive but fields are missing json_v2 path wrong or wrong type Check field names and type
Grafana panels slow over long ranges No aggregation Add aggregateWindow
SD card corrupt after months Constant writes Move InfluxDB data to an SSD
Disk filling up Retention too long influx bucket update --retention 30d

Back up both InfluxDB (influx backup /path/to/backup) and /var/lib/grafana/grafana.db on a schedule. Losing a dashboard you spent an evening tuning is more annoying than losing a week of temperature data.

Get Started with ConnectMQ

Point your sensors and Telegraf at a private broker in minutes: sign up at app.connectmq.com, pick the free Serverless plan (no credit card needed) and copy the generated credentials. For the wider Pi series, start from the Raspberry Pi MQTT tutorial, and if you prefer containers, see the IOTstack on Raspberry Pi guide.

Frequently Asked Questions

Can InfluxDB subscribe to MQTT directly?

Not on its own in InfluxDB 2 OSS. Telegraf's mqtt_consumer input is the standard bridge, and it runs happily on the same Pi with a small memory footprint.

Does this work on a 32-bit Raspberry Pi OS?

InfluxDB 2 requires a 64-bit OS, so use the arm64 image. Grafana and Telegraf have 32-bit builds, but the database is the limiting piece.

How much data can a Raspberry Pi store in InfluxDB?

It depends on how many series and how often they report, plus your disk. A handful of sensors every ten seconds is a light load; use an SSD and a retention policy so growth stays bounded.

Can Grafana read MQTT without InfluxDB?

Yes, with the MQTT data source plugin it can show live values, but you lose history and aggregation. Use InfluxDB when you need trends over days or months.

Related articles

Pico W vs Pico 2 W for MQTT: RP2040 vs RP2350 Performance, Memory and TLS

The Pico 2 W keeps the Pico W's footprint and Wi-Fi chip but swaps the RP2040 for the faster RP2350. This comparison looks at what that means for MQTT devices: CPU, memory, TLS, security features, software support, and which board to buy for which project.

8 min read

Raspberry Pi RFID MQTT Access Logging with the RC522 Reader

Wire an RC522 RFID reader to a Raspberry Pi, read tags in Python, check them against a local allowlist and publish every access attempt as an MQTT event that loggers, dashboards and alerting tools can consume.

7 min read

All articles →

Your private MQTT broker is a few clicks away

Create an account, get your broker credentials and connect your first device — no servers to install, patch or scale.