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 |
Thank you very much, I really appreciate it. That's what I'm going to do. But, It would be awesome to find a way to communicate more than one sensor by channel because that limitation restricts the number of sensors to the amount of USB ports (4). Furthermore, It would allow us to reduce costs, it would let the USB ports free for other connections, and in general, it would be a very interesting solution. So, thank you again for your work and for the time to help me.
…________________________________
De: José L. Ortiz ***@***.***>
Enviado: lunes, 4 de julio de 2022 21:46
Para: bandaangosta ***@***.***>
Cc: Felix Rivero Graveran ***@***.***>; Comment ***@***.***>
Asunto: Re: bandaangosta/pzem_004t.py
@bandaangosta commented on this gist.
________________________________
Hello, first of all, thank you for your work. I used your code and it works perfect. In this moment, I want to expand my current work I want to use three PZEM sensor. I looked into the modbus_tk library documentation but it is not clear to me a way to read data from three sensors in the same channel. I would be grateful if you could support me in this project. Thank you very much.
Hi, Felix. I'm glad you found this useful.
If you have 3 sensors communicating over UART (let's say using USB to UART adapters on a PC/Raspberry Pi), then you need 3 separate UART channels (3 USB to UART adapters). Then you will have three sensors defined like this:
sensor1 = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
sensor2 = serial.Serial(
port='/dev/ttyUSB1',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
sensor3 = serial.Serial(
port='/dev/ttyUSB2',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
Then apply modbus initialization and commands on each sensorX object separately.
Cheers.
—
Reply to this email directly, view it on GitHub<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2F134c9d84ae9bd317297e96dcc0b9c860%23gistcomment-4221319&data=05%7C01%7Cf.riverog%40uniandes.edu.co%7C41d6a0e5df63472abed108da5e282b0d%7Cfabd047cff48492a8bbb8f98b9fb9cca%7C0%7C0%7C637925823877395164%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2B7Gu2JxWru7Xe9Iup6AZxM62iEGRe818Ymy%2FUU8eWkg%3D&reserved=0>, or unsubscribe<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYV2H53P3A2LQCLUCPJIN33VSOHW7ANCNFSM4KFCH5SQ&data=05%7C01%7Cf.riverog%40uniandes.edu.co%7C41d6a0e5df63472abed108da5e282b0d%7Cfabd047cff48492a8bbb8f98b9fb9cca%7C0%7C0%7C637925823877395164%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=4SiYLVx2TtxeaW0NLj5vlMdAzmXNf6%2FJuHg3MlW%2FpV8%3D&reserved=0>.
You are receiving this because you commented.Message ID: ***@***.***>
Hi,
I used current measurement devices from
https://sbc-support.com/fileadmin/user_upload/tx_srcproducts/Catalogues/26-215_ENG18d_SBC-SystemCatalogue_WEB.pdf
You give every device a different Modbus address and only need one USB
adapter.
S.
Am Di., 5. Juli 2022 um 05:02 Uhr schrieb FelixRG161100 <
***@***.***>:
… ***@***.**** commented on this gist.
------------------------------
Thank you very much, I really appreciate it. That's what I'm going to do.
But, It would be awesome to find a way to communicate more than one sensor
by channel because that limitation restricts the number of sensors to the
amount of USB ports (4). Furthermore, It would allow us to reduce costs, it
would let the USB ports free for other connections, and in general, it
would be a very interesting solution. So, thank you again for your work and
for the time to help me.
________________________________
De: José L. Ortiz ***@***.***>
Enviado: lunes, 4 de julio de 2022 21:46
Para: bandaangosta ***@***.***>
Cc: Felix Rivero Graveran ***@***.***>; Comment ***@***.***>
Asunto: Re: bandaangosta/pzem_004t.py
@bandaangosta commented on this gist.
________________________________
Hello, first of all, thank you for your work. I used your code and it
works perfect. In this moment, I want to expand my current work I want to
use three PZEM sensor. I looked into the modbus_tk library documentation
but it is not clear to me a way to read data from three sensors in the same
channel. I would be grateful if you could support me in this project. Thank
you very much.
Hi, Felix. I'm glad you found this useful.
If you have 3 sensors communicating over UART (let's say using USB to UART
adapters on a PC/Raspberry Pi), then you need 3 separate UART channels (3
USB to UART adapters). Then you will have three sensors defined like this:
sensor1 = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
sensor2 = serial.Serial(
port='/dev/ttyUSB1',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
sensor3 = serial.Serial(
port='/dev/ttyUSB2',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
Then apply modbus initialization and commands on each sensorX object
separately.
Cheers.
—
Reply to this email directly, view it on GitHub<
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2F134c9d84ae9bd317297e96dcc0b9c860%23gistcomment-4221319&data=05%7C01%7Cf.riverog%40uniandes.edu.co%7C41d6a0e5df63472abed108da5e282b0d%7Cfabd047cff48492a8bbb8f98b9fb9cca%7C0%7C0%7C637925823877395164%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2B7Gu2JxWru7Xe9Iup6AZxM62iEGRe818Ymy%2FUU8eWkg%3D&reserved=0>,
or unsubscribe<
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYV2H53P3A2LQCLUCPJIN33VSOHW7ANCNFSM4KFCH5SQ&data=05%7C01%7Cf.riverog%40uniandes.edu.co%7C41d6a0e5df63472abed108da5e282b0d%7Cfabd047cff48492a8bbb8f98b9fb9cca%7C0%7C0%7C637925823877395164%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=4SiYLVx2TtxeaW0NLj5vlMdAzmXNf6%2FJuHg3MlW%2FpV8%3D&reserved=0
>.
You are receiving this because you commented.Message ID: ***@***.***>
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/134c9d84ae9bd317297e96dcc0b9c860#gistcomment-4221361>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIBALPOT2K6HUB4LRF6UW3TVSOQWDANCNFSM4KFCH5SQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Wow, thanks connermacleod69!! That's great info to have at hand. Would you mind sharing what model(s) from the catalog you have used?
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:
***@***.***>
Merci @bandaangosta !
this only print the data once right and not a loop? what does the .close() method do?
this only print the data once right and not a loop?
Hi, @FujiwaraKengo . That is correct, data is printed out only once and then the scripts ends, closing the communication channel. You could put the reading part in a loop (lines 27 to 43), allowing for a reasonable waiting time between iterations, or call the full script periodically.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Felix. I'm glad you found this useful.
If you have 3 sensors communicating over UART (let's say using USB to UART adapters on a PC/Raspberry Pi), then you need 3 separate UART channels (3 USB to UART adapters). Then you will have three sensors defined like this:
The
/dev/ttyUSBX
definition for the assigned ports is just an example. This may vary on your system.Then apply modbus initialization and commands on each sensorX object separately.
Cheers.