ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ufire_ec.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "ufire_ec.h"
3#include <cmath>
4
6
7ESPHOME_LOG_TAG(TAG, "ufire_ec");
8
9static constexpr uint32_t DATA_TIMEOUT_ID = 0;
10
12 uint8_t version;
13 if (!this->read_byte(REGISTER_VERSION, &version) || version == 0xFF) {
14 this->mark_failed();
15 return;
16 }
17 ESP_LOGI(TAG, "Found ufire_ec board version 0x%02X", version);
18
19 // Write option for temperature adjustments
20 uint8_t config;
21 this->read_byte(REGISTER_CONFIG, &config);
22 if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
23 config &= ~CONFIG_TEMP_COMPENSATION;
24 } else {
25 config |= CONFIG_TEMP_COMPENSATION;
26 }
27 this->write_byte(REGISTER_CONFIG, config);
28
29 // Update temperature compensation
32}
33
35 int wait = 0;
36
37 if (this->temperature_sensor_ != nullptr) {
38 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
39 wait += 750;
40 } else if (this->temperature_sensor_external_ != nullptr) {
42 }
43
44 if (this->ec_sensor_ != nullptr) {
45 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_EC);
46 wait += 750;
47 }
48
49 if (wait > 0) {
50 this->set_timeout(DATA_TIMEOUT_ID, wait, [this]() { this->update_internal_(); });
51 }
52}
53
55 if (this->temperature_sensor_ != nullptr)
57 if (this->ec_sensor_ != nullptr)
58 this->ec_sensor_->publish_state(this->measure_ms_());
59}
60
61float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
62
63float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
64
65bool UFireECComponent::set_solution_(float solution, float temperature) {
66 float denom = 1 - (this->temperature_coefficient_ * (temperature - 25));
67 if (std::abs(denom) < 1e-6f) {
68 ESP_LOGE(TAG, "Temperature compensation denominator is zero");
69 return false;
70 }
71 solution /= denom;
72 this->write_data_(REGISTER_SOLUTION, solution);
73 return true;
74}
75
76void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
77
78void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
79
81
83 if (!this->set_solution_(solution, temperature))
84 return;
85 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
86}
87
88void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
89
90float UFireECComponent::read_data_(uint8_t reg) {
91 float f;
92 uint8_t temp[4];
93
94 this->write(&reg, 1);
95 delay(10);
96
97 for (uint8_t i = 0; i < 4; i++) {
98 this->read_bytes_raw(temp + i, 1);
99 }
100 memcpy(&f, temp, sizeof(f));
101
102 return f;
103}
104
105void UFireECComponent::write_data_(uint8_t reg, float data) {
106 uint8_t temp[4];
107
108 memcpy(temp, &data, sizeof(data));
109 this->write_bytes(reg, temp, 4);
110 delay(10);
111}
112
114 ESP_LOGCONFIG(TAG,
115 "uFire-EC:\n"
116 " Temperature Compensation: %f\n"
117 " Temperature Coefficient: %f",
119 LOG_I2C_DEVICE(this)
120 LOG_UPDATE_INTERVAL(this);
121 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_);
122 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
123 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_);
124}
125
126} // namespace esphome::ufire_ec
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
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
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:262
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
void set_compensation_(float temperature)
Definition ufire_ec.cpp:76
void calibrate_probe(float solution, float temperature)
Definition ufire_ec.cpp:82
sensor::Sensor * temperature_sensor_external_
Definition ufire_ec.h:54
void set_temperature_(float temperature)
Definition ufire_ec.cpp:80
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:105
sensor::Sensor * temperature_sensor_
Definition ufire_ec.h:53
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:78
bool set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:65
ESPHOME_LOG_TAG(TAG, "ufire_ec")
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12