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

Avoid compile errors in case MQTT is turned off #28

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions espdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ namespace esphome

ESP_LOGI(TAG, "Received valid data");

#ifdef USE_MQTT
if(this->mqtt_client != NULL)
{
this->mqtt_client->publish_json(this->topic, [=](JsonObject root)
Expand Down Expand Up @@ -434,6 +435,7 @@ namespace esphome
}
});
}
#endif // USE_MQTT
}
}

Expand Down Expand Up @@ -495,11 +497,13 @@ namespace esphome
this->timestamp = timestamp;
}

#ifdef USE_MQTT
void DlmsMeter::enable_mqtt(mqtt::MQTTClientComponent *mqtt_client, const char *topic)
{
this->mqtt_client = mqtt_client;
this->topic = topic;
}
#endif // USE_MQTT

void DlmsMeter::log_packet(std::vector<uint8_t> data)
{
Expand Down
9 changes: 6 additions & 3 deletions espdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ namespace esphome
void set_active_energy_sensors(sensor::Sensor *active_energy_plus, sensor::Sensor *active_energy_minus);
void set_reactive_energy_sensors(sensor::Sensor *reactive_energy_plus, sensor::Sensor *reactive_energy_minus);
void set_timestamp_sensor(text_sensor::TextSensor *timestamp);
void set_key(uint8_t key[], size_t keyLength);

#ifdef USE_MQTT
void enable_mqtt(mqtt::MQTTClientComponent *mqtt_client, const char *topic);

void set_key(uint8_t key[], size_t keyLength);
#endif

private:
std::vector<uint8_t> receiveBuffer; // Stores the packet currently being received
Expand Down Expand Up @@ -59,12 +60,14 @@ namespace esphome

text_sensor::TextSensor *timestamp = NULL; // Text sensor for the timestamp value

mqtt::MQTTClientComponent *mqtt_client = NULL;

uint16_t swap_uint16(uint16_t val);
uint32_t swap_uint32(uint32_t val);
void log_packet(std::vector<uint8_t> data);
void abort();
#ifdef USE_MQTT
mqtt::MQTTClientComponent *mqtt_client = NULL;
#endif
};
}
}
160 changes: 160 additions & 0 deletions meter01-noMQTT-HA.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
esphome:
name: meter01
platform: ESP32
board: esp32-poe
includes:
- ./esphome-dlms-meter

ethernet:
type: LAN8720
mdc_pin: GPIO23
mdio_pin: GPIO18
clk_mode: GPIO17_OUT
phy_addr: 0
power_pin: GPIO12
domain: .example.org

# Enable logging
logger:
level: INFO
# tx_buffer_size: 2048 # Only needed when logging large packets

# Enable Home Assistant API if not using MQTT
api:
# password: "1234"
# reboot_timeout: 0s

ota:
password: "1234"

#web_server:
# port: 80

uart:
tx_pin: GPIO4
rx_pin: GPIO36
baud_rate: 2400
rx_buffer_size: 1024 # Needed to receive the large packets send by the smart meter
id: mbus

sensor:
- platform: template
id: meter01_voltage_l1
name: meter01_voltage_l1
unit_of_measurement: V
accuracy_decimals: 1
device_class: "voltage"
state_class: "measurement"
- platform: template
id: meter01_voltage_l2
name: meter01_voltage_l2
unit_of_measurement: V
accuracy_decimals: 1
device_class: "voltage"
state_class: "measurement"
- platform: template
id: meter01_voltage_l3
name: meter01_voltage_l3
unit_of_measurement: V
accuracy_decimals: 1
device_class: "voltage"
state_class: "measurement"

- platform: template
id: meter01_current_l1
name: meter01_current_l1
unit_of_measurement: A
accuracy_decimals: 2
device_class: "current"
state_class: "measurement"
- platform: template
id: meter01_current_l2
name: meter01_current_l2
unit_of_measurement: A
accuracy_decimals: 2
device_class: "current"
state_class: "measurement"
- platform: template
id: meter01_current_l3
name: meter01_current_l3
unit_of_measurement: A
accuracy_decimals: 2
device_class: "current"
state_class: "measurement"

- platform: template
id: meter01_active_power_plus
name: meter01_active_power_plus
unit_of_measurement: W
accuracy_decimals: 0
device_class: "power"
state_class: "measurement"
- platform: template
id: meter01_active_power_minus
name: meter01_active_power_minus
unit_of_measurement: W
accuracy_decimals: 0
device_class: "power"
state_class: "measurement"

- platform: template
id: meter01_active_energy_plus
name: meter01_active_energy_plus
unit_of_measurement: Wh
accuracy_decimals: 0
device_class: "energy"
state_class: "total_increasing"
- platform: template
id: meter01_active_energy_minus
name: meter01_active_energy_minus
unit_of_measurement: Wh
accuracy_decimals: 0
device_class: "energy"
state_class: "total_increasing"

- platform: template
id: meter01_reactive_energy_plus
name: meter01_reactive_energy_plus
unit_of_measurement: Wh
accuracy_decimals: 0
device_class: "energy"
state_class: "total_increasing"
- platform: template
id: meter01_reactive_energy_minus
name: meter01_reactive_energy_minus
unit_of_measurement: Wh
accuracy_decimals: 0
device_class: "energy"
state_class: "total_increasing"

text_sensor:
- platform: template
id: meter01_timestamp
name: meter01_timestamp

#mqtt:
# broker: none
# id: mqtt_broker_off

custom_component:
- lambda: |-

auto dlms_meter = new esphome::espdm::DlmsMeter(id(mbus));

uint8_t key[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
dlms_meter->set_key(key, 16); # Pass your decryption key and key length here

dlms_meter->set_voltage_sensors(id(meter01_voltage_l1), id(meter01_voltage_l2), id(meter01_voltage_l3)); // Set sensors to use for voltage (optional)

dlms_meter->set_current_sensors(id(meter01_current_l1), id(meter01_current_l2), id(meter01_current_l3)); // Set sensors to use for current (optional)

dlms_meter->set_active_power_sensors(id(meter01_active_power_plus), id(meter01_active_power_minus)); // Set sensors to use for active power (optional)

dlms_meter->set_active_energy_sensors(id(meter01_active_energy_plus), id(meter01_active_energy_minus)); // Set sensors to use for active energy (optional)
dlms_meter->set_reactive_energy_sensors(id(meter01_reactive_energy_plus), id(meter01_reactive_energy_minus)); // Set sensors to use for reactive energy (optional)

dlms_meter->set_timestamp_sensor(id(meter01_timestamp)); // Set sensor to use for timestamp (optional)

// dlms_meter->enable_mqtt(id(mqtt_broker), "meter01/data"); // Enable grouped together MQTT report, useful to get exact time with each data for storing results in InfluxDB

return {dlms_meter};