6namespace kamstrup_kmp {
8static const char *
const TAG =
"kamstrup_kmp";
11 ESP_LOGCONFIG(TAG,
"kamstrup_kmp:");
13 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
15 LOG_UPDATE_INTERVAL(
this);
82 msg[3] = command >> 8;
83 msg[4] = command & 0xFF;
91 int buffer_len = msg_len + 2;
92 uint8_t buffer[buffer_len];
95 for (
int i = 0; i < msg_len; i++) {
99 buffer[buffer_len - 2] = 0;
100 buffer[buffer_len - 1] = 0;
103 buffer[buffer_len - 2] = crc >> 8;
104 buffer[buffer_len - 1] = crc & 0xFF;
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;
116 tx_msg[tx_msg_len++] = buffer[i];
120 tx_msg[tx_msg_len++] = 0x0D;
133 uint8_t buffer[20] = {0};
139 while (timeout > 0) {
146 buffer[buffer_len++] = (uint8_t) data;
151 ESP_LOGE(TAG,
"Error while reading from UART");
159 if (timeout == 0 || buffer_len == 0) {
160 ESP_LOGE(TAG,
"Request timed out");
165 if (buffer[0] != 0x40) {
166 ESP_LOGE(TAG,
"Received invalid message (prefix mismatch received 0x%02X, expected 0x40)", buffer[0]);
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]);
176 uint8_t msg[20] = {0};
178 for (
int i = 1; i < buffer_len - 1; i++) {
179 if (buffer[i] == 0x1B) {
180 msg[msg_len++] = buffer[i + 1] ^ 0xFF;
183 msg[msg_len++] = buffer[i];
189 ESP_LOGE(TAG,
"Received invalid message (CRC mismatch)");
200 ESP_LOGE(TAG,
"Received invalid message (message too small)");
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]);
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);
216 uint8_t unit_idx = msg[4];
217 uint8_t mantissa_range = msg[5];
219 if (mantissa_range > 4) {
220 ESP_LOGE(TAG,
"Received invalid message (mantissa size too large %d, expected 4)", mantissa_range);
225 int8_t exp_val = msg[6] & 0x3F;
231 exponent = -exponent;
235 uint32_t mantissa = 0;
236 for (
int i = 0; i < mantissa_range; i++) {
238 mantissa |= msg[i + 7];
242 float value = mantissa * exponent;
249 const char *unit = UNITS[unit_idx];
254 }
else if (command == CMD_POWER && this->
power_sensor_ !=
nullptr) {
256 }
else if (command == CMD_TEMP1 && this->
temp1_sensor_ !=
nullptr) {
258 }
else if (command == CMD_TEMP2 && this->
temp2_sensor_ !=
nullptr) {
262 }
else if (command == CMD_FLOW && this->
flow_sensor_ !=
nullptr) {
264 }
else if (command == CMD_VOLUME && this->
volume_sensor_ !=
nullptr) {
275 ESP_LOGD(TAG,
"Received value for command 0x%04X: %.3f [%s]", command, value, unit);
279 uint32_t poly = 0x1021;
281 for (
int i = 0; i <
len; i++) {
285 if (buffer[i] & mask) {
295 return (uint16_t) reg;
void clear_uart_rx_buffer_()
sensor::Sensor * power_sensor_
void set_sensor_value_(uint16_t command, float value, uint8_t unit_idx)
std::queue< uint16_t > command_queue_
sensor::Sensor * temp_diff_sensor_
void send_command_(uint16_t command)
sensor::Sensor * flow_sensor_
sensor::Sensor * temp2_sensor_
sensor::Sensor * temp1_sensor_
sensor::Sensor * heat_energy_sensor_
void read_command_(uint16_t command)
std::vector< sensor::Sensor * > custom_sensors_
void parse_command_message_(uint16_t command, const uint8_t *msg, int msg_len)
sensor::Sensor * volume_sensor_
void send_message_(const uint8_t *msg, int msg_len)
std::vector< uint16_t > custom_commands_
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
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...
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
uint16_t crc16_ccitt(const uint8_t *buffer, int len)
@ UART_CONFIG_PARITY_NONE
Providing packet encoding functions for exchanging data with a remote host.
void HOT delay(uint32_t ms)
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.