Skip to content

Commit 43d024f

Browse files
committed
Make BME280 usermod i2c address changeable
1 parent c016ded commit 43d024f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

usermods/BME280_v2/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This Usermod is designed to read a `BME280` or `BMP280` sensor and output the fo
77
- Dew Point (`BME280` only)
88

99
Configuration is performed via the Usermod menu. There are no parameters to set in code! The following settings can be configured in the Usermod Menu:
10+
- The i2c address in decimal. Set it to either 118 (0x76, the default) or 119 (0x77). **Requires reboot**.
1011
- Temperature Decimals (number of decimal places to output)
1112
- Humidity Decimals
1213
- Pressure Decimals

usermods/BME280_v2/usermod_bme280.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class UsermodBME280 : public Usermod
2828
bool UseCelsius = true; // Use Celsius for Reporting
2929
bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information
3030
bool enabled = true;
31+
BME280I2C::I2CAddr i2cAddress = BME280I2C::I2CAddr_0x76; // Default i2c address for BME280
3132

3233
// set the default pins based on the architecture, these get overridden by Usermod menu settings
3334
#ifdef ESP8266
@@ -48,7 +49,7 @@ class UsermodBME280 : public Usermod
4849
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific. Default 0x76
4950
};
5051

51-
BME280I2C bme{settings};
52+
BME280I2C bme;
5253

5354
uint8_t sensorType;
5455

@@ -186,6 +187,9 @@ class UsermodBME280 : public Usermod
186187
{
187188
if (i2c_scl<0 || i2c_sda<0) { enabled = false; sensorType = 0; return; }
188189

190+
settings.bme280Addr = i2cAddress;
191+
bme = BME280I2C(settings);
192+
189193
if (!bme.begin())
190194
{
191195
sensorType = 0;
@@ -399,6 +403,7 @@ class UsermodBME280 : public Usermod
399403
{
400404
JsonObject top = root.createNestedObject(FPSTR(_name));
401405
top[FPSTR(_enabled)] = enabled;
406+
top[F("I2CAddress")] = i2cAddress;
402407
top[F("TemperatureDecimals")] = TemperatureDecimals;
403408
top[F("HumidityDecimals")] = HumidityDecimals;
404409
top[F("PressureDecimals")] = PressureDecimals;
@@ -426,6 +431,10 @@ class UsermodBME280 : public Usermod
426431

427432
configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled);
428433
// A 3-argument getJsonValue() assigns the 3rd argument as a default value if the Json value is missing
434+
uint8_t tmpI2cAddress;
435+
configComplete &= getJsonValue(top[F("I2CAddress")], tmpI2cAddress, 0x76);
436+
i2cAddress = static_cast<BME280I2C::I2CAddr>(tmpI2cAddress);
437+
429438
configComplete &= getJsonValue(top[F("TemperatureDecimals")], TemperatureDecimals, 1);
430439
configComplete &= getJsonValue(top[F("HumidityDecimals")], HumidityDecimals, 0);
431440
configComplete &= getJsonValue(top[F("PressureDecimals")], PressureDecimals, 0);

0 commit comments

Comments
 (0)