From 290f6965028206715c43d7a936c3b97919563df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Delr=C3=A9?= Date: Thu, 14 May 2026 11:52:44 +0200 Subject: [PATCH] feat: add IIR filter configuration via BME280_IIR_FILTER env var Write config register 0xF5 (bits [4:2]) before each measurement. Defaults to 0 (filter off) to preserve existing behavior. Recommended value for indoor use is 2 (4x coefficient). Document the new variable in .env.example and README. Co-authored-by: agilicode --- .env.example | 2 ++ README.md | 2 ++ bme280.py | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/.env.example b/.env.example index 34b95a7..389bd2d 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,8 @@ # I2C configuration BME280_I2C_BUS=1 BME280_I2C_ADDRESS=0x77 +# IIR filter coefficient: 0=off 1=2x 2=4x 3=8x 4=16x (recommended: 2 for indoor use) +BME280_IIR_FILTER=0 # MQTT broker MQTT_BROKER_HOST=192.168.1.x diff --git a/README.md b/README.md index cc64217..771dc7b 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ All settings are driven by environment variables. Copy `.env.example` to `.env` |----------|---------|-------------| | `BME280_I2C_BUS` | `1` | I²C bus number (`1` for Pi Rev 2+, `0` for Rev 1) | | `BME280_I2C_ADDRESS` | `0x77` | Sensor I²C address (`0x77` or `0x76` depending on SDO pin) | +| `BME280_IIR_FILTER` | `0` | IIR filter coefficient: `0`=off, `1`=2×, `2`=4×, `3`=8×, `4`=16× (recommended: `2` for indoor use) | | `MQTT_BROKER_HOST` | `localhost` | IP or hostname of your MQTT broker | | `MQTT_USERNAME` | _(empty)_ | MQTT username (leave empty for anonymous) | | `MQTT_PASSWORD` | _(empty)_ | MQTT password | @@ -123,6 +124,7 @@ All settings are driven by environment variables. Copy `.env.example` to `.env` ```ini BME280_I2C_BUS=1 BME280_I2C_ADDRESS=0x77 +BME280_IIR_FILTER=0 MQTT_BROKER_HOST=192.168.1.10 MQTT_USERNAME=homeassistant diff --git a/bme280.py b/bme280.py index dc2374f..20dc1bc 100644 --- a/bme280.py +++ b/bme280.py @@ -7,6 +7,9 @@ I2C_BUS = int(os.environ.get('BME280_I2C_BUS', '1')) DEVICE_ADDRESS = int(os.environ.get('BME280_I2C_ADDRESS', '0x77'), 16) +# IIR filter coefficient: 0=off, 1=2, 2=4, 3=8, 4=16 (see datasheet table 28) +IIR_FILTER = int(os.environ.get('BME280_IIR_FILTER', '0')) + def _get_short(data: list[int], index: int) -> int: return c_short((data[index + 1] << 8) + data[index]).value @@ -54,6 +57,7 @@ def read_all(addr: int = DEVICE_ADDRESS) -> tuple[float, float, float]: time.sleep(0.002) # 2ms startup delay (datasheet section 4.2) _wait_nvm_copy(bus, addr) + bus.write_byte_data(addr, 0xF5, IIR_FILTER << 2) # config: filter bits [4:2] bus.write_byte_data(addr, 0xF2, OVERSAMPLE_HUM) bus.write_byte_data(addr, 0xF4, OVERSAMPLE_TEMP << 5 | OVERSAMPLE_PRES << 2 | MODE)