I’ve started using InfluxDB for storing my sensor data as time series. The main reason for this is that it allows me to use Grafana for analyzing the data. This blog post is an introduction to my setup with these tools on a Raspberry Pi.
All sensor data in my IoT-home setup are transformed to MQTT messages that are published to a self-hosted MQTT broker (mosquitto). I have different services that subscribes to the MQTT topics:
- HomeAssistant. Stores changes of measurements as events. Displays the data and creates automation based on changes in the data.
- A subscriber for MongoDB. Stores all data in a MongoDB database. Used for a home made graph app that probably will be redundant with InfluxDB and Grafana in place.
- A proxy for Adafruit IO. Transforms a subset of the messages to Adafruit IO feeds and publishes them to an external cloud service.
To get data for a data source in Grafana, I simply add an additional MQTT subscriber that stores all messages in time series in an InfluxDB database. My setup will look like this:
An alternative solution for InfluxDB and Grafana is to let HomeAssistant export all state changes to InfluxDB:
I prefer not to use this approach as I don’t want HomeAssistant to be the central hub in my system. I really like HomeAssistant, but I want it to be a subscriber node that no other node has to depend on.
InfluxDB is a time series database. It is optimized for queries in the time domain, e.g. for graphs with data points measured at different points in time. The database contains named “measurements”, e.g. “temperature_from_sensor_1”, and each measurement contains a set of data points that represents samples of the sensor data. A datapoint has a measurement name, a time stamp and a value. An additional set of values and arbitrary key-value tags can be attached to a datapoint (I don’t use this feature in my setup though).
Creating and using a database with measurement, value and time can look like this:
|$ influx -precision rfc3339 // starts the InfluxDB shell and shows time in rfc3339 format|
|Connected to http://localhost:8086 version 1.2.0|
|InfluxDB shell version: 1.2.0|
|> create database demo // Creates a new database|
|> use demo|
|Using database demo|
|> insert mysensor1 value=0.51 // Insert new datapoints|
|> insert mysensor1 value=0.53 // for the measurement|
|> insert mysensor1 value=0.45 // "mysensor1"|
|> select * from mysensor1 // Get the whole time series|
If a time stamp is not provided in the insert statement, InfluxDB will create one from the current time. The time stamps are UTC-based so you have to convert to local time when viewing your data. And if inserting time stamps manually, time should be in UTC.
InfluxDB has a HTTP API that can be used for queries. This is what Grafana uses.
Grafana is a web-based data visualizing tool that can connect to InfluxDB and several other data sources. With Grafana, you can setup custom dashboards, alerts and notifications and you can zoom in- and out of the data sets. Grafana has a web front end that is very responsive and cool looking. It’s open-source and easy to use and self-host. I’m really impressed with this application.
Installation & setup
For an installation of InfluxDB on a Raspberry Pi with Raspbian (stretch version), you need to add an additional apt-source:
For a simplistic setup, open /etc/influxdb/influxdb.conf and enable the http endpoint, the bind-address and set auth-enabled to false:
|# Determines whether HTTP endpoint is enabled.|
|enabled = true|
|# The bind address used by the HTTP service.|
|bind-address = ":8086"|
|# Determines whether user authentication is enabled over HTTP/HTTPS.|
|auth-enabled = false|
Then restart the influxd service with: sudo service influxd restart
Test that your installation and configuration work by using the influx command and try out the InfluxDB example described above.
Grafana also needs an additional apt-source for the installation on Raspberry Pi with Raspbian. See this wiki for instructions:
Then install Grafana with:
sudo apt-get update sudo apt-get install grafana
The Grafana configuration file is /etc/grafana/grafana.ini. You need to enable http as protocol and set a port to use in this file.
|# Protocol (http, https, socket)|
|protocol = http|
|# The ip address to bind to, empty will bind to all interfaces|
|# The http port to use|
|http_port = 3000|
After modifying grafana.ini, use sudo service grafana-server restart to apply your settings to the service.
You should now be able to login to Grafana on http://<IP ADDRESS OF RPI>:3000 with the default user admin/admin.
For more configuration options, see http://docs.grafana.org/installation/configuration/
Integration with my existing system
As mentioned in the introduction, all sensor data from my IoT-nodes are transformed to MQTT messages that are published to a locally hosted mosquitto broker.
Integrating InfluxDB is as simple as adding an additional MQTT subscriber that takes a received sensor message and store it as a value in an InfluxDB database. I use a Python 3 script (as a service) with paho-mqtt for subscribing to MQTT topics and the influxdb-python library for writing the values to the InfluxDB database. See these links on how to install the required Python libraries:
For Python 3.* you can use pip3 for installation:
pip3 install influxdb pip3 paho-mqtt
I use the MQTT topic as measurement type for the data. This lets me get for example “Home/Outdoor/Temperature” as a time series from InfluxDB. In this implementation I set the time property of the data that is uploaded (have to use UTC-time!). If the time property was excluded, InfluxDB would have created a time stamp from the current time.
|import paho.mqtt.client as mqtt|
|from influxdb import InfluxDBClient|
|def on_connect(client, userdata, flags, rc):|
|print("Connected with result code "+str(rc))|
|def on_message(client, userdata, msg):|
|print("Received a message on topic: " + msg.topic)|
|# Use utc as timestamp|
|# Convert the string to a float so that it is stored as a number and not a string in the database|
|val = float(message)|
|print("Could not convert " + message + " to a float value")|
|print(str(receiveTime) + ": " + msg.topic + " " + str(val))|
|json_body = [|
|print("Finished writing to InfluxDB")|
|# Set up a client for InfluxDB|
|dbclient = InfluxDBClient('192.168.1.16', 8086, 'root', 'root', 'sensordata')|
|# Initialize the MQTT client that should connect to the Mosquitto broker|
|client = mqtt.Client()|
|client.on_connect = on_connect|
|client.on_message = on_message|
|while(connOK == False):|
|client.connect("192.168.1.16", 1883, 60)|
|connOK = True|
|connOK = False|
|# Blocking loop to the Mosquitto broker|
With Grafana installed, you can log in as an admin and add an InfluxDB database as a new data source where you specify the http settings for InfluxDB’s http api:
Setting the access to “proxy” (this option is called “Server” in later versions of Grafana) will make Grafana access the database from the server side. You have to use this when using Grafana outside your LAN (with port forwarding) while InfluxDB is only exposed within the LAN network. The default access is “direct” (“Browser” in later versions of Grafana), which means that the browser front end will fetch the data over http (that’s why it will not work outside your LAN unless both Grafana and InfluxDB has been port-forwarded).
With the data source defined, you can create a new dashboard that can be filled with rows and panels. On each panel you select one or several metrics from your datasource.
You can use the drop downs to build up the query or switch to raw mode where you write the query in plain text. Grafana is really easy to use and as the result of a change is shown immediately one can just play around with the settings to learn how it works.
For showing a momentary value, you can use the Singlestat panel:
The Singlestat panel can be configured to show the last value for a time series (in the selected range). On the options tab you can select color and unit for the panel.
My current main dashboard contains an overview of climate sensors and detected events (front door-opened events):
I have also added system monitoring measurements from my three Raspberry Pi:s (temperature, CPU usage, memory usage, disk usage):
Adapting my system to store the data as time series was easy as all data was available as MQTT topics. With the data in place in InfluxDB, connecting it to Grafana was a breeze. I’m really impressed with the ease-of-use, flexibility and performance of Grafana. I think the Grafana dashboards will be my new favourite tool for analyzing my collected data.
Both InfluxDB and Grafana works great on a Raspberry Pi without using much resources.