ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
mk2pvrouter.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome::mk2pvrouter {
9/*
10 * Buffer sizes based on the mk2pvrouter telemetry protocol, as implemented by the
11 * firmware's teleinfo.h (see github.com/FredM67/PVRouter-{1,3}-phase):
12 * - Tags: max 4 chars (S_MC is longest), most are 1-2 chars (P, V1, R2, etc.)
13 * - Values: max 6 digits signed (-10000), typical 1-5 digits. Energy (E) is a daily
14 * counter reset at midnight, so it stays well within 6 digits.
15 * - Frame: STX + multiple lines (LF+tag+TAB+value+TAB+crc+CR) + ETX
16 * - Line format: \n<tag>\t<value>\t<crc>\r (8-15 bytes per line)
17 * - Multi-phase with all features: ~150-200 bytes
18 */
19static constexpr uint8_t MAX_TAG_SIZE = 8; // S_MC (4) + digit (1) + null (1) + margin (2)
20static constexpr uint8_t MAX_VAL_SIZE = 8; // -10000 (6) + null (1) + margin (1)
21static constexpr uint16_t MAX_BUF_SIZE = 256; // Full frame with all features enabled
22
23// Reads frames via UART, validates their CRC, and publishes tag/value pairs to the child entities.
24class Mk2PVRouter : public Component, public uart::UARTDevice {
25 public:
26#ifdef USE_MK2PVROUTER_VALUE_PUBLISHER
27 HUB_PUBLISHER(value, const char *, const char *)
28 HUB_PUBLISHER(dump, const char *)
29#endif
30 void loop() override;
31 void dump_config() override;
32
33 protected:
34 static constexpr size_t CRC_SUFFIX_LEN = 1;
35
36 enum class State : uint8_t {
40 };
41
42 uint16_t buf_index_{0};
44 char tag_[MAX_TAG_SIZE];
45 char val_[MAX_VAL_SIZE];
46 uint8_t buf_[MAX_BUF_SIZE]; // Large buffer last to reduce padding
47
48 bool read_chars_until_(bool drop, uint8_t c);
49 uint8_t calculate_crc_(const uint8_t *grp, size_t grp_len);
50 bool check_crc_(const uint8_t *grp, const uint8_t *grp_end);
51 void process_group_(const uint8_t *grp, const uint8_t *grp_end);
52 void publish_value_(const char *tag, const char *val);
53};
54} // namespace esphome::mk2pvrouter
bool check_crc_(const uint8_t *grp, const uint8_t *grp_end)
static constexpr size_t CRC_SUFFIX_LEN
Definition mk2pvrouter.h:34
HUB_PUBLISHER(value, const char *, const char *) HUB_PUBLISHER(dump
void process_group_(const uint8_t *grp, const uint8_t *grp_end)
void publish_value_(const char *tag, const char *val)
bool read_chars_until_(bool drop, uint8_t c)
const char *void loop() override
uint8_t calculate_crc_(const uint8_t *grp, size_t grp_len)
uint8_t buf_[MAX_BUF_SIZE]
Definition mk2pvrouter.h:46
mopeka_std_values val[3]
uint32_t value
Definition hal.cpp:59
void const char * tag
Definition log.h:110