ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
bmp085.cpp
Go to the documentation of this file.
1#include "bmp085.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace bmp085 {
6
7static const char *const TAG = "bmp085.sensor";
8
9static const uint8_t BMP085_ADDRESS = 0x77;
10static const uint8_t BMP085_REGISTER_AC1_H = 0xAA;
11static const uint8_t BMP085_REGISTER_CONTROL = 0xF4;
12static const uint8_t BMP085_REGISTER_DATA_MSB = 0xF6;
13static const uint8_t BMP085_CONTROL_MODE_TEMPERATURE = 0x2E;
14static const uint8_t BMP085_CONTROL_MODE_PRESSURE_3 = 0xF4;
15
17 if (!this->set_mode_(BMP085_CONTROL_MODE_TEMPERATURE))
18 return;
19
20 this->set_timeout("temperature", 5, [this]() { this->read_temperature_(); });
21}
23 uint8_t data[22];
24 if (!this->read_bytes(BMP085_REGISTER_AC1_H, data, 22)) {
25 this->mark_failed();
26 return;
27 }
28
29 // Load calibration
30 this->calibration_.ac1 = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
31 this->calibration_.ac2 = ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
32 this->calibration_.ac3 = ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
33 this->calibration_.ac4 = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
34 this->calibration_.ac5 = ((data[8] & 0xFF) << 8) | (data[9] & 0xFF);
35 this->calibration_.ac6 = ((data[10] & 0xFF) << 8) | (data[11] & 0xFF);
36 this->calibration_.b1 = ((data[12] & 0xFF) << 8) | (data[13] & 0xFF);
37 this->calibration_.b2 = ((data[14] & 0xFF) << 8) | (data[15] & 0xFF);
38 this->calibration_.mb = ((data[16] & 0xFF) << 8) | (data[17] & 0xFF);
39 this->calibration_.mc = ((data[18] & 0xFF) << 8) | (data[19] & 0xFF);
40 this->calibration_.md = ((data[20] & 0xFF) << 8) | (data[21] & 0xFF);
41}
43 ESP_LOGCONFIG(TAG, "BMP085:");
44 LOG_I2C_DEVICE(this);
45 if (this->is_failed()) {
46 ESP_LOGE(TAG, "Connection with BMP085 failed!");
47 }
48 LOG_UPDATE_INTERVAL(this);
49
50 LOG_SENSOR(" ", "Temperature", this->temperature_);
51 LOG_SENSOR(" ", "Pressure", this->pressure_);
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 = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF);
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 if (this->temperature_ != nullptr)
80 this->temperature_->publish_state(temp);
82
83 if (!this->set_mode_(BMP085_CONTROL_MODE_PRESSURE_3)) {
84 this->status_set_warning();
85 return;
86 }
87
88 this->set_timeout("pressure", 26, [this]() { this->read_pressure_(); });
89}
91 uint8_t buffer[3];
92 if (!this->read_bytes(BMP085_REGISTER_DATA_MSB, buffer, 3)) {
93 this->status_set_warning();
94 return;
95 }
96
97 uint32_t value = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]) << 8) | uint32_t(buffer[2]);
98 if ((value >> 5) == 0) {
99 ESP_LOGW(TAG, "Invalid pressure!");
100 this->status_set_warning();
101 return;
102 }
103
104 double c3 = 160.0 * pow(2.0, -15.0) * this->calibration_.ac3;
105 double c4 = pow(10.0, -3) * pow(2.0, -15.0) * this->calibration_.ac4;
106 double b1 = pow(160.0, 2.0) * pow(2.0, -30.0) * this->calibration_.b1;
107 double x0 = this->calibration_.ac1;
108 double x1 = 160.0 * pow(2.0, -13.0) * this->calibration_.ac2;
109 double x2 = pow(160.0, 2.0) * pow(2.0, -25.0) * this->calibration_.b2;
110 double y0 = c4 * pow(2.0, 15.0);
111 double y1 = c4 * c3;
112 double y2 = c4 * b1;
113 double p0 = (3791.0 - 8.0) / 1600.0;
114 double p1 = 1.0 - 7357.0 * pow(2, -20);
115 double p2 = 3038.0 * 100.0 * pow(2, -36);
116
117 double p = value / 256.0;
118 double s = this->calibration_.temp - 25.0;
119 double x = (x2 * s * s) + (x1 * s) + x0;
120 double y = (y2 * s * s) + (y1 * s) + y0;
121 double z = (p - x) / y;
122 float pressure = (p2 * z * z) + (p1 * z) + p0;
123
124 ESP_LOGD(TAG, "Got Pressure=%.1f hPa", pressure);
125
126 if (this->pressure_ != nullptr)
127 this->pressure_->publish_state(pressure);
128 this->status_clear_warning();
129}
131 ESP_LOGV(TAG, "Setting mode to 0x%02X", mode);
132 return this->write_byte(BMP085_REGISTER_CONTROL, mode);
133}
135
136} // namespace bmp085
137} // namespace esphome
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
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.
CalibrationData calibration_
Definition bmp085.h:41
void setup() override
Setup the sensor and test for a connection.
Definition bmp085.cpp:22
void read_pressure_()
Internal method to read the pressure from the component after it has been scheduled.
Definition bmp085.cpp:90
void update() override
Schedule temperature+pressure readings.
Definition bmp085.cpp:16
float get_setup_priority() const override
Definition bmp085.cpp:134
sensor::Sensor * pressure_
Definition bmp085.h:40
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:130
sensor::Sensor * temperature_
Definition bmp085.h:39
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
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:216
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
bool z
Definition msa3xx.h:1
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6
uint8_t pressure
Definition tt21100.cpp:7