ESPHome  2024.10.2
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 namespace esphome {
11 namespace bme280_base {
12 
13 static const char *const TAG = "bme280.sensor";
14 
15 static const uint8_t BME280_REGISTER_DIG_T1 = 0x88;
16 static const uint8_t BME280_REGISTER_DIG_T2 = 0x8A;
17 static const uint8_t BME280_REGISTER_DIG_T3 = 0x8C;
18 
19 static const uint8_t BME280_REGISTER_DIG_P1 = 0x8E;
20 static const uint8_t BME280_REGISTER_DIG_P2 = 0x90;
21 static const uint8_t BME280_REGISTER_DIG_P3 = 0x92;
22 static const uint8_t BME280_REGISTER_DIG_P4 = 0x94;
23 static const uint8_t BME280_REGISTER_DIG_P5 = 0x96;
24 static const uint8_t BME280_REGISTER_DIG_P6 = 0x98;
25 static const uint8_t BME280_REGISTER_DIG_P7 = 0x9A;
26 static const uint8_t BME280_REGISTER_DIG_P8 = 0x9C;
27 static const uint8_t BME280_REGISTER_DIG_P9 = 0x9E;
28 
29 static const uint8_t BME280_REGISTER_DIG_H1 = 0xA1;
30 static const uint8_t BME280_REGISTER_DIG_H2 = 0xE1;
31 static const uint8_t BME280_REGISTER_DIG_H3 = 0xE3;
32 static const uint8_t BME280_REGISTER_DIG_H4 = 0xE4;
33 static const uint8_t BME280_REGISTER_DIG_H5 = 0xE5;
34 static const uint8_t BME280_REGISTER_DIG_H6 = 0xE7;
35 
36 static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
37 static const uint8_t BME280_REGISTER_RESET = 0xE0;
38 
39 static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2;
40 static const uint8_t BME280_REGISTER_STATUS = 0xF3;
41 static const uint8_t BME280_REGISTER_CONTROL = 0xF4;
42 static const uint8_t BME280_REGISTER_CONFIG = 0xF5;
43 static const uint8_t BME280_REGISTER_MEASUREMENTS = 0xF7;
44 static const uint8_t BME280_REGISTER_PRESSUREDATA = 0xF7;
45 static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA;
46 static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD;
47 
48 static const uint8_t BME280_MODE_FORCED = 0b01;
49 static const uint8_t BME280_SOFT_RESET = 0xB6;
50 static const uint8_t BME280_STATUS_IM_UPDATE = 0b01;
51 
52 inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
53 
54 const char *iir_filter_to_str(BME280IIRFilter filter) { // NOLINT
55  switch (filter) {
57  return "OFF";
59  return "2x";
61  return "4x";
63  return "8x";
65  return "16x";
66  default:
67  return "UNKNOWN";
68  }
69 }
70 
71 const char *oversampling_to_str(BME280Oversampling oversampling) { // NOLINT
72  switch (oversampling) {
74  return "None";
76  return "1x";
78  return "2x";
80  return "4x";
82  return "8x";
84  return "16x";
85  default:
86  return "UNKNOWN";
87  }
88 }
89 
91  ESP_LOGCONFIG(TAG, "Setting up BME280...");
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.
97  this->component_state_ &= ~COMPONENT_STATE_MASK;
99  }
100 
101  if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
102  this->error_code_ = COMMUNICATION_FAILED;
103  this->mark_failed();
104  return;
105  }
106  if (chip_id != 0x60) {
107  this->error_code_ = WRONG_CHIP_ID;
108  this->mark_failed();
109  return;
110  }
111 
112  // Send a soft reset.
113  if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
114  this->mark_failed();
115  return;
116  }
117  // Wait until the NVM data has finished loading.
118  uint8_t status;
119  uint8_t retry = 5;
120  do { // NOLINT
121  delay(2);
122  if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
123  ESP_LOGW(TAG, "Error reading status register.");
124  this->mark_failed();
125  return;
126  }
127  } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
128  if (status & BME280_STATUS_IM_UPDATE) {
129  ESP_LOGW(TAG, "Timeout loading NVM.");
130  this->mark_failed();
131  return;
132  }
133 
134  // Read calibration
135  this->calibration_.t1 = read_u16_le_(BME280_REGISTER_DIG_T1);
136  this->calibration_.t2 = read_s16_le_(BME280_REGISTER_DIG_T2);
137  this->calibration_.t3 = read_s16_le_(BME280_REGISTER_DIG_T3);
138 
139  this->calibration_.p1 = read_u16_le_(BME280_REGISTER_DIG_P1);
140  this->calibration_.p2 = read_s16_le_(BME280_REGISTER_DIG_P2);
141  this->calibration_.p3 = read_s16_le_(BME280_REGISTER_DIG_P3);
142  this->calibration_.p4 = read_s16_le_(BME280_REGISTER_DIG_P4);
143  this->calibration_.p5 = read_s16_le_(BME280_REGISTER_DIG_P5);
144  this->calibration_.p6 = read_s16_le_(BME280_REGISTER_DIG_P6);
145  this->calibration_.p7 = read_s16_le_(BME280_REGISTER_DIG_P7);
146  this->calibration_.p8 = read_s16_le_(BME280_REGISTER_DIG_P8);
147  this->calibration_.p9 = read_s16_le_(BME280_REGISTER_DIG_P9);
148 
149  this->calibration_.h1 = read_u8_(BME280_REGISTER_DIG_H1);
150  this->calibration_.h2 = read_s16_le_(BME280_REGISTER_DIG_H2);
151  this->calibration_.h3 = read_u8_(BME280_REGISTER_DIG_H3);
152  this->calibration_.h4 = read_u8_(BME280_REGISTER_DIG_H4) << 4 | (read_u8_(BME280_REGISTER_DIG_H4 + 1) & 0x0F);
153  this->calibration_.h5 = read_u8_(BME280_REGISTER_DIG_H5 + 1) << 4 | (read_u8_(BME280_REGISTER_DIG_H5) >> 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();
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();
165  return;
166  }
167 
168  uint8_t config_register = 0;
169  if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
170  this->mark_failed();
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();
178  return;
179  }
180 }
182  ESP_LOGCONFIG(TAG, "BME280:");
183  switch (this->error_code_) {
185  ESP_LOGE(TAG, "Communication with BME280 failed!");
186  break;
187  case WRONG_CHIP_ID:
188  ESP_LOGE(TAG, "BME280 has wrong chip ID! Is it a BME280?");
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 }
205 
206 inline uint8_t oversampling_to_time(BME280Oversampling over_sampling) { return (1 << uint8_t(over_sampling)) >> 1; }
207 
209  // Enable sensor
210  ESP_LOGV(TAG, "Sending conversion request...");
211  uint8_t meas_value = 0;
212  meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
213  meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
214  meas_value |= BME280_MODE_FORCED;
215  if (!this->write_byte(BME280_REGISTER_CONTROL, meas_value)) {
216  this->status_set_warning();
217  return;
218  }
219 
220  float meas_time = 1.5f;
221  meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
222  meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
223  meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
224 
225  this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
226  uint8_t data[8];
227  if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
228  ESP_LOGW(TAG, "Error reading registers.");
229  this->status_set_warning();
230  return;
231  }
232  int32_t t_fine = 0;
233  float const temperature = this->read_temperature_(data, &t_fine);
234  if (std::isnan(temperature)) {
235  ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values.");
236  this->status_set_warning();
237  return;
238  }
239  float const pressure = this->read_pressure_(data, t_fine);
240  float const humidity = this->read_humidity_(data, t_fine);
241 
242  ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
243  if (this->temperature_sensor_ != nullptr)
244  this->temperature_sensor_->publish_state(temperature);
245  if (this->pressure_sensor_ != nullptr)
246  this->pressure_sensor_->publish_state(pressure);
247  if (this->humidity_sensor_ != nullptr)
248  this->humidity_sensor_->publish_state(humidity);
249  this->status_clear_warning();
250  });
251 }
252 float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
253  int32_t adc = ((data[3] & 0xFF) << 16) | ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
254  adc >>= 4;
255  if (adc == 0x80000) {
256  // temperature was disabled
257  return NAN;
258  }
259 
260  const int32_t t1 = this->calibration_.t1;
261  const int32_t t2 = this->calibration_.t2;
262  const int32_t t3 = this->calibration_.t3;
263 
264  int32_t const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
265  int32_t const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
266  *t_fine = var1 + var2;
267 
268  float const temperature = (*t_fine * 5 + 128);
269  return temperature / 25600.0f;
270 }
271 
272 float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
273  int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
274  adc >>= 4;
275  if (adc == 0x80000) {
276  // pressure was disabled
277  return NAN;
278  }
279  const int64_t p1 = this->calibration_.p1;
280  const int64_t p2 = this->calibration_.p2;
281  const int64_t p3 = this->calibration_.p3;
282  const int64_t p4 = this->calibration_.p4;
283  const int64_t p5 = this->calibration_.p5;
284  const int64_t p6 = this->calibration_.p6;
285  const int64_t p7 = this->calibration_.p7;
286  const int64_t p8 = this->calibration_.p8;
287  const int64_t p9 = this->calibration_.p9;
288 
289  int64_t var1, var2, p;
290  var1 = int64_t(t_fine) - 128000;
291  var2 = var1 * var1 * p6;
292  var2 = var2 + ((var1 * p5) << 17);
293  var2 = var2 + (p4 << 35);
294  var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
295  var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
296 
297  if (var1 == 0)
298  return NAN;
299 
300  p = 1048576 - adc;
301  p = (((p << 31) - var2) * 3125) / var1;
302  var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
303  var2 = (p8 * p) >> 19;
304 
305  p = ((p + var1 + var2) >> 8) + (p7 << 4);
306  return (p / 256.0f) / 100.0f;
307 }
308 
309 float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
310  uint16_t const raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
311  if (raw_adc == 0x8000)
312  return NAN;
313 
314  int32_t const adc = raw_adc;
315 
316  const int32_t h1 = this->calibration_.h1;
317  const int32_t h2 = this->calibration_.h2;
318  const int32_t h3 = this->calibration_.h3;
319  const int32_t h4 = this->calibration_.h4;
320  const int32_t h5 = this->calibration_.h5;
321  const int32_t h6 = this->calibration_.h6;
322 
323  int32_t v_x1_u32r = t_fine - 76800;
324 
325  v_x1_u32r = ((((adc << 14) - (h4 << 20) - (h5 * v_x1_u32r)) + 16384) >> 15) *
326  (((((((v_x1_u32r * h6) >> 10) * (((v_x1_u32r * h3) >> 11) + 32768)) >> 10) + 2097152) * h2 + 8192) >> 14);
327 
328  v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
329 
330  v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
331  v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
332  float const h = v_x1_u32r >> 12;
333 
334  return h / 1024.0f;
335 }
337  this->temperature_oversampling_ = temperature_over_sampling;
338 }
340  this->pressure_oversampling_ = pressure_over_sampling;
341 }
343  this->humidity_oversampling_ = humidity_over_sampling;
344 }
345 void BME280Component::set_iir_filter(BME280IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
346 uint8_t BME280Component::read_u8_(uint8_t a_register) {
347  uint8_t data = 0;
348  this->read_byte(a_register, &data);
349  return data;
350 }
351 uint16_t BME280Component::read_u16_le_(uint8_t a_register) {
352  uint16_t data = 0;
353  this->read_byte_16(a_register, &data);
354  return (data >> 8) | (data << 8);
355 }
356 int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
357 
358 } // namespace bme280_base
359 } // namespace esphome
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:36
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
uint8_t pressure
Definition: tt21100.cpp:19
BME280Oversampling pressure_oversampling_
Definition: bme280_base.h:100
const char * iir_filter_to_str(BME280IIRFilter filter)
Definition: bme280_base.cpp:54
uint16_t read_u16_le_(uint8_t a_register)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
void set_temperature_oversampling(BME280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
virtual bool write_byte(uint8_t a_register, uint8_t data)=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 humidity_oversampling_
Definition: bme280_base.h:101
virtual bool read_byte_16(uint8_t a_register, uint16_t *data)=0
uint8_t h
Definition: bl0906.h:209
BME280IIRFilter
Enum listing all Infinite Impulse Filter values for the BME280.
Definition: bme280_base.h:51
int16_t read_s16_le_(uint8_t a_register)
uint32_t component_state_
State of this component.
Definition: component.h:272
BME280CalibrationData calibration_
Definition: bme280_base.h:98
void status_clear_warning()
Definition: component.cpp:166
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:33
float read_humidity_(const uint8_t *data, int32_t t_fine)
Read the humidity value in % using the provided t_fine value.
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
void set_humidity_oversampling(BME280Oversampling humidity_over_sampling)
Set the oversampling value for the humidity sensor. Default is 16x.
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:32
BME280Oversampling
Enum listing all Oversampling values for the BME280.
Definition: bme280_base.h:38
uint8_t status
Definition: bl0942.h:74
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void set_iir_filter(BME280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
BME280Oversampling temperature_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.
const char * oversampling_to_str(BME280Oversampling oversampling)
Definition: bme280_base.cpp:71
uint8_t read_u8_(uint8_t a_register)
uint8_t oversampling_to_time(BME280Oversampling over_sampling)
enum esphome::bme280_base::BME280Component::ErrorCode NONE
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
void set_pressure_oversampling(BME280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
Definition: bme280_base.cpp:52
float get_setup_priority() const override