ESPHome 2026.6.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
7static const char *const TAG = "ufire_ec";
8
10 uint8_t version;
11 if (!this->read_byte(REGISTER_VERSION, &version) || version == 0xFF) {
12 this->mark_failed();
13 return;
14 }
15 ESP_LOGI(TAG, "Found ufire_ec board version 0x%02X", version);
16
17 // Write option for temperature adjustments
18 uint8_t config;
19 this->read_byte(REGISTER_CONFIG, &config);
20 if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
21 config &= ~CONFIG_TEMP_COMPENSATION;
22 } else {
23 config |= CONFIG_TEMP_COMPENSATION;
24 }
25 this->write_byte(REGISTER_CONFIG, config);
26
27 // Update temperature compensation
30}
31
33 int wait = 0;
34
35 if (this->temperature_sensor_ != nullptr) {
36 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
37 wait += 750;
38 } else if (this->temperature_sensor_external_ != nullptr) {
40 }
41
42 if (this->ec_sensor_ != nullptr) {
43 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_EC);
44 wait += 750;
45 }
46
47 if (wait > 0) {
48 this->set_timeout("data", wait, [this]() { this->update_internal_(); });
49 }
50}
51
53 if (this->temperature_sensor_ != nullptr)
55 if (this->ec_sensor_ != nullptr)
56 this->ec_sensor_->publish_state(this->measure_ms_());
57}
58
59float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
60
61float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
62
63bool UFireECComponent::set_solution_(float solution, float temperature) {
64 float denom = 1 - (this->temperature_coefficient_ * (temperature - 25));
65 if (std::abs(denom) < 1e-6f) {
66 ESP_LOGE(TAG, "Temperature compensation denominator is zero");
67 return false;
68 }
69 solution /= denom;
70 this->write_data_(REGISTER_SOLUTION, solution);
71 return true;
72}
73
74void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
75
76void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
77
79
81 if (!this->set_solution_(solution, temperature))
82 return;
83 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
84}
85
86void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
87
88float UFireECComponent::read_data_(uint8_t reg) {
89 float f;
90 uint8_t temp[4];
91
92 this->write(&reg, 1);
93 delay(10);
94
95 for (uint8_t i = 0; i < 4; i++) {
96 this->read_bytes_raw(temp + i, 1);
97 }
98 memcpy(&f, temp, sizeof(f));
99
100 return f;
101}
102
103void UFireECComponent::write_data_(uint8_t reg, float data) {
104 uint8_t temp[4];
105
106 memcpy(temp, &data, sizeof(data));
107 this->write_bytes(reg, temp, 4);
108 delay(10);
109}
110
112 ESP_LOGCONFIG(TAG,
113 "uFire-EC:\n"
114 " Temperature Compensation: %f\n"
115 " Temperature Coefficient: %f",
117 LOG_I2C_DEVICE(this)
118 LOG_UPDATE_INTERVAL(this);
119 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_);
120 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
121 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_);
122}
123
124} // namespace esphome::ufire_ec
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:437
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:230
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
void set_compensation_(float temperature)
Definition ufire_ec.cpp:74
void calibrate_probe(float solution, float temperature)
Definition ufire_ec.cpp:80
sensor::Sensor * temperature_sensor_external_
Definition ufire_ec.h:55
void set_temperature_(float temperature)
Definition ufire_ec.cpp:78
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:103
sensor::Sensor * temperature_sensor_
Definition ufire_ec.h:54
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:76
bool set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:63
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t temperature
Definition sun_gtil2.cpp:12