ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up uFire_ec...");
11
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", 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
65void UFireECComponent::set_solution_(float solution, float temperature) {
66 solution /= (1 - (this->temperature_coefficient_ * (temperature - 25)));
67 this->write_data_(REGISTER_SOLUTION, solution);
68}
69
70void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
71
72void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
73
75
77 this->set_solution_(solution, temperature);
78 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
79}
80
81void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
82
83float UFireECComponent::read_data_(uint8_t reg) {
84 float f;
85 uint8_t temp[4];
86
87 this->write(&reg, 1);
88 delay(10);
89
90 for (uint8_t i = 0; i < 4; i++) {
91 this->read_bytes_raw(temp + i, 1);
92 }
93 memcpy(&f, temp, sizeof(f));
94
95 return f;
96}
97
98void UFireECComponent::write_data_(uint8_t reg, float data) {
99 uint8_t temp[4];
100
101 memcpy(temp, &data, sizeof(data));
102 this->write_bytes(reg, temp, 4);
103 delay(10);
104}
105
107 ESP_LOGCONFIG(TAG, "uFire-EC");
108 LOG_I2C_DEVICE(this)
109 LOG_UPDATE_INTERVAL(this)
110 LOG_SENSOR(" ", "EC Sensor", this->ec_sensor_)
111 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_)
112 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_)
113 ESP_LOGCONFIG(TAG, " Temperature Compensation: %f", this->temperature_compensation_);
114 ESP_LOGCONFIG(TAG, " Temperature Coefficient: %f", this->temperature_coefficient_);
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.
Definition component.cpp:72
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:39
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
void set_compensation_(float temperature)
Definition ufire_ec.cpp:70
void calibrate_probe(float solution, float temperature)
Definition ufire_ec.cpp:76
sensor::Sensor * temperature_sensor_external_
Definition ufire_ec.h:56
void set_temperature_(float temperature)
Definition ufire_ec.cpp:74
void write_data_(uint8_t reg, float data)
Definition ufire_ec.cpp:98
void set_solution_(float solution, float temperature)
Definition ufire_ec.cpp:65
sensor::Sensor * temperature_sensor_
Definition ufire_ec.h:55
void set_coefficient_(float coefficient)
Definition ufire_ec.cpp:72
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:28
uint16_t temperature
Definition sun_gtil2.cpp:12