Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mqtt shall be optional #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/sml_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,9 @@ struct Readings {
float total_incoming{0.0f};
float total_outgoing{0.0f};
float frequency{0.0f};

void publishMQTT() const {
std::ostringstream payload;
payload.precision(1);
payload << std::fixed;
payload << "{\"grid\":{\"power\":" << power << ",";
payload << "\"L1\":{\"power\":" << power_l1 << ",\"voltage\":" << voltage_l1 << ",\"current\":" << current_l1
<< "},";
payload << "\"L2\":{\"power\":" << power_l2 << ",\"voltage\":" << voltage_l2 << ",\"current\":" << current_l2
<< "},";
payload << "\"L3\":{\"power\":" << power_l3 << ",\"voltage\":" << voltage_l3 << ",\"current\":" << current_l3
<< "}}}";
id(mqtt_client).publish("homeassistant/energy/grid", payload.str());
}

void publishHA() const {
id(Instantaneous_Power).publish_state(power);
id(Instantaneous_Power_L1).publish_state(power_l1);
id(Instantaneous_Power_L2).publish_state(power_l2);
id(Instantaneous_Power_L3).publish_state(power_l3);
id(Instantaneous_Voltage_L1).publish_state(voltage_l1);
id(Instantaneous_Voltage_L2).publish_state(voltage_l2);
id(Instantaneous_Voltage_L3).publish_state(voltage_l3);
id(Instantaneous_Current_L1).publish_state(current_l1);
id(Instantaneous_Current_L2).publish_state(current_l2);
id(Instantaneous_Current_L3).publish_state(current_l3);
id(Frequency).publish_state(frequency);
id(Total_incoming).publish_state(total_incoming);
id(Total_Outgoing).publish_state(total_outgoing);
}
};

template <bool PublishMQTT>
class SMLReader : public Component, public UARTDevice {
std::vector<std::uint8_t> _buffer;
State _current_state{State::kWaitingForStartSequence};
Expand Down Expand Up @@ -241,13 +212,45 @@ class SMLReader : public Component, public UARTDevice {

void publishReadings() {
if (_readings.has_value()) {
_readings->publishMQTT();
_readings->publishHA();
if constexpr (PublishMQTT) {
publishMQTT(*_readings);
}
publishHA(*_readings);
_readings.reset();
}
ESP_LOGV("SMLReader", "starting over");
}

void publishMQTT(const Readings& readings) const {
std::ostringstream payload;
payload.precision(1);
payload << std::fixed;
payload << "{\"grid\":{\"power\":" << readings.power << ",";
payload << "\"L1\":{\"power\":" << readings.power_l1 << ",\"voltage\":" << readings.voltage_l1
<< ",\"current\":" << readings.current_l1 << "},";
payload << "\"L2\":{\"power\":" << readings.power_l2 << ",\"voltage\":" << readings.voltage_l2
<< ",\"current\":" << readings.current_l2 << "},";
payload << "\"L3\":{\"power\":" << readings.power_l3 << ",\"voltage\":" << readings.voltage_l3
<< ",\"current\":" << readings.current_l3 << "}}}";
id(mqtt_client).publish("homeassistant/energy/grid", payload.str());
}

void publishHA(const Readings& readings) const {
id(Instantaneous_Power).publish_state(readings.power);
id(Instantaneous_Power_L1).publish_state(readings.power_l1);
id(Instantaneous_Power_L2).publish_state(readings.power_l2);
id(Instantaneous_Power_L3).publish_state(readings.power_l3);
id(Instantaneous_Voltage_L1).publish_state(readings.voltage_l1);
id(Instantaneous_Voltage_L2).publish_state(readings.voltage_l2);
id(Instantaneous_Voltage_L3).publish_state(readings.voltage_l3);
id(Instantaneous_Current_L1).publish_state(readings.current_l1);
id(Instantaneous_Current_L2).publish_state(readings.current_l2);
id(Instantaneous_Current_L3).publish_state(readings.current_l3);
id(Frequency).publish_state(readings.frequency);
id(Total_incoming).publish_state(readings.total_incoming);
id(Total_Outgoing).publish_state(readings.total_outgoing);
}

public:
SMLReader(UARTComponent* parent) : UARTDevice(parent) {}

Expand Down
9 changes: 7 additions & 2 deletions yaml/energymeter.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
defaults:
rx_pin: "GPIO2"
mqtt_enabled: "true"

esphome:
includes:
- OneESP32ToRuleThemAll/src/sml_reader.h
Expand All @@ -11,7 +15,7 @@ esphome:
#########################################
uart:
id: smartmeter_uart
rx_pin: GPIO2
rx_pin: ${rx_pin}
baud_rate: 9600

#########################################
Expand All @@ -24,7 +28,7 @@ external_components:

custom_component:
- lambda: |-
auto sml_reader = new SMLReader(id(smartmeter_uart));
auto sml_reader = new SMLReader<${mqtt_enabled}>(id(smartmeter_uart));
return {sml_reader};

#########################################
Expand All @@ -38,6 +42,7 @@ mqtt:
username: !secret mqtt_user
password: !secret mqtt_password
discovery: false
enable_on_boot: ${mqtt_enabled}

#########################################
# #
Expand Down
Loading