ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
bme280_base.cpp
Go to the documentation of this file.
1#include <cmath>
2#include <cstdint>
3
4#include "bme280_base.h"
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
9
10#define BME280_ERROR_WRONG_CHIP_ID "Wrong chip ID or no response"
11
13
14static const char *const TAG = "bme280.sensor";
15
16static const uint8_t BME280_REGISTER_DIG_T1 = 0x88;
17static const uint8_t BME280_REGISTER_DIG_T2 = 0x8A;
18static const uint8_t BME280_REGISTER_DIG_T3 = 0x8C;
19
20static const uint8_t BME280_REGISTER_DIG_P1 = 0x8E;
21static const uint8_t BME280_REGISTER_DIG_P2 = 0x90;
22static const uint8_t BME280_REGISTER_DIG_P3 = 0x92;
23static const uint8_t BME280_REGISTER_DIG_P4 = 0x94;
24static const uint8_t BME280_REGISTER_DIG_P5 = 0x96;
25static const uint8_t BME280_REGISTER_DIG_P6 = 0x98;
26static const uint8_t BME280_REGISTER_DIG_P7 = 0x9A;
27static const uint8_t BME280_REGISTER_DIG_P8 = 0x9C;
28static const uint8_t BME280_REGISTER_DIG_P9 = 0x9E;
29
30static const uint8_t BME280_REGISTER_DIG_H1 = 0xA1;
31static const uint8_t BME280_REGISTER_DIG_H2 = 0xE1;
32static const uint8_t BME280_REGISTER_DIG_H3 = 0xE3;
33static const uint8_t BME280_REGISTER_DIG_H4 = 0xE4;
34static const uint8_t BME280_REGISTER_DIG_H5 = 0xE5;
35static const uint8_t BME280_REGISTER_DIG_H6 = 0xE7;
36
37static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
38static const uint8_t BME280_REGISTER_RESET = 0xE0;
39
40static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2;
41static const uint8_t BME280_REGISTER_STATUS = 0xF3;
42static const uint8_t BME280_REGISTER_CONTROL = 0xF4;
43static const uint8_t BME280_REGISTER_CONFIG = 0xF5;
44static const uint8_t BME280_REGISTER_MEASUREMENTS = 0xF7;
45static const uint8_t BME280_REGISTER_PRESSUREDATA = 0xF7;
46static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA;
47static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD;
48
49static const uint8_t BME280_MODE_FORCED = 0b01;
50static const uint8_t BME280_SOFT_RESET = 0xB6;
51static const uint8_t BME280_STATUS_IM_UPDATE = 0b01;
52
53inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
54
55const char *iir_filter_to_str(BME280IIRFilter filter) { // NOLINT
56 switch (filter) {
58 return "OFF";
60 return "2x";
62 return "4x";
64 return "8x";
66 return "16x";
67 default:
68 return "UNKNOWN";
69 }
70}
71
72const char *oversampling_to_str(BME280Oversampling oversampling) { // NOLINT
73 switch (oversampling) {
75 return "None";
77 return "1x";
79 return "2x";
81 return "4x";
83 return "8x";
85 return "16x";
86 default:
87 return "UNKNOWN";
88 }
89}
90
92 uint8_t chip_id = 0;
93
94 // Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
95 // and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
96 if (this->is_failed()) {
98 }
99
100 if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
101 this->error_code_ = COMMUNICATION_FAILED;
102 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
103 return;
104 }
105 if (chip_id != 0x60) {
106 this->error_code_ = WRONG_CHIP_ID;
107 this->mark_failed(LOG_STR(BME280_ERROR_WRONG_CHIP_ID));
108 return;
109 }
110
111 // Send a soft reset.
112 if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
113 this->mark_failed(LOG_STR("Reset failed"));
114 return;
115 }
116 // Wait until the NVM data has finished loading.
117 uint8_t status;
118 uint8_t retry = 5;
119 do { // NOLINT
120 delay(2);
121 if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
122 this->mark_failed(LOG_STR("Error reading status register"));
123 return;
124 }
125 } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
126 if (status & BME280_STATUS_IM_UPDATE) {
127 this->mark_failed(LOG_STR("Timeout loading NVM"));
128 return;
129 }
130
131 // Read calibration
132 this->calibration_.t1 = read_u16_le_(BME280_REGISTER_DIG_T1);
133 this->calibration_.t2 = read_s16_le_(BME280_REGISTER_DIG_T2);
134 this->calibration_.t3 = read_s16_le_(BME280_REGISTER_DIG_T3);
135
136 this->calibration_.p1 = read_u16_le_(BME280_REGISTER_DIG_P1);
137 this->calibration_.p2 = read_s16_le_(BME280_REGISTER_DIG_P2);
138 this->calibration_.p3 = read_s16_le_(BME280_REGISTER_DIG_P3);
139 this->calibration_.p4 = read_s16_le_(BME280_REGISTER_DIG_P4);
140 this->calibration_.p5 = read_s16_le_(BME280_REGISTER_DIG_P5);
141 this->calibration_.p6 = read_s16_le_(BME280_REGISTER_DIG_P6);
142 this->calibration_.p7 = read_s16_le_(BME280_REGISTER_DIG_P7);
143 this->calibration_.p8 = read_s16_le_(BME280_REGISTER_DIG_P8);
144 this->calibration_.p9 = read_s16_le_(BME280_REGISTER_DIG_P9);
145
146 this->calibration_.h1 = read_u8_(BME280_REGISTER_DIG_H1);
147 this->calibration_.h2 = read_s16_le_(BME280_REGISTER_DIG_H2);
148 this->calibration_.h3 = read_u8_(BME280_REGISTER_DIG_H3);
149 // h4 and h5 are signed 12-bit values; shift left then arithmetic right shift to sign-extend
150 int16_t h4_raw = read_u8_(BME280_REGISTER_DIG_H4) << 4 | (read_u8_(BME280_REGISTER_DIG_H4 + 1) & 0x0F);
151 this->calibration_.h4 = static_cast<int16_t>(h4_raw << 4) >> 4;
152 int16_t h5_raw = read_u8_(BME280_REGISTER_DIG_H5 + 1) << 4 | (read_u8_(BME280_REGISTER_DIG_H5) >> 4);
153 this->calibration_.h5 = static_cast<int16_t>(h5_raw << 4) >> 4;
154 this->calibration_.h6 = read_u8_(BME280_REGISTER_DIG_H6);
155
156 uint8_t humid_control_val = 0;
157 if (!this->read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
158 this->mark_failed(LOG_STR("Read humidity control"));
159 return;
160 }
161 humid_control_val &= ~0b00000111;
162 humid_control_val |= this->humidity_oversampling_ & 0b111;
163 if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
164 this->mark_failed(LOG_STR("Write humidity control"));
165 return;
166 }
167
168 uint8_t config_register = 0;
169 if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
170 this->mark_failed(LOG_STR("Read config"));
171 return;
172 }
173 config_register &= ~0b11111100;
174 config_register |= 0b101 << 5; // 1000 ms standby time
175 config_register |= (this->iir_filter_ & 0b111) << 2;
176 if (!this->write_byte(BME280_REGISTER_CONFIG, config_register)) {
177 this->mark_failed(LOG_STR("Write config"));
178 return;
179 }
180}
182 ESP_LOGCONFIG(TAG, "BME280:");
183 switch (this->error_code_) {
185 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
186 break;
187 case WRONG_CHIP_ID:
188 ESP_LOGE(TAG, BME280_ERROR_WRONG_CHIP_ID);
189 break;
190 case NONE:
191 default:
192 break;
193 }
194 ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_));
195 LOG_UPDATE_INTERVAL(this);
196
197 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
198 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_));
199 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
200 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_));
201 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
202 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->humidity_oversampling_));
203}
204
205inline uint8_t oversampling_to_time(BME280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
206
208 // Enable sensor
209 ESP_LOGV(TAG, "Sending conversion request");
210 uint8_t meas_value = 0;
211 meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
212 meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
213 meas_value |= BME280_MODE_FORCED;
214 if (!this->write_byte(BME280_REGISTER_CONTROL, meas_value)) {
215 this->status_set_warning();
216 return;
217 }
218
219 float meas_time = 1.5f;
220 meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
221 meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
222 meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
223
224 this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
225 uint8_t data[8];
226 if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
227 ESP_LOGW(TAG, "Error reading registers");
228 this->status_set_warning();
229 return;
230 }
231 int32_t t_fine = 0;
232 float const temperature = this->read_temperature_(data, &t_fine);
233 if (std::isnan(temperature)) {
234 ESP_LOGW(TAG, "Invalid temperature");
235 this->status_set_warning();
236 return;
237 }
238 float const pressure = this->read_pressure_(data, t_fine);
239 float const humidity = this->read_humidity_(data, t_fine);
240
241 ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa Humidity=%.1f%%", temperature, pressure, humidity);
242 if (this->temperature_sensor_ != nullptr)
243 this->temperature_sensor_->publish_state(temperature);
244 if (this->pressure_sensor_ != nullptr)
245 this->pressure_sensor_->publish_state(pressure);
246 if (this->humidity_sensor_ != nullptr)
247 this->humidity_sensor_->publish_state(humidity);
248 this->status_clear_warning();
249 });
250}
251float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
252 int32_t adc = ((data[3] & 0xFF) << 16) | ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
253 adc >>= 4;
254 if (adc == 0x80000) {
255 // temperature was disabled
256 return NAN;
257 }
258
259 const int32_t t1 = this->calibration_.t1;
260 const int32_t t2 = this->calibration_.t2;
261 const int32_t t3 = this->calibration_.t3;
262
263 int32_t const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
264 int32_t const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
265 *t_fine = var1 + var2;
266
267 float const temperature = (*t_fine * 5 + 128);
268 return temperature / 25600.0f;
269}
270
271float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
272 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
273 adc >>= 4;
274 if (adc == 0x80000) {
275 // pressure was disabled
276 return NAN;
277 }
278 const int64_t p1 = this->calibration_.p1;
279 const int64_t p2 = this->calibration_.p2;
280 const int64_t p3 = this->calibration_.p3;
281 const int64_t p4 = this->calibration_.p4;
282 const int64_t p5 = this->calibration_.p5;
283 const int64_t p6 = this->calibration_.p6;
284 const int64_t p7 = this->calibration_.p7;
285 const int64_t p8 = this->calibration_.p8;
286 const int64_t p9 = this->calibration_.p9;
287
288 int64_t var1, var2, p;
289 var1 = int64_t(t_fine) - 128000;
290 var2 = var1 * var1 * p6;
291 var2 = var2 + ((var1 * p5) << 17);
292 var2 = var2 + (p4 << 35);
293 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
294 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
295
296 if (var1 == 0)
297 return NAN;
298
299 p = 1048576 - adc;
300 p = (((p << 31) - var2) * 3125) / var1;
301 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
302 var2 = (p8 * p) >> 19;
303
304 p = ((p + var1 + var2) >> 8) + (p7 << 4);
305 return (p / 256.0f) / 100.0f;
306}
307
308float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
309 uint16_t const raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
310 if (raw_adc == 0x8000)
311 return NAN;
312
313 int32_t const adc = raw_adc;
314
315 const int32_t h1 = this->calibration_.h1;
316 const int32_t h2 = this->calibration_.h2;
317 const int32_t h3 = this->calibration_.h3;
318 const int32_t h4 = this->calibration_.h4;
319 const int32_t h5 = this->calibration_.h5;
320 const int32_t h6 = this->calibration_.h6;
321
322 int32_t v_x1_u32r = t_fine - 76800;
323
324 v_x1_u32r = ((((adc << 14) - (h4 << 20) - (h5 * v_x1_u32r)) + 16384) >> 15) *
325 (((((((v_x1_u32r * h6) >> 10) * (((v_x1_u32r * h3) >> 11) + 32768)) >> 10) + 2097152) * h2 + 8192) >> 14);
326
327 v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
328
329 v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
330 v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
331 float const h = v_x1_u32r >> 12;
332
333 return h / 1024.0f;
334}
336 this->temperature_oversampling_ = temperature_over_sampling;
337}
339 this->pressure_oversampling_ = pressure_over_sampling;
340}
342 this->humidity_oversampling_ = humidity_over_sampling;
343}
344void BME280Component::set_iir_filter(BME280IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
345uint8_t BME280Component::read_u8_(uint8_t a_register) {
346 uint8_t data = 0;
347 this->read_byte(a_register, &data);
348 return data;
349}
350uint16_t BME280Component::read_u16_le_(uint8_t a_register) {
351 uint16_t data = 0;
352 this->read_byte_16(a_register, &data);
353 return (data >> 8) | (data << 8);
354}
355int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
356
357} // namespace esphome::bme280_base
uint8_t h
Definition bl0906.h:2
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void reset_to_construction_state()
Reset this component back to the construction state to allow setup to run again.
void status_clear_warning()
Definition component.h:289
uint8_t read_u8_(uint8_t a_register)
int16_t read_s16_le_(uint8_t a_register)
float read_humidity_(const uint8_t *data, int32_t t_fine)
Read the humidity value in % using the provided t_fine value.
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
virtual bool read_byte_16(uint8_t a_register, uint16_t *data)=0
BME280CalibrationData calibration_
Definition bme280_base.h:96
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
void set_iir_filter(BME280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
void set_humidity_oversampling(BME280Oversampling humidity_over_sampling)
Set the oversampling value for the humidity sensor. Default is 16x.
BME280Oversampling temperature_oversampling_
Definition bme280_base.h:97
void set_pressure_oversampling(BME280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
BME280Oversampling humidity_oversampling_
Definition bme280_base.h:99
float read_pressure_(const uint8_t *data, int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
enum esphome::bme280_base::BME280Component::ErrorCode NONE
void set_temperature_oversampling(BME280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
BME280Oversampling pressure_oversampling_
Definition bme280_base.h:98
uint16_t read_u16_le_(uint8_t a_register)
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
float read_temperature_(const uint8_t *data, int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
BME280Oversampling
Enum listing all Oversampling values for the BME280.
Definition bme280_base.h:37
const char * oversampling_to_str(BME280Oversampling oversampling)
const char * iir_filter_to_str(BME280IIRFilter filter)
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
uint8_t oversampling_to_time(BME280Oversampling over_sampling)
BME280IIRFilter
Enum listing all Infinite Impulse Filter values for the BME280.
Definition bme280_base.h:50
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7