ESPHome  2024.10.2
kuntze.cpp
Go to the documentation of this file.
1 #include "kuntze.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace kuntze {
6 
7 static const char *const TAG = "kuntze";
8 
9 static const uint8_t CMD_READ_REG = 0x03;
10 static const uint16_t REGISTER[] = {4136, 4160, 4680, 6000, 4688, 4728, 5832};
11 
12 void Kuntze::on_modbus_data(const std::vector<uint8_t> &data) {
13  auto get_16bit = [&](int i) -> uint16_t { return (uint16_t(data[i * 2]) << 8) | uint16_t(data[i * 2 + 1]); };
14 
15  this->waiting_ = false;
16  ESP_LOGV(TAG, "Data: %s", hexencode(data).c_str());
17 
18  float value = (float) get_16bit(0);
19  for (int i = 0; i < data[3]; i++)
20  value /= 10.0;
21  switch (this->state_) {
22  case 1:
23  ESP_LOGD(TAG, "pH=%.1f", value);
24  if (this->ph_sensor_ != nullptr)
25  this->ph_sensor_->publish_state(value);
26  break;
27  case 2:
28  ESP_LOGD(TAG, "temperature=%.1f", value);
29  if (this->temperature_sensor_ != nullptr)
30  this->temperature_sensor_->publish_state(value);
31  break;
32  case 3:
33  ESP_LOGD(TAG, "DIS1=%.1f", value);
34  if (this->dis1_sensor_ != nullptr)
35  this->dis1_sensor_->publish_state(value);
36  break;
37  case 4:
38  ESP_LOGD(TAG, "DIS2=%.1f", value);
39  if (this->dis2_sensor_ != nullptr)
40  this->dis2_sensor_->publish_state(value);
41  break;
42  case 5:
43  ESP_LOGD(TAG, "REDOX=%.1f", value);
44  if (this->redox_sensor_ != nullptr)
45  this->redox_sensor_->publish_state(value);
46  break;
47  case 6:
48  ESP_LOGD(TAG, "EC=%.1f", value);
49  if (this->ec_sensor_ != nullptr)
50  this->ec_sensor_->publish_state(value);
51  break;
52  case 7:
53  ESP_LOGD(TAG, "OCI=%.1f", value);
54  if (this->oci_sensor_ != nullptr)
55  this->oci_sensor_->publish_state(value);
56  break;
57  }
58  if (++this->state_ > 7)
59  this->state_ = 0;
60 }
61 
62 void Kuntze::loop() {
63  uint32_t now = millis();
64  // timeout after 15 seconds
65  if (this->waiting_ && (now - this->last_send_ > 15000)) {
66  ESP_LOGW(TAG, "timed out waiting for response");
67  this->waiting_ = false;
68  }
69  if (this->waiting_ || (this->state_ == 0))
70  return;
71  this->last_send_ = now;
72  send(CMD_READ_REG, REGISTER[this->state_ - 1], 2);
73  this->waiting_ = true;
74 }
75 
76 void Kuntze::update() { this->state_ = 1; }
77 
79  ESP_LOGCONFIG(TAG, "Kuntze:");
80  ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
81  LOG_SENSOR("", "pH", this->ph_sensor_);
82  LOG_SENSOR("", "temperature", this->temperature_sensor_);
83  LOG_SENSOR("", "DIS1", this->dis1_sensor_);
84  LOG_SENSOR("", "DIS2", this->dis2_sensor_);
85  LOG_SENSOR("", "REDOX", this->redox_sensor_);
86  LOG_SENSOR("", "EC", this->ec_sensor_);
87  LOG_SENSOR("", "OCI", this->oci_sensor_);
88 }
89 
90 } // namespace kuntze
91 } // namespace esphome
sensor::Sensor * ec_sensor_
Definition: kuntze.h:37
void loop() override
Definition: kuntze.cpp:62
uint32_t last_send_
Definition: kuntze.h:30
sensor::Sensor * ph_sensor_
Definition: kuntze.h:32
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
sensor::Sensor * temperature_sensor_
Definition: kuntze.h:33
sensor::Sensor * redox_sensor_
Definition: kuntze.h:36
void dump_config() override
Definition: kuntze.cpp:78
void update() override
Definition: kuntze.cpp:76
sensor::Sensor * dis2_sensor_
Definition: kuntze.h:35
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void on_modbus_data(const std::vector< uint8_t > &data) override
Definition: kuntze.cpp:12
sensor::Sensor * dis1_sensor_
Definition: kuntze.h:34
sensor::Sensor * oci_sensor_
Definition: kuntze.h:38
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition: modbus.h:62