ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
honeywellabp2.cpp
Go to the documentation of this file.
1#include "honeywellabp2.h"
3#include "esphome/core/log.h"
4
6
7static const uint8_t STATUS_BIT_POWER = 6;
8static const uint8_t STATUS_BIT_BUSY = 5;
9static const uint8_t STATUS_BIT_ERROR = 2;
10static const uint8_t STATUS_MATH_SAT = 0;
11
12ESPHOME_LOG_TAG(TAG, "honeywellabp2");
13
14static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
15
16void HONEYWELLABP2Sensor::read_sensor_data() {
17 if (this->read(raw_data_, 7) != i2c::ERROR_OK) {
18 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
19 this->status_set_warning(LOG_STR("couldn't read sensor data"));
20 return;
21 }
22 float press_counts = encode_uint24(raw_data_[1], raw_data_[2], raw_data_[3]); // calculate digital pressure counts
23 float temp_counts = encode_uint24(raw_data_[4], raw_data_[5], raw_data_[6]); // calculate digital temperature counts
24
25 this->last_pressure_ = (((press_counts - this->min_count_) / (this->max_count_ - this->min_count_)) *
26 (this->max_pressure_ - this->min_pressure_)) +
27 this->min_pressure_;
28 this->last_temperature_ = (temp_counts * 200 / 16777215) - 50;
30}
31
32void HONEYWELLABP2Sensor::start_measurement() {
33 if (this->write(i2c_cmd_, 3) != i2c::ERROR_OK) {
34 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
35 this->status_set_warning(LOG_STR("couldn't start measurement"));
36 return;
37 }
38 this->measurement_running_ = true;
39}
40
41bool HONEYWELLABP2Sensor::is_measurement_ready() {
42 if (this->read(raw_data_, 1) != i2c::ERROR_OK) {
43 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
44 this->status_set_warning(LOG_STR("couldn't check measurement"));
45 return false;
46 }
47 if ((raw_data_[0] & (0x1 << STATUS_BIT_BUSY)) > 0) {
48 return false;
49 }
50 this->measurement_running_ = false;
51 return true;
52}
53
54void HONEYWELLABP2Sensor::measurement_timeout() {
55 ESP_LOGE(TAG, "Timeout!");
56 this->measurement_running_ = false;
57 this->status_set_warning(LOG_STR("measurement timed out"));
58}
59
60float HONEYWELLABP2Sensor::get_pressure() { return this->last_pressure_; }
61
62float HONEYWELLABP2Sensor::get_temperature() { return this->last_temperature_; }
63
65 if (this->measurement_running_) {
66 if (this->is_measurement_ready()) {
67 this->cancel_timeout(MEASUREMENT_TIMEOUT_ID);
68
69 this->read_sensor_data();
70 PUBLISH_STATE(this->pressure_sensor_, this->get_pressure());
71 PUBLISH_STATE(this->temperature_sensor_, this->get_temperature());
72 }
73 }
74}
75
76void HONEYWELLABP2Sensor::update() {
77 ESP_LOGV(TAG, "Update Honeywell ABP2 Sensor");
78
79 this->start_measurement();
80 this->set_timeout(MEASUREMENT_TIMEOUT_ID, 100, [this] { this->measurement_timeout(); });
81}
82
83void HONEYWELLABP2Sensor::dump_config() {
84 ESP_LOGCONFIG(TAG,
85 " Min Pressure Range: %0.1f\n"
86 " Max Pressure Range: %0.1f",
87 this->min_pressure_, this->max_pressure_);
89 ESP_LOGCONFIG(TAG, " Transfer function A");
90 } else {
91 ESP_LOGCONFIG(TAG, " Transfer function B");
92 }
93 LOG_UPDATE_INTERVAL(this);
94}
95
96void HONEYWELLABP2Sensor::set_transfer_function(ABP2TRANFERFUNCTION transfer_function) {
97 this->transfer_function_ = transfer_function;
99 this->max_count_ = this->max_count_b_;
100 this->min_count_ = this->min_count_b_;
101 } else {
102 this->max_count_ = this->max_count_a_;
103 this->min_count_ = this->min_count_a_;
104 }
105}
106
107} // namespace esphome::honeywellabp2_i2c
bool cancel_timeout(const char *name)
Cancel a timeout function.
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
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
void status_clear_warning()
Definition component.h:309
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
ESPHOME_LOG_TAG(TAG, "honeywellabp2")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
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
static void uint32_t