This repository implements a MQTT5 broker, InfluxDB 2.x, Telegraf and Arduino ESP32 MQTT PubSubClient to send data to InfluxDB 2.x. The sensor data corresponds to a DHT11 temperature and humidity sensor.
Table of Contents
- Docker
- Docker Compose
- Arduino IDE
- ESP32
- DHT11 sensor
- Clone this repository
- Open the Arduino IDE and install the PubSubClient and Adafruit DHT Sensor libraries
- Open the
secrets.h
file and replace the placeholders with your WiFi and MQTT broker credentials - Open the
publish.ino
file and replace theDHTPIN
,MQTT_CLIENT_ID
,MQTT_SERVER
,MQTT_TOPIC
with your own values - Upload the sketch to your ESP32
- Open the
telegraf.conf
file and replace theservers
andtopics
with your own values
-
Create a
.env
file with the following variables:MQTT_USERNAME=changeme MQTT_PASSWORD=changeme INFLUXDB_TOKEN=default DOCKER_INFLUXDB_INIT_USERNAME=changeme DOCKER_INFLUXDB_INIT_PASSWORD=changeme DOCKER_INFLUXDB_INIT_ORG=changeme DOCKER_INFLUXDB_INIT_BUCKET=changeme
Note:
MQTT_USERNAME
andMQTT_PASSWORD
must match the credentials in thesecrets.h
file. This will later be created as a user in the MQTT broker. Also theINFLUXDB_TOKEN
will be later created as a token in InfluxDB. -
Create a Mosquitto password file
touch mqtt_broker/config/pwfile
-
Create and run docker container for only mqtt5 broker
docker compose up -d mqtt5
-
Create a user in the MQTT broker. Replace
<MQTT_USERNAME>
and<MQTT_PASSWORD>
with your own values also used in thesecrets.h
file.docker compose exec mqtt5 mosquitto_passwd -b /mosquitto/config/pwfile <MQTT_USERNAME> <MQTT_PASSWORD>
-
Create Access Control List (ACL) file to give authorization to certain topics to each user
nano mqtt_broker/config/aclfile
Include the following lines
# Give admin user full access to everything user admin topic # # Allow user 2 read/write to topic foo/bar/baz user user2 topic foo/bar/#
-
Create and run all docker containers
docker compose -p arduino-mqtt-influx up --build -d
-
Open the InfluxDB UI at
http://localhost:8086
and create a new API token through theLoad Data
->API Tokens
menu. Replace theINFLUXDB_TOKEN
in the.env
file with the newly created token. -
Recreate all docker containers
docker compose -p arduino-mqtt-influx up --build -d