ESPHome 2026.1.5
Loading...
Searching...
No Matches
kuntze.cpp
Go to the documentation of this file.
1#include "kuntze.h"
3#include "esphome/core/log.h"
5
6namespace esphome {
7namespace kuntze {
8
9static const char *const TAG = "kuntze";
10
11static const uint8_t CMD_READ_REG = 0x03;
12static const uint16_t REGISTER[] = {4136, 4160, 4680, 6000, 4688, 4728, 5832};
13
14// Maximum bytes to log for Modbus responses (2 registers = 4, plus count = 5)
15static constexpr size_t KUNTZE_MAX_LOG_BYTES = 8;
16
17void Kuntze::on_modbus_data(const std::vector<uint8_t> &data) {
18 auto get_16bit = [&](int i) -> uint16_t { return (uint16_t(data[i * 2]) << 8) | uint16_t(data[i * 2 + 1]); };
19
20 this->waiting_ = false;
21#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
22 char hex_buf[format_hex_pretty_size(KUNTZE_MAX_LOG_BYTES)];
23#endif
24 ESP_LOGV(TAG, "Data: %s", format_hex_pretty_to(hex_buf, data.data(), data.size()));
25
26 float value = (float) get_16bit(0);
27 for (int i = 0; i < data[3]; i++)
28 value /= 10.0;
29 switch (this->state_) {
30 case 1:
31 ESP_LOGD(TAG, "pH=%.1f", value);
32 if (this->ph_sensor_ != nullptr)
33 this->ph_sensor_->publish_state(value);
34 break;
35 case 2:
36 ESP_LOGD(TAG, "temperature=%.1f", value);
37 if (this->temperature_sensor_ != nullptr)
39 break;
40 case 3:
41 ESP_LOGD(TAG, "DIS1=%.1f", value);
42 if (this->dis1_sensor_ != nullptr)
43 this->dis1_sensor_->publish_state(value);
44 break;
45 case 4:
46 ESP_LOGD(TAG, "DIS2=%.1f", value);
47 if (this->dis2_sensor_ != nullptr)
48 this->dis2_sensor_->publish_state(value);
49 break;
50 case 5:
51 ESP_LOGD(TAG, "REDOX=%.1f", value);
52 if (this->redox_sensor_ != nullptr)
53 this->redox_sensor_->publish_state(value);
54 break;
55 case 6:
56 ESP_LOGD(TAG, "EC=%.1f", value);
57 if (this->ec_sensor_ != nullptr)
58 this->ec_sensor_->publish_state(value);
59 break;
60 case 7:
61 ESP_LOGD(TAG, "OCI=%.1f", value);
62 if (this->oci_sensor_ != nullptr)
63 this->oci_sensor_->publish_state(value);
64 break;
65 }
66 if (++this->state_ > 7)
67 this->state_ = 0;
68}
69
71 uint32_t now = App.get_loop_component_start_time();
72 // timeout after 15 seconds
73 if (this->waiting_ && (now - this->last_send_ > 15000)) {
74 ESP_LOGW(TAG, "timed out waiting for response");
75 this->waiting_ = false;
76 }
77 if (this->waiting_ || (this->state_ == 0))
78 return;
79 this->last_send_ = now;
80 send(CMD_READ_REG, REGISTER[this->state_ - 1], 2);
81 this->waiting_ = true;
82}
83
84void Kuntze::update() { this->state_ = 1; }
85
87 ESP_LOGCONFIG(TAG,
88 "Kuntze:\n"
89 " Address: 0x%02X",
90 this->address_);
91 LOG_SENSOR("", "pH", this->ph_sensor_);
92 LOG_SENSOR("", "temperature", this->temperature_sensor_);
93 LOG_SENSOR("", "DIS1", this->dis1_sensor_);
94 LOG_SENSOR("", "DIS2", this->dis2_sensor_);
95 LOG_SENSOR("", "REDOX", this->redox_sensor_);
96 LOG_SENSOR("", "EC", this->ec_sensor_);
97 LOG_SENSOR("", "OCI", this->oci_sensor_);
98}
99
100} // namespace kuntze
101} // 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:84
sensor::Sensor * ph_sensor_
Definition kuntze.h:32
sensor::Sensor * dis1_sensor_
Definition kuntze.h:34
void loop() override
Definition kuntze.cpp:70
sensor::Sensor * redox_sensor_
Definition kuntze.h:36
void dump_config() override
Definition kuntze.cpp:86
void on_modbus_data(const std::vector< uint8_t > &data) override
Definition kuntze.cpp:17
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:65
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:76
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:334
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:830
Application App
Global storage of Application pointer - only one Application can exist.