Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Loading