Skip to main content

MQTT Storage

For storing data from our sensors we like to use InfluxDB - a time-series database. As a bridge between MQTT and InfluxDB, we created a mqtt2influxdb. A tool that connects to InfluxDB and MQTT broker and by user-defined config subscribes to MQTT topics and stores data from messages.

caution

You need Python and pip installed and in system PATH on your device for you to be able to get the Gateway Service

Set Up MQTT to InfluxDb

To install mqtt2influxdb you will just need to put the next command to your command line

sudo pip3 install --upgrade mqtt2influxdb

Next, you will have to create the directory where you will save the config files. To do that, just run the command:

sudo mkdir /etc/hardwario

To create the configuration file, you can use any text editor, in this tutorial we will use nano:

sudo nano /etc/hardwario/mqtt2influxdb.yml
tip

In nano, you can save changes by pressing the key combination Ctrl + O and exit editor by pressing Ctrl + X.

Configuration File Snippet

  mqtt:
host: 127.0.0.1
port: 1883

influxdb:
host: 127.0.0.1
port: 8086
database: node

points:
- measurement: temperature
topic: node/+/thermometer/+/temperature
fields:

value: $.payload

tags:
id: $.topic[1]
channel: $.topic[3]

- measurement: relative-humidity
topic: node/+/hygrometer/0:4/relative-humidity
fields:

value: $.payload

tags:
id: $.topic[1]

- measurement: illuminance
topic: node/+/lux-meter/0:0/illuminance
fields:

value: $.payload

tags:
id: $.topic[1]

- measurement: pressure
topic: node/+/barometer/0:0/pressure
fields:

value: $.payload

tags:
id: $.topic[1]

- measurement: co2
topic: node/+/co2-meter/-/concentration
fields:

value: $.payload

tags:
id: $.topic[1]

- measurement: voltage
topic: node/+/battery/+/voltage
fields:

value: $.payload

tags:
id: $.topic[1]

- measurement: button
topic: node/+/push-button/+/event-count
fields:

value: $.payload

tags:
id: $.topic[1]
channel: $.topic[3]

note

In the section tags, you can use identifiers, e.g.: tags: room: bedroom

To test if your configuration file works, you can paste the command:

mqtt2influxdb -c /etc/hardwario/mqtt2influxdb.yml --test

If everything is ok, you can also run the MQTT to InfluxDB as a service, so it runs in the background and after reboot

pm2 start `which python3` --name "mqtt2influxdb" -- `which mqtt2influxdb` -c /etc/hardwario/mqtt2influxdb.yml
pm2 save

TODO, CONFIG FILE DESCRIPTION