ESPHome  2024.6.6
ufire_ec.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "ufire_ec.h"
3 
4 namespace esphome {
5 namespace ufire_ec {
6 
7 static 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 
61 float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTER_TEMP); }
62 
63 float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
64 
65 void UFireECComponent::set_solution_(float solution, float temperature) {
66  solution /= (1 - (this->temperature_coefficient_ * (temperature - 25)));
67  this->write_data_(REGISTER_SOLUTION, solution);
68 }
69 
70 void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
71 
72 void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(REGISTER_COEFFICENT, coefficient); }
73 
74 void UFireECComponent::set_temperature_(float temperature) { this->write_data_(REGISTER_TEMP, temperature); }
75 
76 void UFireECComponent::calibrate_probe(float solution, float temperature) {
77  this->set_solution_(solution, temperature);
78  this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
79 }
80 
81 void UFireECComponent::reset_board() { this->write_data_(REGISTER_CALIBRATE_OFFSET, NAN); }
82 
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 
98 void 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
sensor::Sensor * temperature_sensor_
Definition: ufire_ec.h:55
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
void calibrate_probe(float solution, float temperature)
Definition: ufire_ec.cpp:76
void write_data_(uint8_t reg, float data)
Definition: ufire_ec.cpp:98
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:225
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:69
void set_temperature_(float temperature)
Definition: ufire_ec.cpp:74
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:186
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void set_solution_(float solution, float temperature)
Definition: ufire_ec.cpp:65
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
void set_coefficient_(float coefficient)
Definition: ufire_ec.cpp:72
void set_compensation_(float temperature)
Definition: ufire_ec.cpp:70
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
sensor::Sensor * temperature_sensor_external_
Definition: ufire_ec.h:56
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248