ESPHome 2026.11.0b6
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"
11
13
14ESPHOME_LOG_TAG(TAG, "bme280.sensor");
15
16static constexpr uint32_t DATA_TIMEOUT_ID = 0;
17
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;
21
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;
31
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;
38
39static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
40static const uint8_t BME280_REGISTER_RESET = 0xE0;
41
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;
50
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;
54
55// Indexed by BME280IIRFilter
56PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "UNKNOWN");
57
58[[maybe_unused]] static const LogString *iir_filter_to_str(BME280IIRFilter value) {
59 return IIRFilterStrings::get_log_str(static_cast<uint8_t>(value), IIRFilterStrings::LAST_INDEX);
60}
61
62// Indexed by BME280Oversampling
63PROGMEM_STRING_TABLE(OversamplingStrings, "None", "1x", "2x", "4x", "8x", "16x", "UNKNOWN");
64
65[[maybe_unused]] static const LogString *oversampling_to_str(BME280Oversampling value) {
66 return OversamplingStrings::get_log_str(static_cast<uint8_t>(value), OversamplingStrings::LAST_INDEX);
67}
68
69void BME280Component::setup() {
70 uint8_t chip_id = 0;
71
72 // Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
73 // and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
74 if (this->is_failed()) {
76 }
77
78 if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
79 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
80 return;
81 }
82 if (chip_id != 0x60) {
83 this->mark_failed(LOG_STR("Wrong chip ID or no response"));
84 return;
85 }
86
87 // Send a soft reset.
88 if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
89 this->mark_failed(LOG_STR("Reset failed"));
90 return;
91 }
92 // Wait until the NVM data has finished loading.
93 uint8_t status;
94 uint8_t retry = 5;
95 do { // NOLINT
96 delay(2);
97 if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
98 this->mark_failed(LOG_STR("Error reading status register"));
99 return;
100 }
101 } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
102 if (status & BME280_STATUS_IM_UPDATE) {
103 this->mark_failed(LOG_STR("Timeout loading NVM"));
104 return;
105 }
106
107 // Read calibration
108 this->calibration_.t1 = read_u16_le_(BME280_REGISTER_DIG_T1);
109 this->calibration_.t2 = read_s16_le_(BME280_REGISTER_DIG_T2);
110 this->calibration_.t3 = read_s16_le_(BME280_REGISTER_DIG_T3);
111
112 this->calibration_.p1 = read_u16_le_(BME280_REGISTER_DIG_P1);
113 this->calibration_.p2 = read_s16_le_(BME280_REGISTER_DIG_P2);
114 this->calibration_.p3 = read_s16_le_(BME280_REGISTER_DIG_P3);
115 this->calibration_.p4 = read_s16_le_(BME280_REGISTER_DIG_P4);
116 this->calibration_.p5 = read_s16_le_(BME280_REGISTER_DIG_P5);
117 this->calibration_.p6 = read_s16_le_(BME280_REGISTER_DIG_P6);
118 this->calibration_.p7 = read_s16_le_(BME280_REGISTER_DIG_P7);
119 this->calibration_.p8 = read_s16_le_(BME280_REGISTER_DIG_P8);
120 this->calibration_.p9 = read_s16_le_(BME280_REGISTER_DIG_P9);
121
122 this->calibration_.h1 = read_u8_(BME280_REGISTER_DIG_H1);
123 this->calibration_.h2 = read_s16_le_(BME280_REGISTER_DIG_H2);
124 this->calibration_.h3 = read_u8_(BME280_REGISTER_DIG_H3);
125 this->calibration_.h4 =
126 sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H4) << 4 | (read_u8_(BME280_REGISTER_DIG_H4 + 1) & 0x0F));
127 this->calibration_.h5 =
128 sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H5 + 1) << 4 | (read_u8_(BME280_REGISTER_DIG_H5) >> 4));
129 this->calibration_.h6 = read_u8_(BME280_REGISTER_DIG_H6);
130
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"));
134 return;
135 }
136 humid_control_val &= ~0b00000111;
137 humid_control_val |= this->humidity_oversampling_ & 0b111;
138 if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
139 this->mark_failed(LOG_STR("Write humidity control"));
140 return;
141 }
142
143 uint8_t config_register = 0;
144 if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
145 this->mark_failed(LOG_STR("Read config"));
146 return;
147 }
148 config_register &= ~0b11111100;
149 config_register |= 0b101 << 5; // 1000 ms standby time
150 config_register |= (this->iir_filter_ & 0b111) << 2;
151 if (!this->write_byte(BME280_REGISTER_CONFIG, config_register)) {
152 this->mark_failed(LOG_STR("Write config"));
153 return;
154 }
155}
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);
160
161 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
162 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
163 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
164 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
165 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
166 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->humidity_oversampling_)));
167}
168
169inline uint8_t oversampling_to_time(BME280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
170
171void BME280Component::update() {
172 // Enable sensor
173 ESP_LOGV(TAG, "Sending conversion request");
174 if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, this->humidity_oversampling_ & 0b111)) {
175 this->status_set_warning();
176 return;
177 }
178 uint8_t meas_value = 0;
179 meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
180 meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
181 meas_value |= BME280_MODE_FORCED;
182 if (!this->write_byte(BME280_REGISTER_CONTROL, meas_value)) {
183 this->status_set_warning();
184 return;
185 }
186
187 float meas_time = 1.5f;
188 meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
189 meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
190 meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
191
192 this->set_timeout(DATA_TIMEOUT_ID, uint32_t(ceilf(meas_time)), [this]() {
193 uint8_t data[8];
194 if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
195 ESP_LOGW(TAG, "Error reading registers");
196 this->status_set_warning();
197 return;
198 }
199 int32_t t_fine = 0;
200 float const temperature = this->read_temperature_(data, &t_fine);
201 if (std::isnan(temperature)) {
202 ESP_LOGW(TAG, "Invalid temperature");
203 this->status_set_warning();
204 return;
205 }
206 float const pressure = this->read_pressure_(data, t_fine);
207 float const humidity = this->read_humidity_(data, t_fine);
208
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);
213 this->status_clear_warning();
214 });
215}
216float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
218 return NAN;
219 }
220 int32_t adc = encode_uint24(data[3], data[4], data[5]);
221 adc >>= 4;
222
223 const int32_t t1 = this->calibration_.t1;
224 const int32_t t2 = this->calibration_.t2;
225 const int32_t t3 = this->calibration_.t3;
226
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;
230
231 float const temperature = (*t_fine * 5 + 128);
232 return temperature / 25600.0f;
233}
234
235float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
237 return NAN;
238 }
239 int32_t adc = encode_uint24(data[0], data[1], data[2]);
240 adc >>= 4;
241 const int64_t p1 = this->calibration_.p1;
242 const int64_t p2 = this->calibration_.p2;
243 const int64_t p3 = this->calibration_.p3;
244 const int64_t p4 = this->calibration_.p4;
245 const int64_t p5 = this->calibration_.p5;
246 const int64_t p6 = this->calibration_.p6;
247 const int64_t p7 = this->calibration_.p7;
248 const int64_t p8 = this->calibration_.p8;
249 const int64_t p9 = this->calibration_.p9;
250
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;
258
259 if (var1 == 0)
260 return NAN;
261
262 p = 1048576 - adc;
263 p = (((p << 31) - var2) * 3125) / var1;
264 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
265 var2 = (p8 * p) >> 19;
266
267 p = ((p + var1 + var2) >> 8) + (p7 << 4);
268 return (p / 256.0f) / 100.0f;
269}
270
271float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
273 return NAN;
274 }
275 int32_t const adc = encode_uint16(data[6], data[7]);
276
277 const int32_t h1 = this->calibration_.h1;
278 const int32_t h2 = this->calibration_.h2;
279 const int32_t h3 = this->calibration_.h3;
280 const int32_t h4 = this->calibration_.h4;
281 const int32_t h5 = this->calibration_.h5;
282 const int32_t h6 = this->calibration_.h6;
283
284 int32_t v_x1_u32r = t_fine - 76800;
285
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);
288
289 v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
290
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;
294
295 return h / 1024.0f;
296}
297void BME280Component::set_temperature_oversampling(BME280Oversampling temperature_over_sampling) {
298 this->temperature_oversampling_ = temperature_over_sampling;
299}
300void BME280Component::set_pressure_oversampling(BME280Oversampling pressure_over_sampling) {
301 this->pressure_oversampling_ = pressure_over_sampling;
302}
303void BME280Component::set_humidity_oversampling(BME280Oversampling humidity_over_sampling) {
304 this->humidity_oversampling_ = humidity_over_sampling;
305}
306uint8_t BME280Component::read_u8_(uint8_t a_register) {
307 uint8_t data = 0;
308 this->read_byte(a_register, &data);
309 return data;
310}
311uint16_t BME280Component::read_u16_le_(uint8_t a_register) {
312 uint16_t data = 0;
313 this->read_byte_16_le(a_register, &data);
314 return data;
315}
316int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
317
318} // 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:287
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:309
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
BME280Oversampling temperature_oversampling_
Definition bme280_base.h:97
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.
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.
BME280Oversampling
Enum listing all Oversampling values for the BME280.
Definition bme280_base.h:37
PROGMEM_STRING_TABLE(IIRFilterStrings, "OFF", "2x", "4x", "8x", "16x", "UNKNOWN")
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")
std::span< const uint8_t > data
uint32_t value
Definition hal.cpp:41
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:1047
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:1057
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
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