ESPHome 2026.11.0b6
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 const bool compensate = this->temperature_sensor_ != nullptr || this->external_temperature_sensor_ != nullptr;
21 this->update_register(REGISTER_CONFIG, CONFIG_TEMP_COMPENSATION, compensate ? CONFIG_TEMP_COMPENSATION : 0);
22
23 // Update temperature compensation
26}
27
28void UFireECComponent::update() {
29 int wait = 0;
30
31 if (this->temperature_sensor_ != nullptr) {
32 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
33 wait += 750;
34 } else if (this->external_temperature_sensor_ != nullptr) {
35 this->set_temperature_(this->external_temperature_sensor_->state);
36 }
37
38 if (this->ec_sensor_ != nullptr) {
39 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_EC);
40 wait += 750;
41 }
42
43 if (wait > 0) {
44 this->set_timeout(DATA_TIMEOUT_ID, wait, [this]() { this->update_internal_(); });
45 }
46}
47
49 PUBLISH_STATE(this->temperature_sensor_, this->measure_temperature_());
50 PUBLISH_STATE(this->ec_sensor_, this->measure_ms_());
51}
52
53float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
54
55float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
56
57bool UFireECComponent::set_solution_(float solution, float temperature) {
58 float denom = 1 - (this->temperature_coefficient_ * (temperature - 25));
59 if (std::abs(denom) < 1e-6f) {
60 ESP_LOGE(TAG, "Temperature compensation denominator is zero");
61 return false;
62 }
63 solution /= denom;
64 this->write_data_(REGISTER_SOLUTION, solution);
65 return true;
66}
67
68void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
69
70void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
71
73
74void UFireECComponent::calibrate_probe(float solution, float temperature) {
75 if (!this->set_solution_(solution, temperature))
76 return;
77 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
78}
79
80void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
81
82float UFireECComponent::read_data_(uint8_t reg) {
83 float f;
84 uint8_t temp[4];
85
86 this->write(&reg, 1);
87 delay(10);
88
89 for (uint8_t i = 0; i < 4; i++) {
90 this->read_bytes_raw(temp + i, 1);
91 }
92 memcpy(&f, temp, sizeof(f));
93
94 return f;
95}
96
97void UFireECComponent::write_data_(uint8_t reg, float data) {
98 uint8_t temp[4];
99
100 memcpy(temp, &data, sizeof(data));
101 this->write_bytes(reg, temp, 4);
102 delay(10);
103}
104
105void UFireECComponent::dump_config() {
106 ESP_LOGCONFIG(TAG,
107 "uFire-EC:\n"
108 " Temperature Compensation: %f\n"
109 " Temperature Coefficient: %f",
111 LOG_I2C_DEVICE(this)
112 LOG_UPDATE_INTERVAL(this);
113 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_);
114 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
115 LOG_SENSOR(" ", "Temperature Sensor external", this->external_temperature_sensor_);
116}
117
118} // namespace esphome::ufire_ec
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
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
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
void set_compensation_(float temperature)
Definition ufire_ec.cpp:68
void set_temperature_(float temperature)
Definition ufire_ec.cpp:72
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:97
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:70
bool set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:57
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