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