Reading PZEM-004t power sensor (new version v3.0) through Modbus-RTU protocol over TTL UART
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reading PZEM-004t power sensor (new version v3.0) through Modbus-RTU protocol over TTL UART | |
# Run as: | |
# python3 pzem_004t.py | |
# To install dependencies: | |
# pip install modbus-tk | |
# pip install pyserial | |
import serial | |
import modbus_tk.defines as cst | |
from modbus_tk import modbus_rtu | |
# Connect to the sensor | |
sensor = serial.Serial( | |
port='/dev/PZEM_sensor', | |
baudrate=9600, | |
bytesize=8, | |
parity='N', | |
stopbits=1, | |
xonxoff=0 | |
) | |
master = modbus_rtu.RtuMaster(sensor) | |
master.set_timeout(2.0) | |
master.set_verbose(True) | |
data = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10) | |
voltage = data[0] / 10.0 # [V] | |
current = (data[1] + (data[2] << 16)) / 1000.0 # [A] | |
power = (data[3] + (data[4] << 16)) / 10.0 # [W] | |
energy = data[5] + (data[6] << 16) # [Wh] | |
frequency = data[7] / 10.0 # [Hz] | |
powerFactor = data[8] / 100.0 | |
alarm = data[9] # 0 = no alarm | |
print('Voltage [V]: ', voltage) | |
print('Current [A]: ', current) | |
print('Power [W]: ', power) # active power (V * I * power factor) | |
print('Energy [Wh]: ', energy) | |
print('Frequency [Hz]: ', frequency) | |
print('Power factor []: ', powerFactor) | |
print('Alarm : ', alarm) | |
# Changing power alarm value to 100 W | |
# master.execute(1, cst.WRITE_SINGLE_REGISTER, 1, output_value=100) | |
try: | |
master.close() | |
if sensor.is_open: | |
sensor.close() | |
except: | |
pass |
Saia PCD ALD1B5F
…On Tue, Jul 5, 2022, 19:39 José L. Ortiz ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Wow, thanks connermacleod69 <https://gist.github.com/connermacleod69>!!
That's great info to have at hand. Would you mind sharing what model(s)
from the catalog you have used?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/134c9d84ae9bd317297e96dcc0b9c860#gistcomment-4222149>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIBALPJYJFNZBJGYMRFY2PLVSRXL5ANCNFSM4KFCH5SQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, thanks connermacleod69!! That's great info to have at hand. Would you mind sharing what model(s) from the catalog you have used?