ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
pylontech.cpp
Go to the documentation of this file.
1#include "pylontech.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace pylontech {
7
8static const char *const TAG = "pylontech";
9static const int MAX_DATA_LENGTH_BYTES = 256;
10static const uint8_t ASCII_LF = 0x0A;
11
13
16 ESP_LOGCONFIG(TAG, "pylontech:");
17 if (this->is_failed()) {
18 ESP_LOGE(TAG, "Connection with pylontech failed!");
19 }
20
21 for (PylontechListener *listener : this->listeners_) {
22 listener->dump_config();
23 }
24
25 LOG_UPDATE_INTERVAL(this);
26}
27
29 while (this->available() != 0) {
30 this->read();
31 }
32}
33
34void PylontechComponent::update() { this->write_str("pwr\n"); }
35
37 if (this->available() > 0) {
38 // pylontech sends a lot of data very suddenly
39 // we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
40 uint8_t data;
41 int recv = 0;
42 while (this->available() > 0) {
43 if (this->read_byte(&data)) {
44 buffer_[buffer_index_write_] += (char) data;
45 recv++;
46 if (buffer_[buffer_index_write_].back() == static_cast<char>(ASCII_LF) ||
47 buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
48 // complete line received
49 buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
50 }
51 }
52 }
53 ESP_LOGV(TAG, "received %d bytes", recv);
54 } else {
55 // only process one line per call of loop() to not block esphome for too long
59 buffer_index_read_ = (buffer_index_read_ + 1) % NUM_BUFFERS;
60 }
61 }
62}
63
64void PylontechComponent::process_line_(std::string &buffer) {
65 ESP_LOGV(TAG, "Read from serial: %s", buffer.substr(0, buffer.size() - 2).c_str());
66 // clang-format off
67 // example line to parse:
68 // Power Volt Curr Tempr Tlow Thigh Vlow Vhigh Base.St Volt.St Curr.St Temp.St Coulomb Time B.V.St B.T.St MosTempr M.T.St
69 // 1 50548 8910 25000 24200 25000 3368 3371 Charge Normal Normal Normal 97% 2021-06-30 20:49:45 Normal Normal 22700 Normal
70 // clang-format on
71
73 char mostempr_s[6];
74 const int parsed = sscanf( // NOLINT
75 buffer.c_str(), "%d %d %d %d %d %d %d %d %7s %7s %7s %7s %d%% %*d-%*d-%*d %*d:%*d:%*d %*s %*s %5s %*s", // NOLINT
76 &l.bat_num, &l.volt, &l.curr, &l.tempr, &l.tlow, &l.thigh, &l.vlow, &l.vhigh, l.base_st, l.volt_st, // NOLINT
77 l.curr_st, l.temp_st, &l.coulomb, mostempr_s); // NOLINT
78
79 if (l.bat_num <= 0) {
80 ESP_LOGD(TAG, "invalid bat_num in line %s", buffer.substr(0, buffer.size() - 2).c_str());
81 return;
82 }
83 if (parsed != 14) {
84 ESP_LOGW(TAG, "invalid line: found only %d items in %s", parsed, buffer.substr(0, buffer.size() - 2).c_str());
85 return;
86 }
87 auto mostempr_parsed = parse_number<int>(mostempr_s);
88 if (mostempr_parsed.has_value()) {
89 l.mostempr = mostempr_parsed.value();
90 } else {
91 l.mostempr = -300;
92 ESP_LOGW(TAG, "bat_num %d: received no mostempr", l.bat_num);
93 }
94
95 for (PylontechListener *listener : this->listeners_) {
96 listener->on_line_read(&l);
97 }
98}
99
101
102} // namespace pylontech
103} // namespace esphome
uint8_t l
Definition bl0906.h:0
bool is_failed() const
void loop() override
Read data once available.
Definition pylontech.cpp:36
std::vector< PylontechListener * > listeners_
Definition pylontech.h:49
void process_line_(std::string &buffer)
Definition pylontech.cpp:64
void update() override
Schedule data readings.
Definition pylontech.cpp:34
void setup() override
Setup the sensor and test for a connection.
Definition pylontech.cpp:28
std::string buffer_[NUM_BUFFERS]
Definition pylontech.h:45
float get_setup_priority() const override
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...
Definition uart.cpp:13
void write_str(const char *str)
Definition uart.h:27
bool read_byte(uint8_t *data)
Definition uart.h:29
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:291
uint16_t length
Definition tt21100.cpp:0