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 uint16_t crc =
crc16be(buffer, buffer_len - 2);
100 buffer[buffer_len - 2] = crc >> 8;
101 buffer[buffer_len - 1] = crc & 0xFF;
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");
114 tx_msg[tx_msg_len++] = 0x1b;
115 tx_msg[tx_msg_len++] = buffer[i] ^ 0xff;
117 if (tx_msg_len + 1 >=
static_cast<int>(
sizeof(tx_msg))) {
118 ESP_LOGE(TAG,
"TX message overflow");
121 tx_msg[tx_msg_len++] = buffer[i];
125 tx_msg[tx_msg_len++] = 0x0D;
138 uint8_t buffer[20] = {0};
139 size_t buffer_len = 0;
144 while (timeout > 0 && buffer_len <
sizeof(buffer)) {
151 buffer[buffer_len++] = (uint8_t) data;
156 ESP_LOGE(TAG,
"Error while reading from UART");
164 if (timeout == 0 || buffer_len == 0) {
165 ESP_LOGE(TAG,
"Request timed out");
170 if (buffer[0] != 0x40) {
171 ESP_LOGE(TAG,
"Received invalid message (prefix mismatch received 0x%02X, expected 0x40)", buffer[0]);
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]);
181 uint8_t msg[20] = {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;
188 msg[msg_len++] = buffer[i];
194 ESP_LOGE(TAG,
"Received invalid message (CRC mismatch)");
205 ESP_LOGE(TAG,
"Received invalid message (message too small)");
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]);
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);
221 uint8_t unit_idx = msg[4];
222 uint8_t mantissa_range = msg[5];
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);
230 int8_t exp_val = msg[6] & 0x3F;
236 exponent = -exponent;
241 for (
int i = 0; i < mantissa_range; i++) {
243 mantissa |= msg[i + 7];
247 float value = mantissa * exponent;
254 const char *unit = unit_idx <
sizeof(UNITS) /
sizeof(UNITS[0]) ? UNITS[unit_idx] :
"";
259 }
else if (command == CMD_POWER && this->
power_sensor_ !=
nullptr) {
261 }
else if (command == CMD_TEMP1 && this->
temp1_sensor_ !=
nullptr) {
263 }
else if (command == CMD_TEMP2 && this->
temp2_sensor_ !=
nullptr) {
267 }
else if (command == CMD_FLOW && this->
flow_sensor_ !=
nullptr) {
269 }
else if (command == CMD_VOLUME && this->
volume_sensor_ !=
nullptr) {
280 ESP_LOGD(TAG,
"Received value for command 0x%04X: %.3f [%s]", command, value, unit);
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)
@ UART_CONFIG_PARITY_NONE
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.