ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bmp085.cpp
Go to the documentation of this file.
1#include "bmp085.h"
3#include "esphome/core/log.h"
4
5namespace esphome::bmp085 {
6
7ESPHOME_LOG_TAG(TAG, "bmp085.sensor");
8
9static constexpr uint32_t TEMPERATURE_TIMEOUT_ID = 0;
10static constexpr uint32_t PRESSURE_TIMEOUT_ID = 1;
11
12static const uint8_t BMP085_ADDRESS = 0x77;
13static const uint8_t BMP085_REGISTER_AC1_H = 0xAA;
14static const uint8_t BMP085_REGISTER_CONTROL = 0xF4;
15static const uint8_t BMP085_REGISTER_DATA_MSB = 0xF6;
16static const uint8_t BMP085_CONTROL_MODE_TEMPERATURE = 0x2E;
17static const uint8_t BMP085_CONTROL_MODE_PRESSURE_3 = 0xF4;
18
20 if (!this->set_mode_(BMP085_CONTROL_MODE_TEMPERATURE))
21 return;
22
23 this->set_timeout(TEMPERATURE_TIMEOUT_ID, 5, [this]() { this->read_temperature_(); });
24}
25void BMP085Component::setup() {
26 uint8_t data[22];
27 if (!this->read_bytes(BMP085_REGISTER_AC1_H, data, 22)) {
28 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
29 return;
30 }
31
32 // Load calibration
33 this->calibration_.ac1 = encode_uint16(data[0], data[1]);
34 this->calibration_.ac2 = encode_uint16(data[2], data[3]);
35 this->calibration_.ac3 = encode_uint16(data[4], data[5]);
36 this->calibration_.ac4 = encode_uint16(data[6], data[7]);
37 this->calibration_.ac5 = encode_uint16(data[8], data[9]);
38 this->calibration_.ac6 = encode_uint16(data[10], data[11]);
39 this->calibration_.b1 = encode_uint16(data[12], data[13]);
40 this->calibration_.b2 = encode_uint16(data[14], data[15]);
41 this->calibration_.mb = encode_uint16(data[16], data[17]);
42 this->calibration_.mc = encode_uint16(data[18], data[19]);
43 this->calibration_.md = encode_uint16(data[20], data[21]);
44}
45void BMP085Component::dump_config() {
46 ESP_LOGCONFIG(TAG, "BMP085:");
47 LOG_I2C_DEVICE(this);
48 LOG_UPDATE_INTERVAL(this);
49
50 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
51 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
52}
53
55 uint8_t buffer[2];
56 // 0xF6
57 if (!this->read_bytes(BMP085_REGISTER_DATA_MSB, buffer, 2)) {
58 this->status_set_warning();
59 return;
60 }
61
62 int32_t ut = encode_uint16(buffer[0], buffer[1]);
63 if (ut == 0) {
64 ESP_LOGW(TAG, "Invalid temperature!");
65 this->status_set_warning();
66 return;
67 }
68
69 double c5 = (pow(2, -15) / 160) * this->calibration_.ac5;
70 double c6 = this->calibration_.ac6;
71 double mc = (2048.0 / 25600.0) * this->calibration_.mc;
72 double md = this->calibration_.md / 160.0;
73
74 double a = c5 * (ut - c6);
75 float temp = a + (mc / (a + md));
76
77 this->calibration_.temp = temp;
78 ESP_LOGD(TAG, "Got Temperature=%.1f °C", temp);
79 PUBLISH_STATE(this->temperature_sensor_, temp);
81
82 if (!this->set_mode_(BMP085_CONTROL_MODE_PRESSURE_3)) {
83 this->status_set_warning();
84 return;
85 }
86
87 this->set_timeout(PRESSURE_TIMEOUT_ID, 26, [this]() { this->read_pressure_(); });
88}
90 uint8_t buffer[3];
91 if (!this->read_bytes(BMP085_REGISTER_DATA_MSB, buffer, 3)) {
92 this->status_set_warning();
93 return;
94 }
95
96 uint32_t value = encode_uint24(buffer[0], buffer[1], buffer[2]);
97 if ((value >> 5) == 0) {
98 ESP_LOGW(TAG, "Invalid pressure!");
99 this->status_set_warning();
100 return;
101 }
102
103 double c3 = 160.0 * pow(2.0, -15.0) * this->calibration_.ac3;
104 double c4 = pow(10.0, -3) * pow(2.0, -15.0) * this->calibration_.ac4;
105 double b1 = pow(160.0, 2.0) * pow(2.0, -30.0) * this->calibration_.b1;
106 double x0 = this->calibration_.ac1;
107 double x1 = 160.0 * pow(2.0, -13.0) * this->calibration_.ac2;
108 double x2 = pow(160.0, 2.0) * pow(2.0, -25.0) * this->calibration_.b2;
109 double y0 = c4 * pow(2.0, 15.0);
110 double y1 = c4 * c3;
111 double y2 = c4 * b1;
112 double p0 = (3791.0 - 8.0) / 1600.0;
113 double p1 = 1.0 - 7357.0 * pow(2, -20);
114 double p2 = 3038.0 * 100.0 * pow(2, -36);
115
116 double p = value / 256.0;
117 double s = this->calibration_.temp - 25.0;
118 double x = (x2 * s * s) + (x1 * s) + x0;
119 double y = (y2 * s * s) + (y1 * s) + y0;
120 double z = (p - x) / y;
121 float pressure = (p2 * z * z) + (p1 * z) + p0;
122
123 ESP_LOGD(TAG, "Got Pressure=%.1f hPa", pressure);
124
125 PUBLISH_STATE(this->pressure_sensor_, pressure);
126 this->status_clear_warning();
127}
129 ESP_LOGV(TAG, "Setting mode to 0x%02X", mode);
130 return this->write_byte(BMP085_REGISTER_CONTROL, mode);
131}
132
133} // namespace esphome::bmp085
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
virtual void update()=0
CalibrationData calibration_
Definition bmp085.h:36
void read_pressure_()
Internal method to read the pressure from the component after it has been scheduled.
Definition bmp085.cpp:89
void read_temperature_()
Internal method to read the temperature from the component after it has been scheduled.
Definition bmp085.cpp:54
bool set_mode_(uint8_t mode)
Definition bmp085.cpp:128
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
uint32_t buffer
Definition gif.cpp:50
bool z
Definition msa3xx.h:1
ESPHOME_LOG_TAG(TAG, "bmp085.sensor")
uint32_t value
Definition hal.cpp:41
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1047
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6
uint8_t pressure
Definition tt21100.cpp:7