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