From f40f7a1776fc26ab6486619f05d7548ce09acd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Wojdy=C5=82a?= Date: Wed, 10 Jun 2026 20:19:50 +0200 Subject: [PATCH] Sensor init: verify chip IDs for INA226 and BME680_BSEC Prevent I2C sensor enumeration from picking up invalid sensors in case of address conflicts. E.g. by default BME680 has the same address as BME280 (0x76). BME680 and other BME sensors check for the correct chip id in begin(), but BME680_BSEC does not. INA260 and INA3221 already check IDs in begin(). INA219 is older and doesn't expose chip ID, it may conflict with other sensors if enabled but not present. --- src/helpers/sensors/EnvironmentSensorManager.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 73842d9ee..7485a374c 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -398,9 +398,18 @@ static void query_ina260(uint8_t ch, uint8_t, CayenneLPP& lpp) { #endif #if ENV_INCLUDE_INA226 +// Documented in the robtillaart INA226 header as the expected getDieID() value. +#define INA226_EXPECTED_DIE_ID 0x2260 + static uint8_t init_ina226(TwoWire*, uint8_t) { // INA226 static instance was constructed with address and wire. - if (!INA226.begin()) return 0; + if (!INA226.begin()) + return 0; + + // begin() doesn't check chip ID + if (INA226.getDieID() != INA226_EXPECTED_DIE_ID) + return 0; + INA226.setMaxCurrentShunt(TELEM_INA226_MAX_AMP, TELEM_INA226_SHUNT_VALUE); return 1; } @@ -499,7 +508,8 @@ static void bsec_save_state() { static uint8_t init_bme680_bsec(TwoWire* wire, uint8_t addr) { bsec_iaq.begin(addr, *wire); - if (bsec_iaq.bsecStatus != BSEC_OK) return 0; + // bme68xStatus needs to be checked, it's set if the sensor doesn't report the expected ID + if (bsec_iaq.bsecStatus != BSEC_OK || bsec_iaq.bme68xStatus != BME68X_OK) return 0; bsec_iaq.setConfig(bsec_config_iaq); if (bsec_iaq.bsecStatus != BSEC_OK) return 0;