11static constexpr uint32_t DATA_TIMEOUT_ID = 0;
13static const uint8_t BME680_REGISTER_COEFF1 = 0x89;
14static const uint8_t BME680_REGISTER_COEFF2 = 0xE1;
16static const uint8_t BME680_REGISTER_CONFIG = 0x75;
17static const uint8_t BME680_REGISTER_CONTROL_MEAS = 0x74;
18static const uint8_t BME680_REGISTER_CONTROL_HUMIDITY = 0x72;
19static const uint8_t BME680_REGISTER_CONTROL_GAS1 = 0x71;
20static const uint8_t BME680_REGISTER_CONTROL_GAS0 = 0x70;
21static const uint8_t BME680_REGISTER_HEATER_HEAT0 = 0x5A;
22static const uint8_t BME680_REGISTER_HEATER_WAIT0 = 0x64;
24static const uint8_t BME680_REGISTER_CHIPID = 0xD0;
26static const uint8_t BME680_REGISTER_FIELD0 = 0x1D;
28constexpr float BME680_GAS_LOOKUP_TABLE_1[16]
PROGMEM = {0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -0.8,
29 0.0, 0.0, -0.2, -0.5, 0.0, -1.0, 0.0, 0.0};
31constexpr float BME680_GAS_LOOKUP_TABLE_2[16]
PROGMEM = {0.0, 0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8,
32 -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
38 return OversamplingStrings::get_log_str(
static_cast<uint8_t
>(
value), OversamplingStrings::LAST_INDEX);
45 return IIRFilterStrings::get_log_str(
static_cast<uint8_t
>(
value), IIRFilterStrings::LAST_INDEX);
48void BME680Component::setup() {
55 if (!this->
read_byte(BME680_REGISTER_CHIPID, &chip_id) || chip_id != 0x61) {
61 if (!this->
read_bytes(BME680_REGISTER_COEFF1, coeff1, 25)) {
65 if (!this->
read_bytes(BME680_REGISTER_COEFF2, coeff2, 16)) {
125 if (!this->
update_register(BME680_REGISTER_CONTROL_GAS1, 0b00011111, 1 << 4)) {
132 if (!this->
update_register(BME680_REGISTER_CONTROL_GAS0, 0b00001000, heat_off << 3)) {
153void BME680Component::dump_config() {
154 ESP_LOGCONFIG(TAG,
"BME680:");
155 LOG_I2C_DEVICE(
this);
156 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->
iir_filter_)));
157 LOG_UPDATE_INTERVAL(
this);
159 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
161 LOG_SENSOR(
" ",
"Pressure", this->pressure_sensor_);
163 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
165 LOG_SENSOR(
" ",
"Gas Resistance", this->gas_resistance_sensor_);
167 ESP_LOGCONFIG(TAG,
" Heater OFF");
173void BME680Component::update() {
174 uint8_t meas_control = 0;
177 meas_control |= 0b01;
178 if (!this->
write_byte(BME680_REGISTER_CONTROL_MEAS, meas_control)) {
205 int32_t heatr_res_x100;
207 var1 = (((int32_t) ambient_temperature * gh3) / 1000) * 256;
208 var2 = (gh1 + 784) * (((((gh2 + 154009) *
temperature * 5) / 100) + 3276800) / 10);
209 var3 = var1 + (var2 / 2);
210 var4 = (var3 / (res_heat_range + 4));
211 var5 = (131 * res_heat_val) + 65536;
212 heatr_res_x100 = (int32_t) (((var4 / var5) - 250) * 34);
213 heatr_res = (uint8_t) ((heatr_res_x100 + 50) / 100);
219 uint8_t duration_value;
222 duration_value = 0xff;
228 duration_value =
duration + (factor * 64);
231 return duration_value;
236 PUBLISH_STATE(this->temperature_sensor_, NAN);
237 PUBLISH_STATE(this->pressure_sensor_, NAN);
238 PUBLISH_STATE(this->humidity_sensor_, NAN);
239 PUBLISH_STATE(this->gas_resistance_sensor_, NAN);
240 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
248 uint8_t gas_range = data[14] & 0x0F;
255 bool gas_valid = (data[14] >> 5) & 1;
256 bool heat_stable = (data[14] >> 4) & 1;
260 ESP_LOGD(TAG,
"Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%% gas_resistance=%.1fΩ",
temperature,
pressure,
261 humidity, gas_resistance);
263 ESP_LOGW(TAG,
"Gas measurement unsuccessful, reading invalid!");
266 ESP_LOGW(TAG,
"Heater unstable, reading invalid! (Normal for a few readings after a power cycle)");
269 PUBLISH_STATE(this->temperature_sensor_,
temperature);
270 PUBLISH_STATE(this->pressure_sensor_,
pressure);
271 PUBLISH_STATE(this->humidity_sensor_, humidity);
272 if (this->gas_resistance_sensor_ !=
nullptr) {
273 if (gas_valid && heat_stable) {
274 this->gas_resistance_sensor_->publish_state(gas_resistance);
277 this->gas_resistance_sensor_->publish_state(NAN);
287 float temp_adc = raw_temperature;
294 var1 = ((temp_adc / 16384.0f) - (t1 / 1024.0f)) * t2;
297 var3 = (temp_adc / 131072.0f) - (t1 / 8192.0f);
298 var2 = var3 * var3 * t3 * 16.0f;
327 var1 = (tfine / 2.0f) - 64000.0f;
328 var2 = var1 * var1 * (p6 / 131072.0f);
329 var2 = var2 + var1 * p5 * 2.0f;
330 var2 = (var2 / 4.0f) + (p4 * 65536.0f);
331 var1 = (((p3 * var1 * var1) / 16384.0f) + (p2 * var1)) / 524288.0f;
332 var1 = (1.0f + (var1 / 32768.0f)) * p1;
333 calc_pres = 1048576.0f - float(raw_pressure);
336 if (
int(var1) != 0) {
337 calc_pres = ((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1;
338 var1 = (p9 * calc_pres * calc_pres) / 2147483648.0f;
339 var2 = calc_pres * (p8 / 32768.0f);
340 var4 = calc_pres / 256.0f;
341 var3 = var4 * var4 * var4 * (p10 / 131072.0f);
342 calc_pres = calc_pres + (var1 + var2 + var3 + (p7 * 128.0f)) / 16.0f;
347 return calc_pres / 100.0f;
368 temp_comp = tfine / 5120.0f;
370 var1 = float(raw_humidity) - (h1 * 16.0f + ((h3 / 2.0f) * temp_comp));
372 (((h2 / 262144.0f) * (1.0f + ((h4 / 16384.0f) * temp_comp) + ((h5 / 1048576.0f) * temp_comp * temp_comp))));
373 var3 = h6 / 16384.0f;
374 var4 = h7 / 2097152.0f;
376 calc_hum = var2 + (var3 + var4 * temp_comp) * var2 * var2;
378 if (calc_hum > 100.0f) {
380 }
else if (calc_hum < 0.0f) {
391 float raw_gas_f = raw_gas;
392 float range_f = 1U <<
range;
395 var1 = 1340.0f + (5.0f * range_sw_err);
396 var2 = var1 * (1.0f + BME680_GAS_LOOKUP_TABLE_1[
range] / 100.0f);
397 var3 = 1.0f + (BME680_GAS_LOOKUP_TABLE_2[
range] / 100.0f);
399 calc_gas_res = 1.0f / (var3 * 0.000000125f * range_f * (((raw_gas_f - 512.0f) / var2) + 1.0f));
406 const uint8_t os_to_meas_cycles[6] = {0, 1, 2, 4, 8, 16};
413 tph_dur = meas_cycles * 1963u;
426void BME680Component::set_temperature_oversampling(
BME680Oversampling temperature_oversampling) {
429void BME680Component::set_pressure_oversampling(
BME680Oversampling pressure_oversampling) {
432void BME680Component::set_humidity_oversampling(
BME680Oversampling humidity_oversampling) {
435void BME680Component::set_heater(uint16_t heater_temperature, uint16_t heater_duration) {
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
uint8_t calc_heater_duration_(uint16_t duration)
Calculate the heater duration value to send to the BME680 register.
void read_data_()
Read data from the BME680 and publish results.
uint8_t calc_heater_resistance_(uint16_t temperature)
Calculate the heater resistance value to send to the BME680 register.
float calc_humidity_(uint16_t raw_humidity)
Calculate the relative humidity in % using the provided raw ADC value.
BME680Oversampling pressure_oversampling_
bool setup_registers_()
Configure the sensor registers; false on an I2C failure.
BME680CalibrationData calibration_
BME680Oversampling temperature_oversampling_
uint16_t heater_temperature_
BME680Oversampling humidity_oversampling_
float calc_gas_resistance_(uint16_t raw_gas, uint8_t range)
Calculate the gas resistance in Ω using the provided raw ADC value.
uint16_t heater_duration_
uint32_t calc_meas_duration_()
Calculate how long the sensor will take until we can retrieve data.
float calc_temperature_(uint32_t raw_temperature)
Calculate the temperature in °C using the provided raw ADC value.
float calc_pressure_(uint32_t raw_pressure)
Calculate the pressure in hPa using the provided raw ADC value.
BME680IIRFilter iir_filter_
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
PROGMEM_STRING_TABLE(OversamplingStrings, "None", "1x", "2x", "4x", "8x", "16x", "UNKNOWN")
BME680IIRFilter
Enum listing all IIR Filter options for the BME680.
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
BME680Oversampling
Enum listing all oversampling options for the BME680.
ESPHOME_LOG_TAG(TAG, "bme680.sensor")
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
int8_t ambient_temperature