mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-29 22:24:26 +00: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
									
								
								esphome/components/pzemac/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/pzemac/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										62
									
								
								esphome/components/pzemac/pzemac.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								esphome/components/pzemac/pzemac.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| #include "pzemac.h" | ||||
| #include "esphome/core/log.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace pzemac { | ||||
|  | ||||
| static const char *TAG = "pzemac"; | ||||
|  | ||||
| static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04; | ||||
| static const uint8_t PZEM_REGISTER_COUNT = 10;  // 10x 16-bit registers | ||||
|  | ||||
| void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) { | ||||
|   if (data.size() < 20) { | ||||
|     ESP_LOGW(TAG, "Invalid size for PZEM AC!"); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // See https://github.com/esphome/feature-requests/issues/49#issuecomment-538636809 | ||||
|   //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ||||
|   // 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34 | ||||
|   // Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc-- | ||||
|  | ||||
|   auto pzem_get_16bit = [&](size_t i) -> uint16_t { | ||||
|     return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0); | ||||
|   }; | ||||
|   auto pzem_get_32bit = [&](size_t i) -> uint32_t { | ||||
|     return (uint32_t(pzem_get_16bit(i + 2)) << 16) | (uint32_t(pzem_get_16bit(i + 0)) << 0); | ||||
|   }; | ||||
|  | ||||
|   uint16_t raw_voltage = pzem_get_16bit(0); | ||||
|   float voltage = raw_voltage / 10.0f;  // max 6553.5 V | ||||
|  | ||||
|   uint32_t raw_current = pzem_get_32bit(2); | ||||
|   float current = raw_current / 1000.0f;  // max 4294967.295 A | ||||
|  | ||||
|   uint32_t raw_active_power = pzem_get_32bit(6); | ||||
|   float active_power = raw_active_power / 10.0f;  // max 429496729.5 W | ||||
|  | ||||
|   uint16_t raw_frequency = pzem_get_16bit(14); | ||||
|   float frequency = raw_frequency / 10.0f; | ||||
|  | ||||
|   uint16_t raw_power_factor = pzem_get_16bit(16); | ||||
|   float power_factor = raw_power_factor / 100.0f; | ||||
|  | ||||
|   ESP_LOGD(TAG, "PZEM AC: V=%.1f V, I=%.3f A, P=%.1f W, F=%.1f Hz, PF=%.2f", voltage, current, active_power, frequency, | ||||
|            power_factor); | ||||
|   if (this->voltage_sensor_ != nullptr) | ||||
|     this->voltage_sensor_->publish_state(voltage); | ||||
|   if (this->current_sensor_ != nullptr) | ||||
|     this->current_sensor_->publish_state(current); | ||||
|   if (this->power_sensor_ != nullptr) | ||||
|     this->power_sensor_->publish_state(active_power); | ||||
|   if (this->frequency_sensor_ != nullptr) | ||||
|     this->frequency_sensor_->publish_state(frequency); | ||||
|   if (this->power_factor_sensor_ != nullptr) | ||||
|     this->power_factor_sensor_->publish_state(power_factor); | ||||
| } | ||||
|  | ||||
| void PZEMAC::update() { this->send(PZEM_CMD_READ_IN_REGISTERS, 0, PZEM_REGISTER_COUNT); } | ||||
|  | ||||
| }  // namespace pzemac | ||||
| }  // namespace esphome | ||||
							
								
								
									
										31
									
								
								esphome/components/pzemac/pzemac.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								esphome/components/pzemac/pzemac.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/components/sensor/sensor.h" | ||||
| #include "esphome/components/modbus/modbus.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace pzemac { | ||||
|  | ||||
| class PZEMAC : public PollingComponent, public modbus::ModbusDevice { | ||||
|  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 set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; } | ||||
|   void set_power_factor_sensor(sensor::Sensor *power_factor_sensor) { power_factor_sensor_ = power_factor_sensor; } | ||||
|  | ||||
|   void update() override; | ||||
|  | ||||
|   void on_modbus_data(const std::vector<uint8_t> &data) override; | ||||
|  | ||||
|  protected: | ||||
|   sensor::Sensor *voltage_sensor_; | ||||
|   sensor::Sensor *current_sensor_; | ||||
|   sensor::Sensor *power_sensor_; | ||||
|   sensor::Sensor *frequency_sensor_; | ||||
|   sensor::Sensor *power_factor_sensor_; | ||||
| }; | ||||
|  | ||||
| }  // namespace pzemac | ||||
| }  // namespace esphome | ||||
							
								
								
									
										47
									
								
								esphome/components/pzemac/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								esphome/components/pzemac/sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import sensor, modbus | ||||
| from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \ | ||||
|     CONF_FREQUENCY, UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT, UNIT_EMPTY, \ | ||||
|     ICON_POWER, CONF_POWER_FACTOR, ICON_CURRENT_AC | ||||
|  | ||||
| AUTO_LOAD = ['modbus'] | ||||
|  | ||||
| pzemac_ns = cg.esphome_ns.namespace('pzemac') | ||||
| PZEMAC = pzemac_ns.class_('PZEMAC', cg.PollingComponent, modbus.ModbusDevice) | ||||
|  | ||||
| CONFIG_SCHEMA = cv.Schema({ | ||||
|     cv.GenerateID(): cv.declare_id(PZEMAC), | ||||
|     cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), | ||||
|     cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_CURRENT_AC, 3), | ||||
|     cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_POWER, 1), | ||||
|     cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(UNIT_EMPTY, ICON_CURRENT_AC, 1), | ||||
|     cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(UNIT_EMPTY, ICON_FLASH, 2), | ||||
| }).extend(cv.polling_component_schema('60s')).extend(modbus.modbus_device_schema(0x01)) | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     yield cg.register_component(var, config) | ||||
|     yield modbus.register_modbus_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)) | ||||
|     if CONF_FREQUENCY in config: | ||||
|         conf = config[CONF_FREQUENCY] | ||||
|         sens = yield sensor.new_sensor(conf) | ||||
|         cg.add(var.set_frequency_sensor(sens)) | ||||
|     if CONF_POWER_FACTOR in config: | ||||
|         conf = config[CONF_POWER_FACTOR] | ||||
|         sens = yield sensor.new_sensor(conf) | ||||
|         cg.add(var.set_power_factor_sensor(sens)) | ||||
		Reference in New Issue
	
	Block a user