ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
kamstrup_kmp.cpp
Go to the documentation of this file.
1#include "kamstrup_kmp.h"
2
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "kamstrup_kmp";
9
11 ESP_LOGCONFIG(TAG, "kamstrup_kmp:");
12 if (this->is_failed()) {
13 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
14 }
15 LOG_UPDATE_INTERVAL(this);
16
17 LOG_SENSOR(" ", "Heat Energy", this->heat_energy_sensor_);
18 LOG_SENSOR(" ", "Power", this->power_sensor_);
19 LOG_SENSOR(" ", "Temperature 1", this->temp1_sensor_);
20 LOG_SENSOR(" ", "Temperature 2", this->temp2_sensor_);
21 LOG_SENSOR(" ", "Temperature Difference", this->temp_diff_sensor_);
22 LOG_SENSOR(" ", "Flow", this->flow_sensor_);
23 LOG_SENSOR(" ", "Volume", this->volume_sensor_);
24
25 for (size_t i = 0; i < this->custom_sensors_.size(); i++) {
26 LOG_SENSOR(" ", "Custom Sensor", this->custom_sensors_[i]);
27 ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom_commands_[i]);
28 }
29
31}
32
34 if (this->heat_energy_sensor_ != nullptr) {
35 this->command_queue_.push(CMD_HEAT_ENERGY);
36 }
37
38 if (this->power_sensor_ != nullptr) {
39 this->command_queue_.push(CMD_POWER);
40 }
41
42 if (this->temp1_sensor_ != nullptr) {
43 this->command_queue_.push(CMD_TEMP1);
44 }
45
46 if (this->temp2_sensor_ != nullptr) {
47 this->command_queue_.push(CMD_TEMP2);
48 }
49
50 if (this->temp_diff_sensor_ != nullptr) {
51 this->command_queue_.push(CMD_TEMP_DIFF);
52 }
53
54 if (this->flow_sensor_ != nullptr) {
55 this->command_queue_.push(CMD_FLOW);
56 }
57
58 if (this->volume_sensor_ != nullptr) {
59 this->command_queue_.push(CMD_VOLUME);
60 }
61
62 for (uint16_t custom_command : this->custom_commands_) {
63 this->command_queue_.push(custom_command);
64 }
65}
66
68 if (!this->command_queue_.empty()) {
69 uint16_t command = this->command_queue_.front();
70 this->send_command_(command);
71 this->command_queue_.pop();
72 }
73}
74
76 uint32_t msg_len = 5;
77 uint8_t msg[msg_len];
78
79 msg[0] = 0x3F;
80 msg[1] = 0x10;
81 msg[2] = 0x01;
82 msg[3] = command >> 8;
83 msg[4] = command & 0xFF;
84
86 this->send_message_(msg, msg_len);
87 this->read_command_(command);
88}
89
90void KamstrupKMPComponent::send_message_(const uint8_t *msg, int msg_len) {
91 int buffer_len = msg_len + 2;
92 uint8_t buffer[buffer_len];
93
94 // Prepare the basic message and appand CRC
95 for (int i = 0; i < msg_len; i++) {
96 buffer[i] = msg[i];
97 }
98
99 uint16_t crc = crc16be(buffer, buffer_len - 2);
100 buffer[buffer_len - 2] = crc >> 8;
101 buffer[buffer_len - 1] = crc & 0xFF;
102
103 // Prepare actual TX message
104 uint8_t tx_msg[20];
105 int tx_msg_len = 1;
106 tx_msg[0] = 0x80; // prefix
107
108 for (int i = 0; i < buffer_len; i++) {
109 if (buffer[i] == 0x06 || buffer[i] == 0x0d || buffer[i] == 0x1b || buffer[i] == 0x40 || buffer[i] == 0x80) {
110 if (tx_msg_len + 2 >= static_cast<int>(sizeof(tx_msg))) {
111 ESP_LOGE(TAG, "TX message overflow");
112 return;
113 }
114 tx_msg[tx_msg_len++] = 0x1b;
115 tx_msg[tx_msg_len++] = buffer[i] ^ 0xff;
116 } else {
117 if (tx_msg_len + 1 >= static_cast<int>(sizeof(tx_msg))) {
118 ESP_LOGE(TAG, "TX message overflow");
119 return;
120 }
121 tx_msg[tx_msg_len++] = buffer[i];
122 }
123 }
124
125 tx_msg[tx_msg_len++] = 0x0D; // EOM
126
127 this->write_array(tx_msg, tx_msg_len);
128}
129
131 uint8_t tmp;
132 while (this->available()) {
133 this->read_byte(&tmp);
134 }
135}
136
138 uint8_t buffer[20] = {0};
139 size_t buffer_len = 0;
140 int data;
141 int timeout = 250; // ms
142
143 // Read the data from the UART
144 while (timeout > 0 && buffer_len < sizeof(buffer)) {
145 if (this->available()) {
146 data = this->read();
147 if (data > -1) {
148 if (data == 0x40) { // start of message
149 buffer_len = 0;
150 }
151 buffer[buffer_len++] = (uint8_t) data;
152 if (data == 0x0D) {
153 break;
154 }
155 } else {
156 ESP_LOGE(TAG, "Error while reading from UART");
157 }
158 } else {
159 delay(1);
160 timeout--;
161 }
162 }
163
164 if (timeout == 0 || buffer_len == 0) {
165 ESP_LOGE(TAG, "Request timed out");
166 return;
167 }
168
169 // Validate message (prefix and suffix)
170 if (buffer[0] != 0x40) {
171 ESP_LOGE(TAG, "Received invalid message (prefix mismatch received 0x%02X, expected 0x40)", buffer[0]);
172 return;
173 }
174
175 if (buffer[buffer_len - 1] != 0x0D) {
176 ESP_LOGE(TAG, "Received invalid message (EOM mismatch received 0x%02X, expected 0x0D)", buffer[buffer_len - 1]);
177 return;
178 }
179
180 // Decode
181 uint8_t msg[20] = {0};
182 int msg_len = 0;
183 for (size_t i = 1; i < buffer_len - 1; i++) {
184 if (buffer[i] == 0x1B) {
185 msg[msg_len++] = buffer[i + 1] ^ 0xFF;
186 i++;
187 } else {
188 msg[msg_len++] = buffer[i];
189 }
190 }
191
192 // Validate CRC
193 if (crc16be(msg, msg_len - 2) != encode_uint16(msg[msg_len - 2], msg[msg_len - 1])) {
194 ESP_LOGE(TAG, "Received invalid message (CRC mismatch)");
195 return;
196 }
197
198 // All seems good. Now parse the message
199 this->parse_command_message_(command, msg, msg_len);
200}
201
202void KamstrupKMPComponent::parse_command_message_(uint16_t command, const uint8_t *msg, int msg_len) {
203 // Validate the message
204 if (msg_len < 8) {
205 ESP_LOGE(TAG, "Received invalid message (message too small)");
206 return;
207 }
208
209 if (msg[0] != 0x3F || msg[1] != 0x10) {
210 ESP_LOGE(TAG, "Received invalid message (invalid header received 0x%02X%02X, expected 0x3F10)", msg[0], msg[1]);
211 return;
212 }
213
214 uint16_t recv_command = msg[2] << 8 | msg[3];
215 if (recv_command != command) {
216 ESP_LOGE(TAG, "Received invalid message (invalid unexpected command received 0x%04X, expected 0x%04X)",
217 recv_command, command);
218 return;
219 }
220
221 uint8_t unit_idx = msg[4];
222 uint8_t mantissa_range = msg[5];
223
224 if (mantissa_range > 4 || msg_len < 7 + mantissa_range) {
225 ESP_LOGE(TAG, "Received invalid message (mantissa size %d, msg_len %d)", mantissa_range, msg_len);
226 return;
227 }
228
229 // Calculate exponent
230 int8_t exp_val = msg[6] & 0x3F;
231 if (msg[6] & 0x40) {
232 exp_val = -exp_val;
233 }
234 float exponent = pow10_int(exp_val);
235 if (msg[6] & 0x80) {
236 exponent = -exponent;
237 }
238
239 // Calculate mantissa
240 uint32_t mantissa = 0;
241 for (int i = 0; i < mantissa_range; i++) {
242 mantissa <<= 8;
243 mantissa |= msg[i + 7];
244 }
245
246 // Calculate the actual value
247 float value = mantissa * exponent;
248
249 // Set sensor value
250 this->set_sensor_value_(command, value, unit_idx);
251}
252
253void KamstrupKMPComponent::set_sensor_value_(uint16_t command, float value, uint8_t unit_idx) {
254 const char *unit = unit_idx < sizeof(UNITS) / sizeof(UNITS[0]) ? UNITS[unit_idx] : "";
255
256 // Standard sensors
257 if (command == CMD_HEAT_ENERGY && this->heat_energy_sensor_ != nullptr) {
259 } else if (command == CMD_POWER && this->power_sensor_ != nullptr) {
260 this->power_sensor_->publish_state(value);
261 } else if (command == CMD_TEMP1 && this->temp1_sensor_ != nullptr) {
262 this->temp1_sensor_->publish_state(value);
263 } else if (command == CMD_TEMP2 && this->temp2_sensor_ != nullptr) {
264 this->temp2_sensor_->publish_state(value);
265 } else if (command == CMD_TEMP_DIFF && this->temp_diff_sensor_ != nullptr) {
266 this->temp_diff_sensor_->publish_state(value);
267 } else if (command == CMD_FLOW && this->flow_sensor_ != nullptr) {
268 this->flow_sensor_->publish_state(value);
269 } else if (command == CMD_VOLUME && this->volume_sensor_ != nullptr) {
270 this->volume_sensor_->publish_state(value);
271 }
272
273 // Custom sensors
274 for (size_t i = 0; i < this->custom_commands_.size(); i++) {
275 if (command == this->custom_commands_[i]) {
276 this->custom_sensors_[i]->publish_state(value);
277 }
278 }
279
280 ESP_LOGD(TAG, "Received value for command 0x%04X: %.3f [%s]", command, value, unit);
281}
282
283} // namespace esphome::kamstrup_kmp
bool is_failed() const
Definition component.h:272
void set_sensor_value_(uint16_t command, float value, uint8_t unit_idx)
std::vector< sensor::Sensor * > custom_sensors_
void parse_command_message_(uint16_t command, const uint8_t *msg, int msg_len)
void send_message_(const uint8_t *msg, int msg_len)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:126
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
void HOT delay(uint32_t ms)
Definition hal.cpp:85
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.
Definition helpers.h:752
static void uint32_t