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