ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ufire_ise.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "ufire_ise.h"
3
4#include <cmath>
5
7
8static const char *const TAG = "ufire_ise";
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_ise 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
30 int wait = 0;
31 if (this->temperature_sensor_ != nullptr) {
32 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
33 wait += 750;
34 }
35 if (this->ph_sensor_ != nullptr) {
36 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_MV);
37 wait += 750;
38 }
39
40 // Wait until measurement are taken
41 this->set_timeout("data", wait, [this]() { this->update_internal_(); });
42}
43
45 float temperature = 0;
46
47 // Read temperature internal and populate it
48 if (this->temperature_sensor_ != nullptr) {
49 temperature = this->measure_temperature_();
50 this->temperature_sensor_->publish_state(temperature);
51 }
52 // Get temperature from external only for adjustments
53 else if (this->temperature_sensor_external_ != nullptr) {
54 temperature = this->temperature_sensor_external_->state;
55 }
56
57 if (this->ph_sensor_ != nullptr) {
58 this->ph_sensor_->publish_state(this->measure_ph_(temperature));
59 }
60}
61
62float UFireISEComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
63
64float UFireISEComponent::measure_mv_() { return this->read_data_(REGISTER_MV); }
65
67 float mv, ph;
68
69 mv = this->measure_mv_();
70 if (mv == -1)
71 return -1;
72
73 ph = fabs(7.0 - (mv / PROBE_MV_TO_PH));
74
75 // Determine the temperature correction
76 float distance_from_7 = std::abs(7 - roundf(ph));
77 float distance_from_25 = std::floor(std::abs(25 - roundf(temperature)) / 10);
78 float temp_multiplier = (distance_from_25 * distance_from_7) * PROBE_TMP_CORRECTION;
79 if ((ph >= 8.0) && (temperature >= 35))
80 temp_multiplier *= -1;
81 if ((ph <= 6.0) && (temperature <= 15))
82 temp_multiplier *= -1;
83
84 ph += temp_multiplier;
85 if ((ph <= 0.0) || (ph > 14.0))
86 ph = -1;
87 if (std::isinf(ph))
88 ph = -1;
89 if (std::isnan(ph))
90 ph = -1;
91
92 return ph;
93}
94
96 solution = (7 - solution) * PROBE_MV_TO_PH;
97 this->write_data_(REGISTER_SOLUTION, solution);
98}
99
101 this->set_solution_(solution);
102 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_LOW);
103}
104
106 this->set_solution_(solution);
107 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_HIGH);
108}
109
111 this->write_data_(REGISTER_REFHIGH, NAN);
112 this->write_data_(REGISTER_REFLOW, NAN);
113 this->write_data_(REGISTER_READHIGH, NAN);
114 this->write_data_(REGISTER_READLOW, NAN);
115}
116
118 float f;
119 uint8_t temp[4];
120
121 this->write(&reg, 1);
122 delay(10);
123
124 for (uint8_t i = 0; i < 4; i++) {
125 this->read_bytes_raw(temp + i, 1);
126 }
127 memcpy(&f, temp, sizeof(f));
128
129 return f;
130}
131
132void UFireISEComponent::write_data_(uint8_t reg, float data) {
133 uint8_t temp[4];
134
135 memcpy(temp, &data, sizeof(data));
136 this->write_bytes(reg, temp, 4);
137 delay(10);
138}
139
141 ESP_LOGCONFIG(TAG, "uFire-ISE");
142 LOG_I2C_DEVICE(this)
143 LOG_UPDATE_INTERVAL(this);
144 LOG_SENSOR(" ", "PH Sensor", this->ph_sensor_);
145 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
146 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_);
147}
148
149} // namespace esphome::ufire_ise
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 write_data_(uint8_t reg, float data)
sensor::Sensor * temperature_sensor_external_
Definition ufire_ise.h:57
void calibrate_probe_low(float solution)
float measure_ph_(float temperature)
Definition ufire_ise.cpp:66
void calibrate_probe_high(float solution)
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t temperature
Definition sun_gtil2.cpp:12