ESPHome 2026.11.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
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 uint8_t config;
22 this->read_byte(REGISTER_CONFIG, &config);
23 if (this->temperature_sensor_ == nullptr && this->temperature_sensor_external_ == nullptr) {
24 config &= ~CONFIG_TEMP_COMPENSATION;
25 } else {
26 config |= CONFIG_TEMP_COMPENSATION;
27 }
28 this->write_byte(REGISTER_CONFIG, config);
29}
30
32 int wait = 0;
33 if (this->temperature_sensor_ != nullptr) {
34 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_TEMP);
35 wait += 750;
36 }
37 if (this->ph_sensor_ != nullptr) {
38 this->write_byte(REGISTER_TASK, COMMAND_MEASURE_MV);
39 wait += 750;
40 }
41
42 // Wait until measurement are taken
43 this->set_timeout(DATA_TIMEOUT_ID, wait, [this]() { this->update_internal_(); });
44}
45
47 float temperature = 0;
48
49 // Read temperature internal and populate it
50 if (this->temperature_sensor_ != nullptr) {
51 temperature = this->measure_temperature_();
52 this->temperature_sensor_->publish_state(temperature);
53 }
54 // Get temperature from external only for adjustments
55 else if (this->temperature_sensor_external_ != nullptr) {
56 temperature = this->temperature_sensor_external_->state;
57 }
58
59 if (this->ph_sensor_ != nullptr) {
60 this->ph_sensor_->publish_state(this->measure_ph_(temperature));
61 }
62}
63
64float UFireISEComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
65
66float UFireISEComponent::measure_mv_() { return this->read_data_(REGISTER_MV); }
67
69 float mv, ph;
70
71 mv = this->measure_mv_();
72 if (mv == -1)
73 return -1;
74
75 ph = fabsf(7.0f - (mv / PROBE_MV_TO_PH));
76
77 // Determine the temperature correction
78 float distance_from_7 = std::abs(7 - roundf(ph));
79 float distance_from_25 = std::floor(std::abs(25 - roundf(temperature)) / 10);
80 float temp_multiplier = (distance_from_25 * distance_from_7) * PROBE_TMP_CORRECTION;
81 if ((ph >= 8.0f) && (temperature >= 35))
82 temp_multiplier *= -1;
83 if ((ph <= 6.0f) && (temperature <= 15))
84 temp_multiplier *= -1;
85
86 ph += temp_multiplier;
87 if ((ph <= 0.0f) || (ph > 14.0f))
88 ph = -1;
89 if (std::isinf(ph))
90 ph = -1;
91 if (std::isnan(ph))
92 ph = -1;
93
94 return ph;
95}
96
98 solution = (7 - solution) * PROBE_MV_TO_PH;
99 this->write_data_(REGISTER_SOLUTION, solution);
100}
101
103 this->set_solution_(solution);
104 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_LOW);
105}
106
108 this->set_solution_(solution);
109 this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_HIGH);
110}
111
113 this->write_data_(REGISTER_REFHIGH, NAN);
114 this->write_data_(REGISTER_REFLOW, NAN);
115 this->write_data_(REGISTER_READHIGH, NAN);
116 this->write_data_(REGISTER_READLOW, NAN);
117}
118
120 float f;
121 uint8_t temp[4];
122
123 this->write(&reg, 1);
124 delay(10);
125
126 for (uint8_t i = 0; i < 4; i++) {
127 this->read_bytes_raw(temp + i, 1);
128 }
129 memcpy(&f, temp, sizeof(f));
130
131 return f;
132}
133
134void UFireISEComponent::write_data_(uint8_t reg, float data) {
135 uint8_t temp[4];
136
137 memcpy(temp, &data, sizeof(data));
138 this->write_bytes(reg, temp, 4);
139 delay(10);
140}
141
143 ESP_LOGCONFIG(TAG, "uFire-ISE");
144 LOG_I2C_DEVICE(this)
145 LOG_UPDATE_INTERVAL(this);
146 LOG_SENSOR(" ", "PH Sensor", this->ph_sensor_);
147 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
148 LOG_SENSOR(" ", "Temperature Sensor external", this->temperature_sensor_external_);
149}
150
151} // namespace esphome::ufire_ise
void mark_failed()
Mark this component as failed.
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
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
void write_data_(uint8_t reg, float data)
sensor::Sensor * temperature_sensor_external_
Definition ufire_ise.h:56
void calibrate_probe_low(float solution)
float measure_ph_(float temperature)
Definition ufire_ise.cpp:68
void calibrate_probe_high(float solution)
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