ESPHome  2024.5.2
pylontech.cpp
Go to the documentation of this file.
1 #include "pylontech.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace pylontech {
7 
8 static const char *const TAG = "pylontech";
9 static const int MAX_DATA_LENGTH_BYTES = 256;
10 static 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  ESP_LOGCONFIG(TAG, "Setting up pylontech...");
30  while (this->available() != 0) {
31  this->read();
32  }
33 }
34 
35 void PylontechComponent::update() { this->write_str("pwr\n"); }
36 
38  if (this->available() > 0) {
39  // pylontech sends a lot of data very suddenly
40  // we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
41  uint8_t data;
42  int recv = 0;
43  while (this->available() > 0) {
44  if (this->read_byte(&data)) {
45  buffer_[buffer_index_write_] += (char) data;
46  recv++;
47  if (buffer_[buffer_index_write_].back() == static_cast<char>(ASCII_LF) ||
48  buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
49  // complete line received
50  buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
51  }
52  }
53  }
54  ESP_LOGV(TAG, "received %d bytes", recv);
55  } else {
56  // only process one line per call of loop() to not block esphome for too long
59  buffer_[buffer_index_read_].clear();
60  buffer_index_read_ = (buffer_index_read_ + 1) % NUM_BUFFERS;
61  }
62  }
63 }
64 
65 void PylontechComponent::process_line_(std::string &buffer) {
66  ESP_LOGV(TAG, "Read from serial: %s", buffer.substr(0, buffer.size() - 2).c_str());
67  // clang-format off
68  // example line to parse:
69  // 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
70  // 1 50548 8910 25000 24200 25000 3368 3371 Charge Normal Normal Normal 97% 2021-06-30 20:49:45 Normal Normal 22700 Normal
71  // clang-format on
72 
74  char mostempr_s[6];
75  const int parsed = sscanf( // NOLINT
76  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
77  &l.bat_num, &l.volt, &l.curr, &l.tempr, &l.tlow, &l.thigh, &l.vlow, &l.vhigh, l.base_st, l.volt_st, // NOLINT
78  l.curr_st, l.temp_st, &l.coulomb, mostempr_s); // NOLINT
79 
80  if (l.bat_num <= 0) {
81  ESP_LOGD(TAG, "invalid bat_num in line %s", buffer.substr(0, buffer.size() - 2).c_str());
82  return;
83  }
84  if (parsed != 14) {
85  ESP_LOGW(TAG, "invalid line: found only %d items in %s", parsed, buffer.substr(0, buffer.size() - 2).c_str());
86  return;
87  }
88  auto mostempr_parsed = parse_number<int>(mostempr_s);
89  if (mostempr_parsed.has_value()) {
90  l.mostempr = mostempr_parsed.value();
91  } else {
92  l.mostempr = -300;
93  ESP_LOGW(TAG, "bat_num %d: received no mostempr", l.bat_num);
94  }
95 
96  for (PylontechListener *listener : this->listeners_) {
97  listener->on_line_read(&l);
98  }
99 }
100 
102 
103 } // namespace pylontech
104 } // namespace esphome
void write_str(const char *str)
Definition: uart.h:27
void loop() override
Read data once available.
Definition: pylontech.cpp:37
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: pylontech.cpp:101
void setup() override
Setup the sensor and test for a connection.
Definition: pylontech.cpp:28
uint8_t l
Definition: bl0939.h:19
std::vector< PylontechListener * > listeners_
Definition: pylontech.h:49
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 update() override
Schedule data readings.
Definition: pylontech.cpp:35
bool read_byte(uint8_t *data)
Definition: uart.h:29
uint16_t length
Definition: tt21100.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void process_line_(std::string &buffer)
Definition: pylontech.cpp:65
std::string buffer_[NUM_BUFFERS]
Definition: pylontech.h:45