ESPHome 2026.9.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// Listener interface for entities that want updates for a specific tag.
25 public:
26 explicit Mk2PVRouterListener(const char *tag) : tag_(tag) {}
27 virtual ~Mk2PVRouterListener() = default;
28 const char *get_tag() const { return this->tag_; }
29 virtual void publish_val(const char *val) = 0;
30
31 protected:
32 const char *tag_;
33};
34
35// Reads frames via UART, validates their CRC, and publishes tag/value pairs to listeners.
36class Mk2PVRouter final : public Component, public uart::UARTDevice {
37 public:
38#ifdef MK2PVROUTER_LISTENER_COUNT
40#endif
41 void loop() override;
42 void dump_config() override;
43
44 protected:
45 static constexpr size_t CRC_SUFFIX_LEN = 1;
46 static constexpr uint32_t BAUD_RATE = 9600;
47
48 enum class State : uint8_t {
52 };
53
54#ifdef MK2PVROUTER_LISTENER_COUNT
56#endif
57 uint16_t buf_index_{0};
59 char tag_[MAX_TAG_SIZE];
60 char val_[MAX_VAL_SIZE];
61 char buf_[MAX_BUF_SIZE]; // Large buffer last to reduce padding
62
63 bool read_chars_until_(bool drop, uint8_t c);
64 uint8_t calculate_crc_(const char *grp, size_t grp_len);
65 bool check_crc_(const char *grp, const char *grp_end);
66 void process_group_(const char *grp, const char *grp_end);
67 void publish_value_(const char *tag, const char *val);
68};
69} // namespace esphome::mk2pvrouter
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:227
bool check_crc_(const char *grp, const char *grp_end)
static constexpr size_t CRC_SUFFIX_LEN
Definition mk2pvrouter.h:45
void process_group_(const char *grp, const char *grp_end)
void publish_value_(const char *tag, const char *val)
bool read_chars_until_(bool drop, uint8_t c)
uint8_t calculate_crc_(const char *grp, size_t grp_len)
static constexpr uint32_t BAUD_RATE
Definition mk2pvrouter.h:46
void register_mk2pvrouter_listener(Mk2PVRouterListener *listener)
StaticVector< Mk2PVRouterListener *, MK2PVROUTER_LISTENER_COUNT > mk2pvrouter_listeners_
Definition mk2pvrouter.h:55
virtual void publish_val(const char *val)=0
mopeka_std_values val[3]
static void uint32_t