ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
kuntze.cpp
Go to the documentation of this file.
1#include "kuntze.h"
2#include "esphome/core/log.h"
3
4namespace esphome::kuntze {
5
6static const char *const TAG = "kuntze";
7
8static constexpr uint16_t REGISTER_PH = 4136;
9static constexpr uint16_t REGISTER_TEMPERATURE = 4160;
10static constexpr uint16_t REGISTER_DIS1 = 4680;
11static constexpr uint16_t REGISTER_DIS2 = 6000;
12static constexpr uint16_t REGISTER_REDOX = 4688;
13static constexpr uint16_t REGISTER_EC = 4728;
14static constexpr uint16_t REGISTER_OCI = 5832;
15static constexpr uint16_t REGISTER[] = {REGISTER_PH, REGISTER_TEMPERATURE, REGISTER_DIS1, REGISTER_DIS2,
16 REGISTER_REDOX, REGISTER_EC, REGISTER_OCI};
17
18void Kuntze::on_read_holding_registers(uint16_t start_address, std::span<const uint16_t> registers,
20 if (!modbus::succeeded(status) || registers.size() < 2)
21 return;
22
23 // Each value is a register pair: the reading, then the number of decimal places in its low byte.
24 float value = registers[0];
25 for (uint16_t i = 0; i < (registers[1] & 0xFF); i++)
26 value /= 10.0f;
27
28 switch (start_address) {
29 case REGISTER_PH:
30 ESP_LOGD(TAG, "pH=%.1f", value);
31 if (this->ph_sensor_ != nullptr)
32 this->ph_sensor_->publish_state(value);
33 break;
34 case REGISTER_TEMPERATURE:
35 ESP_LOGD(TAG, "temperature=%.1f", value);
36 if (this->temperature_sensor_ != nullptr)
38 break;
39 case REGISTER_DIS1:
40 ESP_LOGD(TAG, "DIS1=%.1f", value);
41 if (this->dis1_sensor_ != nullptr)
42 this->dis1_sensor_->publish_state(value);
43 break;
44 case REGISTER_DIS2:
45 ESP_LOGD(TAG, "DIS2=%.1f", value);
46 if (this->dis2_sensor_ != nullptr)
47 this->dis2_sensor_->publish_state(value);
48 break;
49 case REGISTER_REDOX:
50 ESP_LOGD(TAG, "REDOX=%.1f", value);
51 if (this->redox_sensor_ != nullptr)
52 this->redox_sensor_->publish_state(value);
53 break;
54 case REGISTER_EC:
55 ESP_LOGD(TAG, "EC=%.1f", value);
56 if (this->ec_sensor_ != nullptr)
57 this->ec_sensor_->publish_state(value);
58 break;
59 case REGISTER_OCI:
60 ESP_LOGD(TAG, "OCI=%.1f", value);
61 if (this->oci_sensor_ != nullptr)
62 this->oci_sensor_->publish_state(value);
63 break;
64 }
65}
66
68 for (uint16_t reg : REGISTER)
69 this->read_holding_registers(reg, 2);
70}
71
73 ESP_LOGCONFIG(TAG,
74 "Kuntze:\n"
75 " Address: 0x%02X",
76 this->address_);
77 LOG_SENSOR("", "pH", this->ph_sensor_);
78 LOG_SENSOR("", "temperature", this->temperature_sensor_);
79 LOG_SENSOR("", "DIS1", this->dis1_sensor_);
80 LOG_SENSOR("", "DIS2", this->dis2_sensor_);
81 LOG_SENSOR("", "REDOX", this->redox_sensor_);
82 LOG_SENSOR("", "EC", this->ec_sensor_);
83 LOG_SENSOR("", "OCI", this->oci_sensor_);
84}
85
86} // namespace esphome::kuntze
uint8_t status
Definition bl0942.h:8
sensor::Sensor * temperature_sensor_
Definition kuntze.h:30
void on_read_holding_registers(uint16_t start_address, std::span< const uint16_t > registers, modbus::ResponseStatus status) override
Definition kuntze.cpp:18
sensor::Sensor * dis2_sensor_
Definition kuntze.h:32
sensor::Sensor * ec_sensor_
Definition kuntze.h:34
sensor::Sensor * oci_sensor_
Definition kuntze.h:35
void update() override
Definition kuntze.cpp:67
sensor::Sensor * ph_sensor_
Definition kuntze.h:29
sensor::Sensor * dis1_sensor_
Definition kuntze.h:31
sensor::Sensor * redox_sensor_
Definition kuntze.h:33
void dump_config() override
Definition kuntze.cpp:72
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
std::optional< ExceptionCode > ResponseStatus
Definition modbus.h:306
bool succeeded(ResponseStatus status)
True when a transaction carried no exception.
Definition modbus.h:309