- Open terminal
- Make sure
git
is installed:The output should look something like:git --version
git version 2.25.1
- Make sure Python 3.9+ is installed:
The output should look something like:
python --version
Python 3.9.7
- If it shows
2.7.x
instead, trypython3
instead and use it in the rest of the guide. - If it shows
3.8.x
or lower, usepyenv
to install a higher version of Python on your system.
- If it shows
- Make sure
poetry
is installed:The output should look something like:poetry --version
Poetry version 1.1.5
- Clone this repository:
git clone https://github.com/radeklat/mqtt-influxdb-gateway.git cd mqtt-influxdb-gateway
- Copy the
.env.template
file into.env
file:cp .env.template .env
- Edit the
.env
file, fill all missing values and/or change existing ones to suit your needs. - Install all application dependencies:
poetry install --no-dev
- Run the app:
poetry run python src/main.py
- Open terminal
- Make sure
docker
is installed:The output should look something like:docker --version
Docker version 20.10.5, build 55c4c88
- Create a base folder structure for the service:
cd mqtt-influxdb-gateway # Download .env file template curl https://raw.githubusercontent.com/radeklat/mqtt-influxdb-gateway/main/.env.template --output .env
- Edit the
.env
file and fill the missing details. - Run the container:
This configuration will run the service in the foreground (you need to keep the terminal open) and always use the latest version.
docker run \ --pull always --rm --name mqtt-influxdb-gateway \ --env-file=.env \ radeklat/mqtt-influxdb-gateway:latest
- Change
latest
to a specific version if you don't always want the latest version or remove the--pull always
flag to not update.
- Change
Add --detach
flag to run in the background. You can close the terminal, but it will not start on system start up.
- To see the log of the service running in the background, run
docker logs mqtt-influxdb-gateway
- To stop the service, run
docker container stop mqtt-influxdb-gateway
You can use the above-mentioned docker image to run the sync as a service on server or even your local machine. The simples way is to use docker compose:
- Make sure you have
docker
anddocker-compose
installed:The output should look something like:docker --version docker-compose --version
Docker version 20.10.5, build 55c4c88 docker-compose version 1.28.5, build unknown docker-py version: 4.4.4 CPython version: 3.8.5 OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
- Create a base folder structure for the service:
mkdir mqtt-influxdb-gateway cd mqtt-influxdb-gateway
- Download a compose file and an example .env file:
BASE_URL=https://raw.githubusercontent.com/radeklat/mqtt-influxdb-gateway/main curl $BASE_URL/.env.template --output .env curl $BASE_URL/docker-compose.yml -O
- Edit the
.env
file and fill the missing details. - Run the service:
This command will run the service in the foreground (you need to keep the terminal open) and always use the latest version.
docker-compose up
- Change
latest
to a specific version if you don't always want the latest version.
- Change
Add --detach
flag to run in the background. You can close the terminal. The service should start on system start up.
- To see the log of the service running in the background, run
docker-compose logs mqtt-influxdb-gateway
- To stop the service, run
docker-compose stop mqtt-influxdb-gateway
-
Open terminal
-
Navigate to the project repository:
cd mqtt-influxdb-gateway
-
Update the source code:
git pull
-
Update application dependencies:
poetry install --no-dev
-
Run the application again.
If you used the latest
tag, run docker-compose pull mqtt-influxdb-gateway
.
Optional, default value: INFO
Logging level.
TRACE
, DEBUG
, INFO
, SUCCESS
, WARNING
, ERROR
, CRITICAL
Required
URL of your InfluxDB Cloud instance.
Required
Organization ID. A hexadecimal string. Should be part of the cloud instance URL or in About Organization. This is not the organization name!
Required
API token with write access to influxdb_default_bucket
.
Optional, default value: ns
The precision for the unix timestamps within the body line-protocol.
s
, ms
, ns
, us
Optional, default value: None
Default value if {bucket}
in mqtt_topic_pattern
below is not set or parsed.
Optional, default value: None
Default value if {measurement}
in mqtt_topic_pattern
below is not set or parsed.
Required
IP address or a host name of an MQTT broker.
Optional, default value: 1883
Port of the MQTT broker.
Optional, default value: None
User name used to authenticate with the MQTT broker.
Optional, default value: None
Password used to authenticate with the MQTT broker.
Required
A topic to subscribe to. You can also use the '+' and '#' wildcards.
Required
A pattern to parse the topic into fields sent to InfluxDB. Use {variable}
or {variable_type:value}
syntax in topic parts to create the match. Use a plain string to ignore a topic part. The pattern must be a prefix of mqtt_topic_subscribe
.
When both a variable and a influxdb_*
configuration option are defined, variable takes precedence.
dt/influxdb/{bucket}/{measurement}/{tag:device_id}/{field}/{value_type}
will parse dt/influxdb/home/environment/esp8266/temperature/float 23.412
into:
{
"bucket": "home",
"measurement": "environment",
"tags": {"device_id": "esp8266"},
"fields": {"temperature": 23.412}
}
{bucket}
: InfluxDB bucket to send data to. Required unlessinfluxdb_default_bucket
is set.{measurement}
: InfluxDB measurement field. Required unlessinfluxdb_default_measurement
is set.{field}
: Field name. Required.{value_type}
: Value type used to parse the received data. Optional. Acceptable values in the topic arestr
,int
,float
andbool
. Default value isstr
.{tag:TAG_NAME}
: where TAG_NAME will be used as a tag name and the parsed value as a value. Optional.
Optional, default value: {measurement}{bucket}{tags}
It is expected there will be a single value in each topic. But InfluxDB allows multiple fields to be sent at once (for example from one device). Use this option to define criteria on which to merge data points. The order and any extra characters aside from variables below are irrelevant. All collected fields are then sent to InfluxDB when any of the fields is seen more than once (suggesting new data is coming in).
A value of {tags[device_id]}
will merge all received data that share the same device_id
tag.
{bucket}
{measurement}
{tags}
: for all tags and their values{tags[NAME]}
: for a value of single tag namedNAME
At the moment, build for ARMv7 takes about 15-20 minutes. This is caused by the cryptography package switching to Rust compiler (some info here). The build process is significantly slower and not suitable for running in the free tier CircleCI build pipeline.
To build and push the image manually, run:
inv docker-build
https://github.com/eclipse/paho.mqtt.python#contents
- https://docs.influxdata.com/influxdb/cloud/write-data/developer-tools/api/
- https://docs.influxdata.com/influxdb/cloud/api/#operation/PostWrite
- Define what to merge data points on.