6 namespace bmp280_base {
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...");
75 if (chip_id != 0x58) {
82 if (!this->
write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
91 if (!this->
read_byte(BMP280_REGISTER_STATUS, &status)) {
92 ESP_LOGW(TAG,
"Error reading status register.");
96 }
while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
97 if (status & BMP280_STATUS_IM_UPDATE) {
98 ESP_LOGW(TAG,
"Timeout loading NVM.");
118 uint8_t config_register = 0;
119 if (!this->
read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
123 config_register &= ~0b11111100;
124 config_register |= 0b000 << 5;
125 config_register |= (this->
iir_filter_ & 0b111) << 2;
126 if (!this->
write_byte(BMP280_REGISTER_CONFIG, config_register)) {
132 ESP_LOGCONFIG(TAG,
"BMP280:");
133 switch (this->error_code_) {
135 ESP_LOGE(TAG,
"Communication with BMP280 failed!");
138 ESP_LOGE(TAG,
"BMP280 has wrong chip ID! Is it a BME280?");
144 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", iir_filter_to_str(this->
iir_filter_));
145 LOG_UPDATE_INTERVAL(
this);
158 ESP_LOGV(TAG,
"Sending conversion request...");
159 uint8_t meas_value = 0;
163 if (!this->
write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
172 this->
set_timeout(
"data", uint32_t(ceilf(meas_time)), [
this]() {
175 if (std::isnan(temperature)) {
176 ESP_LOGW(TAG,
"Invalid temperature, cannot read pressure values.");
182 ESP_LOGD(TAG,
"Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
193 if (!this->
read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
195 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
197 if (adc == 0x80000) {
206 int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
207 int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
208 *t_fine = var1 + var2;
211 return temperature / 25600.0f;
216 if (!this->
read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
218 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
220 if (adc == 0x80000) {
234 int64_t var1, var2, p;
235 var1 = int64_t(t_fine) - 128000;
236 var2 = var1 * var1 * p6;
237 var2 = var2 + ((var1 * p5) << 17);
238 var2 = var2 + (p4 << 35);
239 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
240 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
246 p = (((p << 31) - var2) * 3125) / var1;
247 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
248 var2 = (p8 * p) >> 19;
250 p = ((p + var1 + var2) >> 8) + (p7 << 4);
251 return (p / 256.0f) / 100.0f;
268 return (data >> 8) | (data << 8);
BMP280Oversampling temperature_oversampling_
const float DATA
For components that import data from directly connected sensors like DHT.
void dump_config() override
sensor::Sensor * pressure_sensor_
virtual bool read_byte_16(uint8_t a_register, uint16_t *data)=0
void status_set_warning(const char *message="unspecified")
BMP280Oversampling
Enum listing all Oversampling values for the BMP280.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void set_pressure_oversampling(BMP280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
uint16_t read_u16_le_(uint8_t a_register)
enum esphome::bmp280_base::BMP280Component::ErrorCode NONE
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
BMP280CalibrationData calibration_
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
sensor::Sensor * temperature_sensor_
uint8_t read_u8_(uint8_t a_register)
float read_temperature_(int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
BMP280Oversampling pressure_oversampling_
BMP280IIRFilter
Enum listing all Infinite Impulse Filter values for the BMP280.
void status_clear_warning()
void set_temperature_oversampling(BMP280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
void publish_state(float state)
Publish a new state to the front-end.
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
uint8_t oversampling_to_time(BMP280Oversampling over_sampling)
int16_t read_s16_le_(uint8_t a_register)
BMP280IIRFilter iir_filter_
float get_setup_priority() const override
void set_iir_filter(BMP280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
float read_pressure_(int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
void IRAM_ATTR HOT delay(uint32_t ms)