ESPHome 2025.9.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
4namespace esphome {
5namespace ufire_ec {
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
63void UFireECComponent::set_solution_(float solution, float temperature) {
64 solution /= (1 - (this->temperature_coefficient_ * (temperature - 25)));
65 this->write_data_(REGISTER_SOLUTION, solution);
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
75 this->set_solution_(solution, temperature);
76 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
77}
78
79void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
80
81float UFireECComponent::read_data_(uint8_t reg) {
82 float f;
83 uint8_t temp[4];
84
85 this->write(&reg, 1);
86 delay(10);
87
88 for (uint8_t i = 0; i < 4; i++) {
89 this->read_bytes_raw(temp + i, 1);
90 }
91 memcpy(&f, temp, sizeof(f));
92
93 return f;
94}
95
96void UFireECComponent::write_data_(uint8_t reg, float data) {
97 uint8_t temp[4];
98
99 memcpy(temp, &data, sizeof(data));
100 this->write_bytes(reg, temp, 4);
101 delay(10);
102}
103
105 ESP_LOGCONFIG(TAG, "uFire-EC");
106 LOG_I2C_DEVICE(this)
107 LOG_UPDATE_INTERVAL(this)
108 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_)
109 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_)
110 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_)
111 ESP_LOGCONFIG(TAG,
112 " Temperature Compensation: %f\n"
113 " Temperature Coefficient: %f",
115}
116
117} // namespace ufire_ec
118} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:229
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:133
void set_compensation_(float temperature)
Definition ufire_ec.cpp:68
void calibrate_probe(float solution, float temperature)
Definition ufire_ec.cpp:74
sensor::Sensor * temperature_sensor_external_
Definition ufire_ec.h:56
void set_temperature_(float temperature)
Definition ufire_ec.cpp:72
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:96
void set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:63
sensor::Sensor * temperature_sensor_
Definition ufire_ec.h:55
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:70
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12