diff --git a/espdm.cpp b/espdm.cpp index 030c191..7c53e22 100644 --- a/espdm.cpp +++ b/espdm.cpp @@ -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) @@ -434,6 +435,7 @@ namespace esphome } }); } +#endif // USE_MQTT } } @@ -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 data) { diff --git a/espdm.h b/espdm.h index 181c816..0f8e4ed 100644 --- a/espdm.h +++ b/espdm.h @@ -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 receiveBuffer; // Stores the packet currently being received @@ -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 data); void abort(); +#ifdef USE_MQTT + mqtt::MQTTClientComponent *mqtt_client = NULL; +#endif }; } } diff --git a/meter01-noMQTT-HA.example.yaml b/meter01-noMQTT-HA.example.yaml new file mode 100644 index 0000000..f473ef2 --- /dev/null +++ b/meter01-noMQTT-HA.example.yaml @@ -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};