7 static const char *
const TAG =
"bme280.sensor";
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;
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;
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;
30 static const uint8_t BME280_REGISTER_CHIPID = 0xD0;
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;
41 static const uint8_t BME280_MODE_FORCED = 0b01;
43 inline uint16_t
combine_bytes(uint8_t msb, uint8_t lsb) {
return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
46 switch (oversampling) {
82 ESP_LOGCONFIG(TAG,
"Setting up BME280...");
89 if (!this->
read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
94 if (chip_id != 0x60) {
122 uint8_t humid_control_val = 0;
123 if (!this->
read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
127 humid_control_val &= ~0b00000111;
129 if (!this->
write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
134 uint8_t config_register = 0;
135 if (!this->
read_byte(BME280_REGISTER_CONFIG, &config_register)) {
139 config_register &= ~0b11111100;
140 config_register |= 0b000 << 5;
141 config_register |= (this->
iir_filter_ & 0b111) << 2;
142 if (!this->
write_byte(BME280_REGISTER_CONFIG, config_register)) {
148 ESP_LOGCONFIG(TAG,
"BME280:");
149 LOG_I2C_DEVICE(
this);
150 switch (this->error_code_) {
152 ESP_LOGE(TAG,
"Communication with BME280 failed!");
155 ESP_LOGE(TAG,
"BME280 has wrong chip ID! Is it a BME280?");
161 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", iir_filter_to_str(this->
iir_filter_));
162 LOG_UPDATE_INTERVAL(
this);
177 ESP_LOGV(TAG,
"Sending conversion request...");
178 uint8_t meas_value = 0;
181 meas_value |= BME280_MODE_FORCED;
182 if (!this->
write_byte(BME280_REGISTER_CONTROL, meas_value)) {
187 float meas_time = 1.5f;
192 this->
set_timeout(
"data", uint32_t(ceilf(meas_time)), [
this]() {
194 if (!this->
read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
195 ESP_LOGW(TAG,
"Error reading registers.");
201 if (std::isnan(temperature)) {
202 ESP_LOGW(TAG,
"Invalid temperature, cannot read pressure & humidity values.");
209 ESP_LOGV(TAG,
"Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
220 int32_t adc = ((data[3] & 0xFF) << 16) | ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
222 if (adc == 0x80000) {
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;
236 return temperature / 100.0f;
240 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
242 if (adc == 0x80000) {
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;
268 p = (((p << 31) - var2) * 3125) / var1;
269 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
270 var2 = (p8 * p) >> 19;
272 p = ((p + var1 + var2) >> 8) + (p7 << 4);
273 return (p / 256.0f) / 100.0f;
277 uint16_t raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
278 if (raw_adc == 0x8000)
281 int32_t adc = raw_adc;
290 int32_t v_x1_u32r = t_fine - 76800;
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);
295 v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * h1) >> 4);
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;
321 return (data >> 8) | (data << 8);
const uint32_t COMPONENT_STATE_FAILED
sensor::Sensor * pressure_sensor_
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
uint16_t read_u16_le_(uint8_t a_register)
float read_temperature_(const uint8_t *data, int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
sensor::Sensor * temperature_sensor_
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
float read_humidity_(const uint8_t *data, int32_t t_fine)
Read the humidity value in % using the provided t_fine value.
uint32_t component_state_
State of this component.
uint8_t read_u8_(uint8_t a_register)
enum esphome::bme280::BME280Component::ErrorCode NONE
BME280Oversampling humidity_oversampling_
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
uint8_t oversampling_to_time(BME280Oversampling over_sampling)
sensor::Sensor * humidity_sensor_
BME280Oversampling
Enum listing all Oversampling values for the BME280.
void status_set_warning()
BME280IIRFilter
Enum listing all Infinite Impulse Filter values for the BME280.
void set_iir_filter(BME280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
BME280IIRFilter iir_filter_
float get_setup_priority() const override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
void set_humidity_oversampling(BME280Oversampling humidity_over_sampling)
Set the oversampling value for the humidity sensor. Default is 16x.
float read_pressure_(const uint8_t *data, int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
BME280CalibrationData calibration_
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
int16_t read_s16_le_(uint8_t a_register)
BME280Oversampling pressure_oversampling_
void dump_config() override
void set_temperature_oversampling(BME280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
void set_pressure_oversampling(BME280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
BME280Oversampling temperature_oversampling_