added energy reading for pzem004 (#1022)

This commit is contained in:
Adrian Cuzman 2020-05-29 01:54:00 +03:00 committed by GitHub
parent ecd65003d4
commit 7e4d12f880
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View file

@ -59,11 +59,19 @@ void PZEM004T::loop() {
if (this->power_sensor_ != nullptr) if (this->power_sensor_ != nullptr)
this->power_sensor_->publish_state(power); this->power_sensor_->publish_state(power);
ESP_LOGD(TAG, "Got Power %u W", power); ESP_LOGD(TAG, "Got Power %u W", power);
this->write_state_(READ_ENERGY);
break;
}
case 0xA3: { // Energy Response
uint32_t energy = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3]));
if (this->energy_sensor_ != nullptr)
this->energy_sensor_->publish_state(energy);
ESP_LOGD(TAG, "Got Energy %u Wh", energy);
this->write_state_(DONE); this->write_state_(DONE);
break; break;
} }
case 0xA3: // Energy Response
case 0xA5: // Set Power Alarm Response case 0xA5: // Set Power Alarm Response
case 0xB0: // Voltage Request case 0xB0: // Voltage Request
case 0xB1: // Current Request case 0xB1: // Current Request

View file

@ -12,6 +12,7 @@ class PZEM004T : public PollingComponent, public uart::UARTDevice {
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; } void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; } void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
void loop() override; void loop() override;
@ -23,12 +24,14 @@ class PZEM004T : public PollingComponent, public uart::UARTDevice {
sensor::Sensor *voltage_sensor_; sensor::Sensor *voltage_sensor_;
sensor::Sensor *current_sensor_; sensor::Sensor *current_sensor_;
sensor::Sensor *power_sensor_; sensor::Sensor *power_sensor_;
sensor::Sensor *energy_sensor_;
enum PZEM004TReadState { enum PZEM004TReadState {
SET_ADDRESS = 0xB4, SET_ADDRESS = 0xB4,
READ_VOLTAGE = 0xB0, READ_VOLTAGE = 0xB0,
READ_CURRENT = 0xB1, READ_CURRENT = 0xB1,
READ_POWER = 0xB2, READ_POWER = 0xB2,
READ_ENERGY = 0xB3,
DONE = 0x00, DONE = 0x00,
} read_state_{DONE}; } read_state_{DONE};

View file

@ -2,7 +2,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import sensor, uart from esphome.components import sensor, uart
from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \ from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \
UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT CONF_ENERGY, UNIT_VOLT, ICON_FLASH, ICON_COUNTER, UNIT_AMPERE, UNIT_WATT, \
UNIT_WATT_HOURS
DEPENDENCIES = ['uart'] DEPENDENCIES = ['uart']
@ -15,6 +16,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1),
cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2),
cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 0), cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 0),
cv.Optional(CONF_ENERGY): sensor.sensor_schema(UNIT_WATT_HOURS, ICON_COUNTER, 0)
}).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA) }).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA)
@ -35,3 +37,7 @@ def to_code(config):
conf = config[CONF_POWER] conf = config[CONF_POWER]
sens = yield sensor.new_sensor(conf) sens = yield sensor.new_sensor(conf)
cg.add(var.set_power_sensor(sens)) cg.add(var.set_power_sensor(sens))
if CONF_ENERGY in config:
conf = config[CONF_ENERGY]
sens = yield sensor.new_sensor(conf)
cg.add(var.set_energy_sensor(sens))