5#define BMP280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
10static const char *
const TAG =
"bmp280.sensor";
12static const uint8_t BMP280_REGISTER_STATUS = 0xF3;
13static const uint8_t BMP280_REGISTER_CONTROL = 0xF4;
14static const uint8_t BMP280_REGISTER_CONFIG = 0xF5;
15static const uint8_t BMP280_REGISTER_PRESSUREDATA = 0xF7;
16static const uint8_t BMP280_REGISTER_TEMPDATA = 0xFA;
17static const uint8_t BMP280_REGISTER_RESET = 0xE0;
19static const uint8_t BMP280_MODE_FORCED = 0b01;
20static const uint8_t BMP280_SOFT_RESET = 0xB6;
21static const uint8_t BMP280_STATUS_IM_UPDATE = 0b01;
23inline uint16_t
combine_bytes(uint8_t msb, uint8_t lsb) {
return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
26 switch (oversampling) {
76 if (chip_id != 0x58) {
83 if (!this->
write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
96 }
while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
97 if (
status & BMP280_STATUS_IM_UPDATE) {
117 uint8_t config_register = 0;
118 if (!this->
read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
122 config_register &= ~0b11111100;
123 config_register |= 0b000 << 5;
124 config_register |= (this->
iir_filter_ & 0b111) << 2;
125 if (!this->
write_byte(BMP280_REGISTER_CONFIG, config_register)) {
131 ESP_LOGCONFIG(TAG,
"BMP280:");
132 switch (this->error_code_) {
134 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
137 ESP_LOGE(TAG, BMP280_ERROR_WRONG_CHIP_ID);
143 ESP_LOGCONFIG(TAG,
" IIR Filter: %s", iir_filter_to_str(this->
iir_filter_));
144 LOG_UPDATE_INTERVAL(
this);
157 ESP_LOGV(TAG,
"Sending conversion request");
158 uint8_t meas_value = 0;
162 if (!this->
write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
171 this->
set_timeout(
"data", uint32_t(ceilf(meas_time)), [
this]() {
175 ESP_LOGW(TAG,
"Invalid temperature");
192 if (!this->
read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
194 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
196 if (adc == 0x80000) {
205 int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
206 int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
207 *t_fine = var1 + var2;
215 if (!this->
read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
217 int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
219 if (adc == 0x80000) {
233 int64_t var1, var2, p;
234 var1 = int64_t(t_fine) - 128000;
235 var2 = var1 * var1 * p6;
236 var2 = var2 + ((var1 * p5) << 17);
237 var2 = var2 + (p4 << 35);
238 var1 = ((var1 * var1 * p3) >> 8) + ((var1 * p2) << 12);
239 var1 = ((int64_t(1) << 47) + var1) * p1 >> 33;
245 p = (((p << 31) - var2) * 3125) / var1;
246 var1 = (p9 * (p >> 13) * (p >> 13)) >> 25;
247 var2 = (p8 * p) >> 19;
249 p = ((p + var1 + var2) >> 8) + (p7 << 4);
250 return (p / 256.0f) / 100.0f;
267 return (data >> 8) | (data << 8);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
enum esphome::bmp280_base::BMP280Component::ErrorCode NONE
uint8_t read_u8_(uint8_t a_register)
uint16_t read_u16_le_(uint8_t a_register)
void dump_config() override
int16_t read_s16_le_(uint8_t a_register)
float read_pressure_(int32_t t_fine)
Read the pressure value in hPa using the provided t_fine value.
BMP280IIRFilter iir_filter_
sensor::Sensor * temperature_sensor_
BMP280Oversampling pressure_oversampling_
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
virtual bool read_byte_16(uint8_t a_register, uint16_t *data)=0
float read_temperature_(int32_t *t_fine)
Read the temperature value and store the calculated ambient temperature in t_fine.
void set_pressure_oversampling(BMP280Oversampling pressure_over_sampling)
Set the oversampling value for the pressure sensor. Default is 16x.
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
BMP280Oversampling temperature_oversampling_
void set_temperature_oversampling(BMP280Oversampling temperature_over_sampling)
Set the oversampling value for the temperature sensor. Default is 16x.
sensor::Sensor * pressure_sensor_
BMP280CalibrationData calibration_
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
void set_iir_filter(BMP280IIRFilter iir_filter)
Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
float get_setup_priority() const override
void publish_state(float state)
Publish a new state to the front-end.
const char * iir_filter_to_str(BME280IIRFilter filter)
uint16_t combine_bytes(uint8_t msb, uint8_t lsb)
BMP280IIRFilter
Enum listing all Infinite Impulse Filter values for the BMP280.
BMP280Oversampling
Enum listing all Oversampling values for the BMP280.
@ BMP280_OVERSAMPLING_16X
@ BMP280_OVERSAMPLING_NONE
uint8_t oversampling_to_time(BMP280Oversampling over_sampling)
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay(uint32_t ms)