ESPHome 2026.11.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"
7#include "esphome/core/log.h"
10
12
13ESPHOME_LOG_TAG(TAG, "bme280.sensor");
14
15static constexpr uint32_t DATA_TIMEOUT_ID = 0;
16
17static const uint8_t BME280_REGISTER_DIG_T1 = 0x88;
18static const uint8_t BME280_REGISTER_DIG_T2 = 0x8A;
19static const uint8_t BME280_REGISTER_DIG_T3 = 0x8C;
20
21static const uint8_t BME280_REGISTER_DIG_P1 = 0x8E;
22static const uint8_t BME280_REGISTER_DIG_P2 = 0x90;
23static const uint8_t BME280_REGISTER_DIG_P3 = 0x92;
24static const uint8_t BME280_REGISTER_DIG_P4 = 0x94;
25static const uint8_t BME280_REGISTER_DIG_P5 = 0x96;
26static const uint8_t BME280_REGISTER_DIG_P6 = 0x98;
27static const uint8_t BME280_REGISTER_DIG_P7 = 0x9A;
28static const uint8_t BME280_REGISTER_DIG_P8 = 0x9C;
29static const uint8_t BME280_REGISTER_DIG_P9 = 0x9E;
30
31static const uint8_t BME280_REGISTER_DIG_H1 = 0xA1;
32static const uint8_t BME280_REGISTER_DIG_H2 = 0xE1;
33static const uint8_t BME280_REGISTER_DIG_H3 = 0xE3;
34static const uint8_t BME280_REGISTER_DIG_H4 = 0xE4;
35static const uint8_t BME280_REGISTER_DIG_H5 = 0xE5;
36static const uint8_t BME280_REGISTER_DIG_H6 = 0xE7;
37
38static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
39static const uint8_t BME280_REGISTER_RESET = 0xE0;
40
41static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2;
42static const uint8_t BME280_REGISTER_STATUS = 0xF3;
43static const uint8_t BME280_REGISTER_CONTROL = 0xF4;
44static const uint8_t BME280_REGISTER_CONFIG = 0xF5;
45static const uint8_t BME280_REGISTER_MEASUREMENTS = 0xF7;
46static const uint8_t BME280_REGISTER_PRESSUREDATA = 0xF7;
47static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA;
48static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD;
49
50static const uint8_t BME280_MODE_FORCED = 0b01;
51static const uint8_t BME280_SOFT_RESET = 0xB6;
52static const uint8_t BME280_STATUS_IM_UPDATE = 0b01;
53
54inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
55
56const char *iir_filter_to_str(BME280IIRFilter filter) { // NOLINT
57 switch (filter) {
59 return "OFF";
61 return "2x";
63 return "4x";
65 return "8x";
67 return "16x";
68 default:
69 return "UNKNOWN";
70 }
71}
72
73const char *oversampling_to_str(BME280Oversampling oversampling) { // NOLINT
74 switch (oversampling) {
76 return "None";
78 return "1x";
80 return "2x";
82 return "4x";
84 return "8x";
86 return "16x";
87 default:
88 return "UNKNOWN";
89 }
90}
91
93 uint8_t chip_id = 0;
94
95 // Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
96 // and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
97 if (this->is_failed()) {
99 }
100
101 if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
102 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
103 return;
104 }
105 if (chip_id != 0x60) {
106 this->mark_failed(LOG_STR("Wrong chip ID or no response"));
107 return;
108 }
109
110 // Send a soft reset.
111 if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
112 this->mark_failed(LOG_STR("Reset failed"));
113 return;
114 }
115 // Wait until the NVM data has finished loading.
116 uint8_t status;
117 uint8_t retry = 5;
118 do { // NOLINT
119 delay(2);
120 if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
121 this->mark_failed(LOG_STR("Error reading status register"));
122 return;
123 }
124 } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
125 if (status & BME280_STATUS_IM_UPDATE) {
126 this->mark_failed(LOG_STR("Timeout loading NVM"));
127 return;
128 }
129
130 // Read calibration
131 this->calibration_.t1 = read_u16_le_(BME280_REGISTER_DIG_T1);
132 this->calibration_.t2 = read_s16_le_(BME280_REGISTER_DIG_T2);
133 this->calibration_.t3 = read_s16_le_(BME280_REGISTER_DIG_T3);
134
135 this->calibration_.p1 = read_u16_le_(BME280_REGISTER_DIG_P1);
136 this->calibration_.p2 = read_s16_le_(BME280_REGISTER_DIG_P2);
137 this->calibration_.p3 = read_s16_le_(BME280_REGISTER_DIG_P3);
138 this->calibration_.p4 = read_s16_le_(BME280_REGISTER_DIG_P4);
139 this->calibration_.p5 = read_s16_le_(BME280_REGISTER_DIG_P5);
140 this->calibration_.p6 = read_s16_le_(BME280_REGISTER_DIG_P6);
141 this->calibration_.p7 = read_s16_le_(BME280_REGISTER_DIG_P7);
142 this->calibration_.p8 = read_s16_le_(BME280_REGISTER_DIG_P8);
143 this->calibration_.p9 = read_s16_le_(BME280_REGISTER_DIG_P9);
144
145 this->calibration_.h1 = read_u8_(BME280_REGISTER_DIG_H1);
146 this->calibration_.h2 = read_s16_le_(BME280_REGISTER_DIG_H2);
147 this->calibration_.h3 = read_u8_(BME280_REGISTER_DIG_H3);
148 this->calibration_.h4 =
149 sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H4) << 4 | (read_u8_(BME280_REGISTER_DIG_H4 + 1) & 0x0F));
150 this->calibration_.h5 =
151 sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H5 + 1) << 4 | (read_u8_(BME280_REGISTER_DIG_H5) >> 4));
152 this->calibration_.h6 = read_u8_(BME280_REGISTER_DIG_H6);
153
154 uint8_t humid_control_val = 0;
155 if (!this->read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
156 this->mark_failed(LOG_STR("Read humidity control"));
157 return;
158 }
159 humid_control_val &= ~0b00000111;
160 humid_control_val |= this->humidity_oversampling_ & 0b111;
161 if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
162 this->mark_failed(LOG_STR("Write humidity control"));
163 return;
164 }
165
166 uint8_t config_register = 0;
167 if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
168 this->mark_failed(LOG_STR("Read config"));
169 return;
170 }
171 config_register &= ~0b11111100;
172 config_register |= 0b101 << 5; // 1000 ms standby time
173 config_register |= (this->iir_filter_ & 0b111) << 2;
174 if (!this->write_byte(BME280_REGISTER_CONFIG, config_register)) {
175 this->mark_failed(LOG_STR("Write config"));
176 return;
177 }
178}
180 ESP_LOGCONFIG(TAG, "BME280:");
181 ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_));
182 LOG_UPDATE_INTERVAL(this);
183
184 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
185 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_));
186 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
187 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_));
188 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
189 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->humidity_oversampling_));
190}
191
192inline uint8_t oversampling_to_time(BME280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
193
195 // Enable sensor
196 ESP_LOGV(TAG, "Sending conversion request");
197 uint8_t meas_value = 0;
198 meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
199 meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
200 meas_value |= BME280_MODE_FORCED;
201 if (!this->write_byte(BME280_REGISTER_CONTROL, meas_value)) {
202 this->status_set_warning();
203 return;
204 }
205
206 float meas_time = 1.5f;
207 meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
208 meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
209 meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
210
211 this->set_timeout(DATA_TIMEOUT_ID, uint32_t(ceilf(meas_time)), [this]() {
212 uint8_t data[8];
213 if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
214 ESP_LOGW(TAG, "Error reading registers");
215 this->status_set_warning();
216 return;
217 }
218 int32_t t_fine = 0;
219 float const temperature = this->read_temperature_(data, &t_fine);
220 if (std::isnan(temperature)) {
221 ESP_LOGW(TAG, "Invalid temperature");
222 this->status_set_warning();
223 return;
224 }
225 float const pressure = this->read_pressure_(data, t_fine);
226 float const humidity = this->read_humidity_(data, t_fine);
227
228 ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa Humidity=%.1f%%", temperature, pressure, humidity);
229 if (this->temperature_sensor_ != nullptr)
230 this->temperature_sensor_->publish_state(temperature);
231 if (this->pressure_sensor_ != nullptr)
232 this->pressure_sensor_->publish_state(pressure);
233 if (this->humidity_sensor_ != nullptr)
234 this->humidity_sensor_->publish_state(humidity);
235 this->status_clear_warning();
236 });
237}
238float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
239 int32_t adc = encode_uint24(data[3], data[4], data[5]);
240 adc >>= 4;
241 if (adc == 0x80000) {
242 // temperature was disabled
243 return NAN;
244 }
245
246 const int32_t t1 = this->calibration_.t1;
247 const int32_t t2 = this->calibration_.t2;
248 const int32_t t3 = this->calibration_.t3;
249
250 int32_t const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
251 int32_t const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
252 *t_fine = var1 + var2;
253
254 float const temperature = (*t_fine * 5 + 128);
255 return temperature / 25600.0f;
256}
257
258float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
259 int32_t adc = encode_uint24(data[0], data[1], data[2]);
260 adc >>= 4;
261 if (adc == 0x80000) {
262 // pressure was disabled
263 return NAN;
264 }
265 const int64_t p1 = this->calibration_.p1;
266 const int64_t p2 = this->calibration_.p2;
267 const int64_t p3 = this->calibration_.p3;
268 const int64_t p4 = this->calibration_.p4;
269 const int64_t p5 = this->calibration_.p5;
270 const int64_t p6 = this->calibration_.p6;
271 const int64_t p7 = this->calibration_.p7;
272 const int64_t p8 = this->calibration_.p8;
273 const int64_t p9 = this->calibration_.p9;
274
275 int64_t var1, var2, p;
276 var1 = int64_t(t_fine) - 128000;
277 var2 = var1 * var1 * p6;
278 var2 = var2 + ((var1 * p5) << 17);
279 var2 = var2 + (p4 << 35);
280 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
281 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
282
283 if (var1 == 0)
284 return NAN;
285
286 p = 1048576 - adc;
287 p = (((p << 31) - var2) * 3125) / var1;
288 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
289 var2 = (p8 * p) >> 19;
290
291 p = ((p + var1 + var2) >> 8) + (p7 << 4);
292 return (p / 256.0f) / 100.0f;
293}
294
295float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
296 uint16_t const raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
297 if (raw_adc == 0x8000)
298 return NAN;
299
300 int32_t const adc = raw_adc;
301
302 const int32_t h1 = this->calibration_.h1;
303 const int32_t h2 = this->calibration_.h2;
304 const int32_t h3 = this->calibration_.h3;
305 const int32_t h4 = this->calibration_.h4;
306 const int32_t h5 = this->calibration_.h5;
307 const int32_t h6 = this->calibration_.h6;
308
309 int32_t v_x1_u32r = t_fine - 76800;
310
311 v_x1_u32r = ((((adc << 14) - (h4 << 20) - (h5 * v_x1_u32r)) + 16384) >> 15) *
312 (((((((v_x1_u32r * h6) >> 10) * (((v_x1_u32r * h3) >> 11) + 32768)) >> 10) + 2097152) * h2 + 8192) >> 14);
313
314 v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
315
316 v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
317 v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
318 float const h = v_x1_u32r >> 12;
319
320 return h / 1024.0f;
321}
323 this->temperature_oversampling_ = temperature_over_sampling;
324}
326 this->pressure_oversampling_ = pressure_over_sampling;
327}
329 this->humidity_oversampling_ = humidity_over_sampling;
330}
331uint8_t BME280Component::read_u8_(uint8_t a_register) {
332 uint8_t data = 0;
333 this->read_byte(a_register, &data);
334 return data;
335}
336uint16_t BME280Component::read_u16_le_(uint8_t a_register) {
337 uint16_t data = 0;
338 this->read_byte_16_le(a_register, &data);
339 return data;
340}
341int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
342
343} // 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:274
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
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:296
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_le(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_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.
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:65
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
ESPHOME_LOG_TAG(TAG, "bme280.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.
Definition helpers.h:1002
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
Definition helpers.h:1012
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