8 static const char *
const TAG =
"bmp280.sensor";
10 static const uint8_t BMP280_REGISTER_STATUS = 0xF3;
11 static const uint8_t BMP280_REGISTER_CONTROL = 0xF4;
12 static const uint8_t BMP280_REGISTER_CONFIG = 0xF5;
13 static const uint8_t BMP280_REGISTER_PRESSUREDATA = 0xF7;
14 static const uint8_t BMP280_REGISTER_TEMPDATA = 0xFA;
15 static const uint8_t BMP280_REGISTER_RESET = 0xE0;
17 static const uint8_t BMP280_MODE_FORCED = 0b01;
18 static const uint8_t BMP280_SOFT_RESET = 0xB6;
19 static const uint8_t BMP280_STATUS_IM_UPDATE = 0b01;
21 inline uint16_t
combine_bytes(uint8_t msb, uint8_t lsb) {
return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
24 switch (oversampling) {
60 ESP_LOGCONFIG(TAG,
"Setting up BMP280...");
67 if (chip_id != 0x58) {
74 if (!this->
write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
83 if (!this->
read_byte(BMP280_REGISTER_STATUS, &status)) {
84 ESP_LOGW(TAG,
"Error reading status register.");
88 }
while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
89 if (status & BMP280_STATUS_IM_UPDATE) {
90 ESP_LOGW(TAG,
"Timeout loading NVM.");
110 uint8_t config_register = 0;
111 if (!this->
read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
115 config_register &= ~0b11111100;
116 config_register |= 0b000 << 5;
117 config_register |= (this->
iir_filter_ & 0b111) << 2;
118 if (!this->
write_byte(BMP280_REGISTER_CONFIG, config_register)) {
124 ESP_LOGCONFIG(TAG,
"BMP280:");
125 LOG_I2C_DEVICE(
this);
126 switch (this->error_code_) {
128 ESP_LOGE(TAG,
"Communication with BMP280 failed!");
131 ESP_LOGE(TAG,
"BMP280 has wrong chip ID! Is it a BME280?");
137 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", iir_filter_to_str(this->
iir_filter_));
138 LOG_UPDATE_INTERVAL(
this);
151 ESP_LOGV(TAG,
"Sending conversion request...");
152 uint8_t meas_value = 0;
156 if (!this->
write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
165 this->
set_timeout(
"data", uint32_t(ceilf(meas_time)), [
this]() {
168 if (std::isnan(temperature)) {
169 ESP_LOGW(TAG,
"Invalid temperature, cannot read pressure values.");
175 ESP_LOGD(TAG,
"Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
186 if (!this->
read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
188 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
190 if (adc == 0x80000) {
199 int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
200 int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
201 *t_fine = var1 + var2;
204 return temperature / 100.0f;
209 if (!this->
read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
211 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
213 if (adc == 0x80000) {
227 int64_t var1, var2, p;
228 var1 = int64_t(t_fine) - 128000;
229 var2 = var1 * var1 * p6;
230 var2 = var2 + ((var1 * p5) << 17);
231 var2 = var2 + (p4 << 35);
232 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
233 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
239 p = (((p << 31) - var2) * 3125) / var1;
240 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
241 var2 = (p8 * p) >> 19;
243 p = ((p + var1 + var2) >> 8) + (p7 << 4);
244 return (p / 256.0f) / 100.0f;
261 return (data >> 8) | (data << 8);
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.
float read_temperature_(int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
void set_iir_filter(BMP280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
sensor::Sensor * pressure_sensor_
uint8_t read_u8_(uint8_t a_register)
void dump_config() override
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)
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
BMP280Oversampling pressure_oversampling_
float get_setup_priority() const override
void set_pressure_oversampling(BMP280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
int16_t read_s16_le_(uint8_t a_register)
void status_clear_warning()
sensor::Sensor * temperature_sensor_
void publish_state(float state)
Publish a new state to the front-end.
BMP280CalibrationData calibration_
uint16_t read_u16_le_(uint8_t a_register)
void status_set_warning()
enum esphome::bmp280::BMP280Component::ErrorCode NONE
BMP280IIRFilter
Enum listing all Infinite Impulse Filter values for the BMP280.
void set_temperature_oversampling(BMP280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
uint8_t oversampling_to_time(BMP280Oversampling over_sampling)
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
BMP280Oversampling
Enum listing all Oversampling values for the BMP280.
virtual void mark_failed()
Mark this component as failed.
float read_pressure_(int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
Implementation of SPI Controller mode.
BMP280IIRFilter iir_filter_
BMP280Oversampling temperature_oversampling_
void IRAM_ATTR HOT delay(uint32_t ms)