ESPHome 2026.11.0b6
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
8ESPHOME_LOG_TAG(TAG, "ufire_ise");
9
10static constexpr uint32_t DATA_TIMEOUT_ID = 0;
11
13 uint8_t version;
14 if (!this->read_byte(REGISTER_VERSION, &version) || version == 0xFF) {
15 this->mark_failed();
16 return;
17 }
18 ESP_LOGI(TAG, "Found uFire_ise board version 0x%02X", version);
19
20 // Write option for temperature adjustments
21 const bool compensate = this->temperature_sensor_ != nullptr || this->external_temperature_sensor_ != nullptr;
22 this->update_register(REGISTER_CONFIG, CONFIG_TEMP_COMPENSATION, compensate ? CONFIG_TEMP_COMPENSATION : 0);
23}
24
25void UFireISEComponent::update() {
26 int wait = 0;
27 if (this->temperature_sensor_ != nullptr) {
28 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
29 wait += 750;
30 }
31 if (this->ph_sensor_ != nullptr) {
32 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_MV);
33 wait += 750;
34 }
35
36 // Wait until measurement are taken
37 this->set_timeout(DATA_TIMEOUT_ID, wait, [this]() { this->update_internal_(); });
38}
39
41 float temperature = 0;
42
43 // Read temperature internal and populate it
44 if (this->temperature_sensor_ != nullptr) {
46 this->temperature_sensor_->publish_state(temperature);
47 }
48 // Get temperature from external only for adjustments
49 else if (this->external_temperature_sensor_ != nullptr) {
50 temperature = this->external_temperature_sensor_->state;
51 }
52
53 PUBLISH_STATE(this->ph_sensor_, this->measure_ph_(temperature));
54}
55
56float UFireISEComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
57
58float UFireISEComponent::measure_mv_() { return this->read_data_(REGISTER_MV); }
59
61 float mv, ph;
62
63 mv = this->measure_mv_();
64 if (mv == -1)
65 return -1;
66
67 ph = fabsf(7.0f - (mv / PROBE_MV_TO_PH));
68
69 // Determine the temperature correction
70 float distance_from_7 = std::abs(7 - roundf(ph));
71 float distance_from_25 = std::floor(std::abs(25 - roundf(temperature)) / 10);
72 float temp_multiplier = (distance_from_25 * distance_from_7) * PROBE_TMP_CORRECTION;
73 if ((ph >= 8.0f) && (temperature >= 35))
74 temp_multiplier *= -1;
75 if ((ph <= 6.0f) && (temperature <= 15))
76 temp_multiplier *= -1;
77
78 ph += temp_multiplier;
79 if ((ph <= 0.0f) || (ph > 14.0f))
80 ph = -1;
81 if (std::isinf(ph))
82 ph = -1;
83 if (std::isnan(ph))
84 ph = -1;
85
86 return ph;
87}
88
90 solution = (7 - solution) * PROBE_MV_TO_PH;
91 this->write_data_(REGISTER_SOLUTION, solution);
92}
93
94void UFireISEComponent::calibrate_probe_low(float solution) {
95 this->set_solution_(solution);
96 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_LOW);
97}
98
99void UFireISEComponent::calibrate_probe_high(float solution) {
100 this->set_solution_(solution);
101 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_HIGH);
102}
103
104void UFireISEComponent::reset_board() {
105 this->write_data_(REGISTER_REFHIGH, NAN);
106 this->write_data_(REGISTER_REFLOW, NAN);
107 this->write_data_(REGISTER_READHIGH, NAN);
108 this->write_data_(REGISTER_READLOW, NAN);
109}
110
112 float f;
113 uint8_t temp[4];
114
115 this->write(&reg, 1);
116 delay(10);
117
118 for (uint8_t i = 0; i < 4; i++) {
119 this->read_bytes_raw(temp + i, 1);
120 }
121 memcpy(&f, temp, sizeof(f));
122
123 return f;
124}
125
126void UFireISEComponent::write_data_(uint8_t reg, float data) {
127 uint8_t temp[4];
128
129 memcpy(temp, &data, sizeof(data));
130 this->write_bytes(reg, temp, 4);
131 delay(10);
132}
133
134void UFireISEComponent::dump_config() {
135 ESP_LOGCONFIG(TAG, "uFire-ISE");
136 LOG_I2C_DEVICE(this)
137 LOG_UPDATE_INTERVAL(this);
138 LOG_SENSOR(" ", "PH Sensor", this->ph_sensor_);
139 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
140 LOG_SENSOR(" ", "Temperature Sensor external", this->external_temperature_sensor_);
141}
142
143} // namespace esphome::ufire_ise
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 write_data_(uint8_t reg, float data)
float measure_ph_(float temperature)
Definition ufire_ise.cpp:60
ESPHOME_LOG_TAG(TAG, "ufire_ise")
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12