ESPHome 2026.2.3
Loading...
Searching...
No Matches
kamstrup_kmp.cpp
Go to the documentation of this file.
1#include "kamstrup_kmp.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace kamstrup_kmp {
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 buffer[buffer_len - 2] = 0;
100 buffer[buffer_len - 1] = 0;
101
102 uint16_t crc = crc16_ccitt(buffer, buffer_len);
103 buffer[buffer_len - 2] = crc >> 8;
104 buffer[buffer_len - 1] = crc & 0xFF;
105
106 // Prepare actual TX message
107 uint8_t tx_msg[20];
108 int tx_msg_len = 1;
109 tx_msg[0] = 0x80; // prefix
110
111 for (int i = 0; i < buffer_len; i++) {
112 if (buffer[i] == 0x06 || buffer[i] == 0x0d || buffer[i] == 0x1b || buffer[i] == 0x40 || buffer[i] == 0x80) {
113 tx_msg[tx_msg_len++] = 0x1b;
114 tx_msg[tx_msg_len++] = buffer[i] ^ 0xff;
115 } else {
116 tx_msg[tx_msg_len++] = buffer[i];
117 }
118 }
119
120 tx_msg[tx_msg_len++] = 0x0D; // EOM
121
122 this->write_array(tx_msg, tx_msg_len);
123}
124
126 uint8_t tmp;
127 while (this->available()) {
128 this->read_byte(&tmp);
129 }
130}
131
133 uint8_t buffer[20] = {0};
134 int buffer_len = 0;
135 int data;
136 int timeout = 250; // ms
137
138 // Read the data from the UART
139 while (timeout > 0) {
140 if (this->available()) {
141 data = this->read();
142 if (data > -1) {
143 if (data == 0x40) { // start of message
144 buffer_len = 0;
145 }
146 buffer[buffer_len++] = (uint8_t) data;
147 if (data == 0x0D) {
148 break;
149 }
150 } else {
151 ESP_LOGE(TAG, "Error while reading from UART");
152 }
153 } else {
154 delay(1);
155 timeout--;
156 }
157 }
158
159 if (timeout == 0 || buffer_len == 0) {
160 ESP_LOGE(TAG, "Request timed out");
161 return;
162 }
163
164 // Validate message (prefix and suffix)
165 if (buffer[0] != 0x40) {
166 ESP_LOGE(TAG, "Received invalid message (prefix mismatch received 0x%02X, expected 0x40)", buffer[0]);
167 return;
168 }
169
170 if (buffer[buffer_len - 1] != 0x0D) {
171 ESP_LOGE(TAG, "Received invalid message (EOM mismatch received 0x%02X, expected 0x0D)", buffer[buffer_len - 1]);
172 return;
173 }
174
175 // Decode
176 uint8_t msg[20] = {0};
177 int msg_len = 0;
178 for (int i = 1; i < buffer_len - 1; i++) {
179 if (buffer[i] == 0x1B) {
180 msg[msg_len++] = buffer[i + 1] ^ 0xFF;
181 i++;
182 } else {
183 msg[msg_len++] = buffer[i];
184 }
185 }
186
187 // Validate CRC
188 if (crc16_ccitt(msg, msg_len)) {
189 ESP_LOGE(TAG, "Received invalid message (CRC mismatch)");
190 return;
191 }
192
193 // All seems good. Now parse the message
194 this->parse_command_message_(command, msg, msg_len);
195}
196
197void KamstrupKMPComponent::parse_command_message_(uint16_t command, const uint8_t *msg, int msg_len) {
198 // Validate the message
199 if (msg_len < 8) {
200 ESP_LOGE(TAG, "Received invalid message (message too small)");
201 return;
202 }
203
204 if (msg[0] != 0x3F || msg[1] != 0x10) {
205 ESP_LOGE(TAG, "Received invalid message (invalid header received 0x%02X%02X, expected 0x3F10)", msg[0], msg[1]);
206 return;
207 }
208
209 uint16_t recv_command = msg[2] << 8 | msg[3];
210 if (recv_command != command) {
211 ESP_LOGE(TAG, "Received invalid message (invalid unexpected command received 0x%04X, expected 0x%04X)",
212 recv_command, command);
213 return;
214 }
215
216 uint8_t unit_idx = msg[4];
217 uint8_t mantissa_range = msg[5];
218
219 if (mantissa_range > 4) {
220 ESP_LOGE(TAG, "Received invalid message (mantissa size too large %d, expected 4)", mantissa_range);
221 return;
222 }
223
224 // Calculate exponent
225 float exponent = msg[6] & 0x3F;
226 if (msg[6] & 0x40) {
227 exponent = -exponent;
228 }
229 exponent = powf(10, exponent);
230 if (msg[6] & 0x80) {
231 exponent = -exponent;
232 }
233
234 // Calculate mantissa
235 uint32_t mantissa = 0;
236 for (int i = 0; i < mantissa_range; i++) {
237 mantissa <<= 8;
238 mantissa |= msg[i + 7];
239 }
240
241 // Calculate the actual value
242 float value = mantissa * exponent;
243
244 // Set sensor value
245 this->set_sensor_value_(command, value, unit_idx);
246}
247
248void KamstrupKMPComponent::set_sensor_value_(uint16_t command, float value, uint8_t unit_idx) {
249 const char *unit = UNITS[unit_idx];
250
251 // Standard sensors
252 if (command == CMD_HEAT_ENERGY && this->heat_energy_sensor_ != nullptr) {
254 } else if (command == CMD_POWER && this->power_sensor_ != nullptr) {
255 this->power_sensor_->publish_state(value);
256 } else if (command == CMD_TEMP1 && this->temp1_sensor_ != nullptr) {
257 this->temp1_sensor_->publish_state(value);
258 } else if (command == CMD_TEMP2 && this->temp2_sensor_ != nullptr) {
259 this->temp2_sensor_->publish_state(value);
260 } else if (command == CMD_TEMP_DIFF && this->temp_diff_sensor_ != nullptr) {
261 this->temp_diff_sensor_->publish_state(value);
262 } else if (command == CMD_FLOW && this->flow_sensor_ != nullptr) {
263 this->flow_sensor_->publish_state(value);
264 } else if (command == CMD_VOLUME && this->volume_sensor_ != nullptr) {
265 this->volume_sensor_->publish_state(value);
266 }
267
268 // Custom sensors
269 for (size_t i = 0; i < this->custom_commands_.size(); i++) {
270 if (command == this->custom_commands_[i]) {
271 this->custom_sensors_[i]->publish_state(value);
272 }
273 }
274
275 ESP_LOGD(TAG, "Received value for command 0x%04X: %.3f [%s]", command, value, unit);
276}
277
278uint16_t crc16_ccitt(const uint8_t *buffer, int len) {
279 uint32_t poly = 0x1021;
280 uint32_t reg = 0x00;
281 for (int i = 0; i < len; i++) {
282 int mask = 0x80;
283 while (mask > 0) {
284 reg <<= 1;
285 if (buffer[i] & mask) {
286 reg |= 1;
287 }
288 mask >>= 1;
289 if (reg & 0x10000) {
290 reg &= 0xffff;
291 reg ^= poly;
292 }
293 }
294 }
295 return (uint16_t) reg;
296}
297
298} // namespace kamstrup_kmp
299} // namespace esphome
bool is_failed() const
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:65
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 crc16_ccitt(const uint8_t *buffer, int len)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:692
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26