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