timothybrown / BSEC-Conduit

A first class Systemd process which acts as a conduit between between BSEC Library and MQTT.
MIT License
13 stars 11 forks source link

BSEC-Conduit Daemon

A first class Systemd process which acts as a conduit between between BSEC-Library and MQTT. Provides an alternative method of getting data out of an I2C connected Bosch BME680 sensor and into Home Assistant. Much more accurate than the native HA BME680 module, as it uses the Bosch Sensortec Environmental Cluster (BSEC) fusion library to process the raw BME680 sensor readings.

Thanks to @rstoermer for bsec_bme680.py upon which I based this. (https://github.com/rstoermer/bsec_bme680_python/)

Attribution

Requirements

sudo apt-get install python3-systemd python3-paho.mqtt or pip3 install python-systemd paho.mqtt

Installation

In this example we'll be installing into a Python venv located at /opt/bsec with the user pi on a recent Debian based distro (Raspbian/Hassbian). You can use any location and user you want, just make sure they are a member of the i2c group.

Usage

Here's a typical log output when started for the first time, stopping and subsequent runs:

pi@raspberrypi ~# systemctl start bsec-conduit.service

 systemd[1]: Starting BSEC-Conduit Daemon...
 raspberrypi BSEC-Conduit[1234]: BSEC-Conduit v0.3.3
 raspberrypi BSEC-Conduit[1234]: Generated MQTT Client ID: BME680-A12BC3D4
 raspberrypi BSEC-Conduit[1234]: Generated MQTT Base Topic: raspberrypi/BME680
 raspberrypi BSEC-Conduit[1234]: Connected to MQTT Broker.
 raspberrypi BSEC-Conduit[1234]: BSEC-Library executable or hash file not found, starting build process.
 raspberrypi BSEC-Conduit[1234]: BSEC-Library source file not found, writing file: /opt/bsec/BSEC_1.4.7.1_Generic_Release_20180907/bsec-library.c
 raspberrypi BSEC-Conduit[1234]: Detected architecture as ARMv8 64-Bit.
 raspberrypi BSEC-Conduit[1234]: Build process complete.
 raspberrypi BSEC-Conduit[1234]: Created new BSEC-Library configuration [generic_33v_3s_28d].
 raspberrypi BSEC-Conduit[1234]: Created blank BSEC-Library state file.
 raspberrypi BSEC-Conduit[1234]: BSEC-Library started.
 raspberrypi systemd[1]: Started BSEC-Conduit Daemon.

pi@raspberrypi ~# systemctl stop bsec-conduit.service

raspberrypi systemd[1]: Stopping BSEC-Conduit Daemon...
raspberrypi BSEC-Conduit[1234]: Caught Signal 15 (SIGTERM).
raspberrypi BSEC-Conduit[1234]: BSEC-Library stopped.
raspberrypi BSEC-Conduit[1234]: Disconnected from MQTT Broker.
systemd[1]: Stopped BSEC-Conduit Daemon.

pi@raspberrypi ~# systemctl start bsec-conduit.service

 systemd[1]: Starting BSEC-Conduit Daemon...
 raspberrypi BSEC-Conduit[2345]: BSEC-Conduit v0.3.3
 raspberrypi BSEC-Conduit[2345]: Generated MQTT Client ID: BME680-A12BC3D4
 raspberrypi BSEC-Conduit[2345]: Generated MQTT Base Topic: raspberrypi/BME680
 raspberrypi BSEC-Conduit[2345]: Connected to MQTT Broker.
 raspberrypi BSEC-Conduit[2345]: Found existing BSEC-Library executable, skipping build.
 raspberrypi BSEC-Conduit[2345]: Using existing BSEC-Library configuration [generic_33v_3s_28d].
 raspberrypi BSEC-Conduit[2345]: Found existing BSEC-Library state file, skipping creation.
 raspberrypi BSEC-Conduit[2345]: BSEC-Library started.
 raspberrypi systemd[1]: Started BSEC-Conduit Daemon.

Version History

BSECLibrary

Uses the Bosch BSEC sensor fusion library to retrieve and process data from a BME680 sensor.

Attribution

Usage

BSECLibrary(i2c_address, temp_offset, sample_rate, voltage, retain_state, logger=None, base_dir=None)

BSECLibrary.open()

Call to start the underlying BSEC-Library communication process.

BSECLibrary.close()

Call to stop the underlying BSEC-Library communication process.

BSECLibrary.output()

Returns an iterator that you can loop over forever. Blocks between samples from the sensor. Each item is a dict() that contains the following keys:

Example

from bseclib import BSECLibrary

bsec_lib = BSECLibrary(0x77, 2.0, 3, 3.3, 4)
count = 0
bsec_lib.open()
for sample in bsec_lib.output():
    print(sample)
    if count == 10:
        bsec_lib.close()
        exit()
    else:
        count += 1

Version History