Skip to content

ESP32 DevKit v1 home sensor (bme280) with InfluxDB, Telegraf and Mosquitto MQTT

License

Notifications You must be signed in to change notification settings

etiennetremel/esp32-home-sensor

Repository files navigation

ESP-32 home sensor

ESP32 DevKit v1 home sensor connected via I2C to a BME280 sensor which measure temperature, humidity and pressure. Measurements are sent over MQTTv5 to Mosquitto MQTT broker which are then consumed by Telegraf and persisted into InfluxDB.

Overview

flowchart LR
    A(fa:fa-microchip ESP32<br /><small><i>outdoor front</i></small>)
    B(fa:fa-microchip ESP32<br /><small><i>indoor living room</i></small>)
    C(fa:fa-microchip ESP32<br /><small><i>indoor bedroom</i></small>)
    D(fa:fa-tower-broadcast Mosquitto MQTT)
    E(fa:fa-gear Telegraf)
    F(fa:fa-database InfluxDB)
    G(fa:fa-chart-line Dashboards)

    A-->D
    B-->D
    C-->D
    D-->E
    E-->F
    F-->G

Getting started

ESP32

Requirements

Pin-out

Sensor Pin Sensor ESP32 DevKit v1 Pin
BME280 SDA GPIO 21 / D21
BME280 SCL GPIO 22 / D22
BME280 GND GND
BME280 3.3v 3v3
SDS011 RX GPIO 17 / U2:TXD
SDS011 TX GPIO 16 / U2:RXD
SDS011 GND GND
SDS011 5v 3v3

Configuration

Before flashing the device, you will need to configure parameters in the ./cfg.toml file, for example:

[esp32_home_sensor]
wifi_ssid = "my-wifi"
wifi_psk = "wifi-password"
hostname = "esp32-outdoor"
mqtt_hostname = "homie.local"
mqtt_port = 1883
mqtt_username = "esp32-outdoor"
mqtt_password = "someranddompassword"
mqtt_topic = "sensors"
location = "outdoor"

Development

Connect the device via USB, then run the following command to run flash and retrieve logs from the device using espmonitor:

espup update

. $HOME/export-esp.sh

# run specific features/sensors
cargo run --features=influx,bme280

Flashing

Connect the device via USB, then flash it with the following command:

. $HOME/export-esp.sh
cargo espflash flash --release --features=json,sds011,bme280

Available features

The following Cargo features allow you to enable/disable sensors and select the MQTT message format to use:

Feature Description
bme280 Enable BME280 sensor
sds011 Enable SDS011 sensor
influx Set MQTT payload format to Influx
json Set MQTT payload format to Json

For example, to only enable BME280 with JSON format:

. $HOME/export-esp.sh
# enable BME280 with MQTT message in INFLUX format
cargo espflash flash --release --features influx,bme280

# enable BME280, SDS011 with MQTT message in JSON format
cargo espflash flash --release --features json,bme280,sds011

Setup infrastructure using Docker

Docker compose is used to setup the infrastructure. It is composed of 4 services:

  • InfluxDB - persistent storage and basic dash-boarding
  • Mosquitto - MQTT broker
  • Telegraf - consume MQTT messages and store them in the database
  • A bash job to setup InfluxDB user/token

First, review/adjust environment variables in ./infra/influxdb.env. Then, for each device (one per line) define a username/password in ./infra/mqtt.auth file. For example:

esp32-outdoor:a19sn#sA94k!a5o10
esp32-indoor:a93KifAoBf7#01-jl

After what, run docker compose up -d to deploy the infrastructure.

Note, you might have to restart all containers to get the environment variables re-populated into the container after being generated during initial boot.

You can visualise metrics by importing ./infra/influx-dashboard.json as dashboard into InfluxDB.

InfluxDB dashboard

Setup Home Assistant or other providers

It's possible to change the payload format of the MQTT message to JSON instead of Influx by using the --features json.

Refer to the Home Assistant documentation for details on how to set this up.

Home Assistant dashboard