16static constexpr uint32_t DATA_TIMEOUT_ID = 0;
18static const uint8_t BME280_REGISTER_DIG_T1 = 0x88;
19static const uint8_t BME280_REGISTER_DIG_T2 = 0x8A;
20static const uint8_t BME280_REGISTER_DIG_T3 = 0x8C;
22static const uint8_t BME280_REGISTER_DIG_P1 = 0x8E;
23static const uint8_t BME280_REGISTER_DIG_P2 = 0x90;
24static const uint8_t BME280_REGISTER_DIG_P3 = 0x92;
25static const uint8_t BME280_REGISTER_DIG_P4 = 0x94;
26static const uint8_t BME280_REGISTER_DIG_P5 = 0x96;
27static const uint8_t BME280_REGISTER_DIG_P6 = 0x98;
28static const uint8_t BME280_REGISTER_DIG_P7 = 0x9A;
29static const uint8_t BME280_REGISTER_DIG_P8 = 0x9C;
30static const uint8_t BME280_REGISTER_DIG_P9 = 0x9E;
32static const uint8_t BME280_REGISTER_DIG_H1 = 0xA1;
33static const uint8_t BME280_REGISTER_DIG_H2 = 0xE1;
34static const uint8_t BME280_REGISTER_DIG_H3 = 0xE3;
35static const uint8_t BME280_REGISTER_DIG_H4 = 0xE4;
36static const uint8_t BME280_REGISTER_DIG_H5 = 0xE5;
37static const uint8_t BME280_REGISTER_DIG_H6 = 0xE7;
39static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
40static const uint8_t BME280_REGISTER_RESET = 0xE0;
42static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2;
43static const uint8_t BME280_REGISTER_STATUS = 0xF3;
44static const uint8_t BME280_REGISTER_CONTROL = 0xF4;
45static const uint8_t BME280_REGISTER_CONFIG = 0xF5;
46static const uint8_t BME280_REGISTER_MEASUREMENTS = 0xF7;
47static const uint8_t BME280_REGISTER_PRESSUREDATA = 0xF7;
48static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA;
49static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD;
51static const uint8_t BME280_MODE_FORCED = 0b01;
52static const uint8_t BME280_SOFT_RESET = 0xB6;
53static const uint8_t BME280_STATUS_IM_UPDATE = 0b01;
59 return IIRFilterStrings::get_log_str(
static_cast<uint8_t
>(
value), IIRFilterStrings::LAST_INDEX);
66 return OversamplingStrings::get_log_str(
static_cast<uint8_t
>(
value), OversamplingStrings::LAST_INDEX);
69void BME280Component::setup() {
78 if (!this->
read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
82 if (chip_id != 0x60) {
83 this->
mark_failed(LOG_STR(
"Wrong chip ID or no response"));
88 if (!this->
write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
97 if (!this->
read_byte(BME280_REGISTER_STATUS, &status)) {
98 this->
mark_failed(LOG_STR(
"Error reading status register"));
101 }
while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
102 if (status & BME280_STATUS_IM_UPDATE) {
131 uint8_t humid_control_val = 0;
132 if (!this->
read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
133 this->
mark_failed(LOG_STR(
"Read humidity control"));
136 humid_control_val &= ~0b00000111;
138 if (!this->
write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
139 this->
mark_failed(LOG_STR(
"Write humidity control"));
143 uint8_t config_register = 0;
144 if (!this->
read_byte(BME280_REGISTER_CONFIG, &config_register)) {
148 config_register &= ~0b11111100;
149 config_register |= 0b101 << 5;
150 config_register |= (this->
iir_filter_ & 0b111) << 2;
151 if (!this->
write_byte(BME280_REGISTER_CONFIG, config_register)) {
156void BME280Component::dump_config() {
157 ESP_LOGCONFIG(TAG,
"BME280:");
158 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->
iir_filter_)));
159 LOG_UPDATE_INTERVAL(
this);
161 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
163 LOG_SENSOR(
" ",
"Pressure", this->pressure_sensor_);
165 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
171void BME280Component::update() {
173 ESP_LOGV(TAG,
"Sending conversion request");
178 uint8_t meas_value = 0;
181 meas_value |= BME280_MODE_FORCED;
182 if (!this->
write_byte(BME280_REGISTER_CONTROL, meas_value)) {
187 float meas_time = 1.5f;
194 if (!this->
read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
195 ESP_LOGW(TAG,
"Error reading registers");
202 ESP_LOGW(TAG,
"Invalid temperature");
209 ESP_LOGV(TAG,
"Temperature=%.1f°C Pressure=%.1fhPa Humidity=%.1f%%",
temperature,
pressure, humidity);
210 PUBLISH_STATE(this->temperature_sensor_,
temperature);
211 PUBLISH_STATE(this->pressure_sensor_,
pressure);
212 PUBLISH_STATE(this->humidity_sensor_, humidity);
227 int32_t
const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
228 int32_t
const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
229 *t_fine = var1 + var2;
251 int64_t var1, var2, p;
252 var1 = int64_t(t_fine) - 128000;
253 var2 = var1 * var1 * p6;
254 var2 = var2 + ((var1 * p5) << 17);
255 var2 = var2 + (p4 << 35);
256 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
257 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
263 p = (((p << 31) - var2) * 3125) / var1;
264 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
265 var2 = (p8 * p) >> 19;
267 p = ((p + var1 + var2) >> 8) + (p7 << 4);
268 return (p / 256.0f) / 100.0f;
284 int32_t v_x1_u32r = t_fine - 76800;
286 v_x1_u32r = ((((adc << 14) - (h4 << 20) - (h5 * v_x1_u32r)) + 16384) >> 15) *
287 (((((((v_x1_u32r * h6) >> 10) * (((v_x1_u32r * h3) >> 11) + 32768)) >> 10) + 2097152) * h2 + 8192) >> 14);
289 v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
291 v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
292 v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
293 float const h = v_x1_u32r >> 12;
297void BME280Component::set_temperature_oversampling(
BME280Oversampling temperature_over_sampling) {
300void BME280Component::set_pressure_oversampling(
BME280Oversampling pressure_over_sampling) {
303void BME280Component::set_humidity_oversampling(
BME280Oversampling humidity_over_sampling) {