mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-06 04:48:27 +02:00
Add PZEM004T/PZEMAC/PZEMDC Support (#587)
* Add PZEM004T Support * Don't flush as much * Update pzem004t.cpp * Add generalized modbus * Add PZEMAC * Add PZEMDC * Fix file modes * Lint * Fix * Fix * Add check_uart_settings
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#include "pzem004t.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pzem004t {
|
||||
|
||||
static const char *TAG = "pzem004t";
|
||||
|
||||
void PZEM004T::loop() {
|
||||
const uint32_t now = millis();
|
||||
if (now - this->last_read_ > 500 && this->available()) {
|
||||
while (this->available())
|
||||
this->read();
|
||||
this->last_read_ = now;
|
||||
}
|
||||
|
||||
// PZEM004T packet size is 7 byte
|
||||
while (this->available() >= 7) {
|
||||
auto resp = *this->read_array<7>();
|
||||
// packet format:
|
||||
// 0: packet type
|
||||
// 1-5: data
|
||||
// 6: checksum (sum of other bytes)
|
||||
// see https://github.com/olehs/PZEM004T
|
||||
uint8_t sum = 0;
|
||||
for (int i = 0; i < 6; i++)
|
||||
sum += resp[i];
|
||||
|
||||
if (sum != resp[6]) {
|
||||
ESP_LOGV(TAG, "PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (resp[0]) {
|
||||
case 0xA4: { // Set Module Address Response
|
||||
this->write_state_(READ_VOLTAGE);
|
||||
break;
|
||||
}
|
||||
case 0xA0: { // Voltage Response
|
||||
uint16_t int_voltage = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
|
||||
float voltage = int_voltage + (resp[3] / 10.0f);
|
||||
if (this->voltage_sensor_ != nullptr)
|
||||
this->voltage_sensor_->publish_state(voltage);
|
||||
ESP_LOGD(TAG, "Got Voltage %.1f V", voltage);
|
||||
this->write_state_(READ_CURRENT);
|
||||
break;
|
||||
}
|
||||
case 0xA1: { // Current Response
|
||||
uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
|
||||
float current = int_current + (resp[3] / 100.0f);
|
||||
if (this->current_sensor_ != nullptr)
|
||||
this->current_sensor_->publish_state(current);
|
||||
ESP_LOGD(TAG, "Got Current %.2f A", current);
|
||||
this->write_state_(READ_POWER);
|
||||
break;
|
||||
}
|
||||
case 0xA2: { // Active Power Response
|
||||
uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
|
||||
if (this->power_sensor_ != nullptr)
|
||||
this->power_sensor_->publish_state(power);
|
||||
ESP_LOGD(TAG, "Got Power %u W", power);
|
||||
this->write_state_(DONE);
|
||||
break;
|
||||
}
|
||||
|
||||
case 0xA3: // Energy Response
|
||||
case 0xA5: // Set Power Alarm Response
|
||||
case 0xB0: // Voltage Request
|
||||
case 0xB1: // Current Request
|
||||
case 0xB2: // Active Power Response
|
||||
case 0xB3: // Energy Request
|
||||
case 0xB4: // Set Module Address Request
|
||||
case 0xB5: // Set Power Alarm Request
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
this->last_read_ = now;
|
||||
}
|
||||
}
|
||||
void PZEM004T::update() { this->write_state_(SET_ADDRESS); }
|
||||
void PZEM004T::write_state_(PZEM004T::PZEM004TReadState state) {
|
||||
if (state == DONE) {
|
||||
this->read_state_ = state;
|
||||
return;
|
||||
}
|
||||
std::array<uint8_t, 7> data{};
|
||||
data[0] = state;
|
||||
data[1] = 192;
|
||||
data[2] = 168;
|
||||
data[3] = 1;
|
||||
data[4] = 1;
|
||||
data[5] = 0;
|
||||
data[6] = 0;
|
||||
for (int i = 0; i < 6; i++)
|
||||
data[6] += data[i];
|
||||
|
||||
this->write_array(data);
|
||||
this->read_state_ = state;
|
||||
}
|
||||
|
||||
} // namespace pzem004t
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pzem004t {
|
||||
|
||||
class PZEM004T : public PollingComponent, public uart::UARTDevice {
|
||||
public:
|
||||
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_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
||||
|
||||
void loop() override;
|
||||
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
sensor::Sensor *voltage_sensor_;
|
||||
sensor::Sensor *current_sensor_;
|
||||
sensor::Sensor *power_sensor_;
|
||||
|
||||
enum PZEM004TReadState {
|
||||
SET_ADDRESS = 0xB4,
|
||||
READ_VOLTAGE = 0xB0,
|
||||
READ_CURRENT = 0xB1,
|
||||
READ_POWER = 0xB2,
|
||||
DONE = 0x00,
|
||||
} read_state_{DONE};
|
||||
|
||||
void write_state_(PZEM004TReadState state);
|
||||
|
||||
uint32_t last_read_{0};
|
||||
};
|
||||
|
||||
} // namespace pzem004t
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,37 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, uart
|
||||
from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \
|
||||
UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT
|
||||
|
||||
DEPENDENCIES = ['uart']
|
||||
|
||||
pzem004t_ns = cg.esphome_ns.namespace('pzem004t')
|
||||
PZEM004T = pzem004t_ns.class_('PZEM004T', cg.PollingComponent, uart.UARTDevice)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(PZEM004T),
|
||||
|
||||
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_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 0),
|
||||
}).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield uart.register_uart_device(var, config)
|
||||
|
||||
if CONF_VOLTAGE in config:
|
||||
conf = config[CONF_VOLTAGE]
|
||||
sens = yield sensor.new_sensor(conf)
|
||||
cg.add(var.set_voltage_sensor(sens))
|
||||
if CONF_CURRENT in config:
|
||||
conf = config[CONF_CURRENT]
|
||||
sens = yield sensor.new_sensor(conf)
|
||||
cg.add(var.set_current_sensor(sens))
|
||||
if CONF_POWER in config:
|
||||
conf = config[CONF_POWER]
|
||||
sens = yield sensor.new_sensor(conf)
|
||||
cg.add(var.set_power_sensor(sens))
|
||||
Reference in New Issue
Block a user