ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
emontx.cpp
Go to the documentation of this file.
1#include "emontx.h"
2#include "esphome/core/log.h"
4
5namespace esphome::emontx {
6
7static const char *const TAG = "emontx";
8
9void EmonTx::setup() { this->buffer_pos_ = 0; }
10
19 // Read all available data to prevent UART buffer overflow
20 while (this->available() > 0) {
21 uint8_t received = this->read();
22
23 if (received == '\r') {
24 continue; // Ignore CR
25 } else if (received == '\n') {
26 // End of line - process the buffer
27 if (this->buffer_pos_ > 0) {
28 // Null-terminate for safe logging and c_str() use
29 size_t len = this->buffer_pos_;
30 this->buffer_[len] = '\0';
31 this->buffer_pos_ = 0;
32
33 StringRef line(this->buffer_.data(), len);
34 ESP_LOGD(TAG, "Received line: %s", line.c_str());
35
36 // Fire data callbacks for all received lines
37 this->data_callbacks_.call(line);
38
39 // Check if this line is JSON (starts with '{')
40 if (this->buffer_[0] == '{') {
41 ESP_LOGV(TAG, "Line is JSON, parsing...");
42 this->parse_json_(this->buffer_.data(), len);
43 }
44 }
45 } else if (this->buffer_pos_ >= MAX_LINE_LENGTH) {
46 ESP_LOGW(TAG, "Buffer overflow (>%zu bytes), discarding buffer", MAX_LINE_LENGTH);
47 this->buffer_pos_ = 0;
48 } else {
49 this->buffer_[this->buffer_pos_++] = static_cast<char>(received);
50 }
51 }
52}
53
54void EmonTx::parse_json_(const char *data, size_t len) {
55 bool success = json::parse_json(reinterpret_cast<const uint8_t *>(data), len, [this, data, len](JsonObject root) {
56#ifdef USE_SENSOR
57 for (auto &sensor_pair : this->sensors_) {
58 auto val = root[sensor_pair.first];
59 if (val.is<JsonVariant>()) {
60 float value = val;
61 ESP_LOGV(TAG, "Updating sensor '%s' with value: %.2f", sensor_pair.first, value);
62 sensor_pair.second->publish_state(value);
63 }
64 }
65#endif
66
67 this->json_callbacks_.call(root, StringRef(data, len));
68 return true;
69 });
70
71 if (!success) {
72 ESP_LOGW(TAG, "Failed to parse JSON");
73 }
74}
75
80 ESP_LOGCONFIG(TAG, "EmonTx:");
81
82#ifdef USE_SENSOR
83 ESP_LOGCONFIG(TAG, " Registered sensors: %zu", this->sensors_.size());
84 for (const auto &sensor_pair : this->sensors_) {
85 ESP_LOGCONFIG(TAG, " Sensor: %s", sensor_pair.first);
86 }
87#else
88 ESP_LOGCONFIG(TAG, " Sensor support: DISABLED");
89#endif
90}
91
97void EmonTx::send_command(const std::string &command) {
98 ESP_LOGD(TAG, "Sending command to emonTx: %s", command.c_str());
99 this->write_str(command.c_str());
100 this->write_byte('\n');
101}
102
103#ifdef USE_SENSOR
110void EmonTx::register_sensor(const char *tag_name, sensor::Sensor *sensor) {
111 ESP_LOGCONFIG(TAG, "Registering sensor for tag: %s", tag_name);
112 this->sensors_.emplace_back(tag_name, sensor);
113}
114#endif
115
116} // namespace esphome::emontx
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void setup() override
Definition emontx.cpp:9
FixedVector< std::pair< const char *, sensor::Sensor * > > sensors_
Definition emontx.h:53
void loop() override
Implements the main loop for parsing data from the serial port.
Definition emontx.cpp:18
void dump_config() override
Logs the EmonTx component configuration details.
Definition emontx.cpp:79
std::array< char, MAX_LINE_LENGTH+1 > buffer_
Definition emontx.h:58
void send_command(const std::string &command)
Sends a command string to the emonTx device via UART.
Definition emontx.cpp:97
void register_sensor(const char *tag_name, sensor::Sensor *sensor)
Registers a sensor to receive updates for a specific JSON tag.
Definition emontx.cpp:110
LazyCallbackManager< void(JsonObject, StringRef)> json_callbacks_
Definition emontx.h:55
LazyCallbackManager< void(StringRef)> data_callbacks_
Definition emontx.h:56
void parse_json_(const char *data, size_t len)
Definition emontx.cpp:54
Base-class for all sensors.
Definition sensor.h:47
void write_str(const char *str)
Definition uart.h:32
void write_byte(uint8_t data)
Definition uart.h:18
mopeka_std_values val[3]
bool parse_json(const std::string &data, const json_parse_t &f)
Parse a JSON string and run the provided json parse function if it's valid.
Definition json_util.cpp:27
const char int line
Definition log.h:74
std::string size_t len
Definition helpers.h:1045