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 if (tx_msg_len + 2 >=
static_cast<int>(
sizeof(tx_msg))) {
114 ESP_LOGE(TAG,
"TX message overflow");
117 tx_msg[tx_msg_len++] = 0x1b;
118 tx_msg[tx_msg_len++] = buffer[i] ^ 0xff;
120 if (tx_msg_len + 1 >=
static_cast<int>(
sizeof(tx_msg))) {
121 ESP_LOGE(TAG,
"TX message overflow");
124 tx_msg[tx_msg_len++] = buffer[i];
128 tx_msg[tx_msg_len++] = 0x0D;
141 uint8_t buffer[20] = {0};
147 while (timeout > 0 && buffer_len <
static_cast<int>(
sizeof(buffer))) {
154 buffer[buffer_len++] = (uint8_t) data;
159 ESP_LOGE(TAG,
"Error while reading from UART");
167 if (timeout == 0 || buffer_len == 0) {
168 ESP_LOGE(TAG,
"Request timed out");
173 if (buffer[0] != 0x40) {
174 ESP_LOGE(TAG,
"Received invalid message (prefix mismatch received 0x%02X, expected 0x40)", buffer[0]);
178 if (buffer[buffer_len - 1] != 0x0D) {
179 ESP_LOGE(TAG,
"Received invalid message (EOM mismatch received 0x%02X, expected 0x0D)", buffer[buffer_len - 1]);
184 uint8_t msg[20] = {0};
186 for (
int i = 1; i < buffer_len - 1; i++) {
187 if (buffer[i] == 0x1B) {
188 msg[msg_len++] = buffer[i + 1] ^ 0xFF;
191 msg[msg_len++] = buffer[i];
197 ESP_LOGE(TAG,
"Received invalid message (CRC mismatch)");
208 ESP_LOGE(TAG,
"Received invalid message (message too small)");
212 if (msg[0] != 0x3F || msg[1] != 0x10) {
213 ESP_LOGE(TAG,
"Received invalid message (invalid header received 0x%02X%02X, expected 0x3F10)", msg[0], msg[1]);
217 uint16_t recv_command = msg[2] << 8 | msg[3];
218 if (recv_command != command) {
219 ESP_LOGE(TAG,
"Received invalid message (invalid unexpected command received 0x%04X, expected 0x%04X)",
220 recv_command, command);
224 uint8_t unit_idx = msg[4];
225 uint8_t mantissa_range = msg[5];
227 if (mantissa_range > 4 || msg_len < 7 + mantissa_range) {
228 ESP_LOGE(TAG,
"Received invalid message (mantissa size %d, msg_len %d)", mantissa_range, msg_len);
233 int8_t exp_val = msg[6] & 0x3F;
239 exponent = -exponent;
244 for (
int i = 0; i < mantissa_range; i++) {
246 mantissa |= msg[i + 7];
250 float value = mantissa * exponent;
257 const char *unit = unit_idx <
sizeof(UNITS) /
sizeof(UNITS[0]) ? UNITS[unit_idx] :
"";
262 }
else if (command == CMD_POWER && this->
power_sensor_ !=
nullptr) {
264 }
else if (command == CMD_TEMP1 && this->
temp1_sensor_ !=
nullptr) {
266 }
else if (command == CMD_TEMP2 && this->
temp2_sensor_ !=
nullptr) {
270 }
else if (command == CMD_FLOW && this->
flow_sensor_ !=
nullptr) {
272 }
else if (command == CMD_VOLUME && this->
volume_sensor_ !=
nullptr) {
283 ESP_LOGD(TAG,
"Received value for command 0x%04X: %.3f [%s]", command, value, unit);
289 for (
int i = 0; i <
len; i++) {
293 if (buffer[i] & mask) {
303 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.