Skip to content

Commit

Permalink
Make i2c address changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
LordMike committed May 10, 2024
1 parent b9ca2cf commit d4ba0ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions usermods/BME280_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This Usermod is designed to read a `BME280` or `BMP280` sensor and output the fo
- Dew Point (`BME280` only)

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:
- The i2c address in decimal. Set it to either 118 (0x76, the default) or 119 (0x77). **Requires reboot**.
- Temperature Decimals (number of decimal places to output)
- Humidity Decimals
- Pressure Decimals
Expand Down
11 changes: 10 additions & 1 deletion usermods/BME280_v2/usermod_bme280.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class UsermodBME280 : public Usermod
bool UseCelsius = true; // Use Celsius for Reporting
bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information
bool enabled = true;
BME280I2C::I2CAddr i2cAddress = BME280I2C::I2CAddr_0x76; // Default i2c address for BME280

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

BME280I2C bme{settings};
BME280I2C bme;

uint8_t sensorType;

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

settings.bme280Addr = i2cAddress;
bme = BME280I2C(settings);

if (!bme.begin())
{
sensorType = 0;
Expand Down Expand Up @@ -399,6 +403,7 @@ class UsermodBME280 : public Usermod
{
JsonObject top = root.createNestedObject(FPSTR(_name));
top[FPSTR(_enabled)] = enabled;
top[F("I2CAddress")] = i2cAddress;
top[F("TemperatureDecimals")] = TemperatureDecimals;
top[F("HumidityDecimals")] = HumidityDecimals;
top[F("PressureDecimals")] = PressureDecimals;
Expand Down Expand Up @@ -426,6 +431,10 @@ class UsermodBME280 : public Usermod

configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled);
// A 3-argument getJsonValue() assigns the 3rd argument as a default value if the Json value is missing
uint8_t tmpI2cAddress;
configComplete &= getJsonValue(top[F("I2CAddress")], tmpI2cAddress, 0x76);
i2cAddress = static_cast<BME280I2C::I2CAddr>(tmpI2cAddress);

configComplete &= getJsonValue(top[F("TemperatureDecimals")], TemperatureDecimals, 1);
configComplete &= getJsonValue(top[F("HumidityDecimals")], HumidityDecimals, 0);
configComplete &= getJsonValue(top[F("PressureDecimals")], PressureDecimals, 0);
Expand Down

0 comments on commit d4ba0ba

Please sign in to comment.