ESPHome 2026.5.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
5namespace esphome {
6namespace ufire_ec {
7
8static const char *const TAG = "ufire_ec";
9
11 uint8_t version;
12 if (!this->read_byte(REGISTER_VERSION, &version) || version == 0xFF) {
13 this->mark_failed();
14 return;
15 }
16 ESP_LOGI(TAG, "Found ufire_ec board version 0x%02X", version);
17
18 // Write option for temperature adjustments
19 uint8_t config;
20 this->read_byte(REGISTER_CONFIG, &config);
21 if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
22 config &= ~CONFIG_TEMP_COMPENSATION;
23 } else {
24 config |= CONFIG_TEMP_COMPENSATION;
25 }
26 this->write_byte(REGISTER_CONFIG, config);
27
28 // Update temperature compensation
31}
32
34 int wait = 0;
35
36 if (this->temperature_sensor_ != nullptr) {
37 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
38 wait += 750;
39 } else if (this->temperature_sensor_external_ != nullptr) {
41 }
42
43 if (this->ec_sensor_ != nullptr) {
44 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_EC);
45 wait += 750;
46 }
47
48 if (wait > 0) {
49 this->set_timeout("data", wait, [this]() { this->update_internal_(); });
50 }
51}
52
54 if (this->temperature_sensor_ != nullptr)
56 if (this->ec_sensor_ != nullptr)
57 this->ec_sensor_->publish_state(this->measure_ms_());
58}
59
60float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
61
62float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
63
64bool UFireECComponent::set_solution_(float solution, float temperature) {
65 float denom = 1 - (this->temperature_coefficient_ * (temperature - 25));
66 if (std::abs(denom) < 1e-6f) {
67 ESP_LOGE(TAG, "Temperature compensation denominator is zero");
68 return false;
69 }
70 solution /= denom;
71 this->write_data_(REGISTER_SOLUTION, solution);
72 return true;
73}
74
75void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
76
77void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
78
80
82 if (!this->set_solution_(solution, temperature))
83 return;
84 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
85}
86
87void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
88
89float UFireECComponent::read_data_(uint8_t reg) {
90 float f;
91 uint8_t temp[4];
92
93 this->write(&reg, 1);
94 delay(10);
95
96 for (uint8_t i = 0; i < 4; i++) {
97 this->read_bytes_raw(temp + i, 1);
98 }
99 memcpy(&f, temp, sizeof(f));
100
101 return f;
102}
103
104void UFireECComponent::write_data_(uint8_t reg, float data) {
105 uint8_t temp[4];
106
107 memcpy(temp, &data, sizeof(data));
108 this->write_bytes(reg, temp, 4);
109 delay(10);
110}
111
113 ESP_LOGCONFIG(TAG,
114 "uFire-EC:\n"
115 " Temperature Compensation: %f\n"
116 " Temperature Coefficient: %f",
118 LOG_I2C_DEVICE(this)
119 LOG_UPDATE_INTERVAL(this);
120 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_);
121 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
122 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_);
123}
124
125} // namespace ufire_ec
126} // namespace esphome
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:510
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:454
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:75
void calibrate_probe(float solution, float temperature)
Definition ufire_ec.cpp:81
sensor::Sensor * temperature_sensor_external_
Definition ufire_ec.h:56
void set_temperature_(float temperature)
Definition ufire_ec.cpp:79
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:104
sensor::Sensor * temperature_sensor_
Definition ufire_ec.h:55
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:77
bool set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:64
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint16_t temperature
Definition sun_gtil2.cpp:12