medtech1
Posts: 1
Joined: Wed Mar 14, 2018 1:20 pm

Re: Peaceview PZEM-004T Energy Module

Wed Mar 14, 2018 1:34 pm

I just wanted to say that given these PZEM* energy modules are among the least expensive ones available (less than $20 each), using them with the raspberry pi has quite a bit of potential.

Also, given that the purpose of a forum like this is for users to share their ideas an projects, so that others can benefit it would be great if some have success in getting the PZEM modules working well with the RP, to provide enough details to enable success for others to also achieve the same goal.

Many of the energy modules available, require internet connections to clouds, etc (ick), and are very expensive. Has anyone seen PI projects using these PZEM energy modules that provide all the technical detail for others who want to follow in the same footsteps without having show-stopper issues.

Just wondering :)

Dr.No
Posts: 1
Joined: Fri Sep 07, 2018 3:58 pm
Location: South France

Re: Peaceview PZEM-004T Energy Module

Fri Sep 07, 2018 4:45 pm

@ Massi & ilovemipi

I am a beginner with Python and Raspberry pi and I want to connect it directly to the PZEM-004T. I took a look at the code of Massi but it is rather hermetic for me who begins. Some comments on each line of code would be a great help for me.

Regarding the TTL output of PZEM-004T (I have not bought it yet, I'll see this in October). However, on the images I see that the mains isolation is made by 2 optocouplers, (one for Rx the other for Tx).
This means no worries for Tx since it is Vdd which feeds and which is provided by the Pi (3v3 for GPIOs).
For the input Rx I do not think there is any problem with a GPIO which delivers 3.3v otherwise it will be necessary to lower the value of the resistor which is in series with the photocoupler diode.

I'll be back in end of October Probably with a long list of questions. :D

Greening
Posts: 2
Joined: Thu Dec 13, 2018 3:33 am

Re: Peaceview PZEM-004T Energy Module

Thu Dec 13, 2018 3:42 am

Massi wrote:
Tue Mar 08, 2016 8:48 am
PPPS: i've removed from the code the debug and logging code, so maybe i left some typo or formal mistake..

Code: Select all


.....

	def readVoltage(self):
		self.ser.write(serial.to_bytes(self.readVoltageBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				tension = unpacked[2]+unpacked[3]/10.0
				return tension
		else:
			raise serial.SerialTimeoutException("Timeout reading tension")
.....
Hello!
Working good, thanks!
But when voltage more than 256Volt, the device returns the code eg
A0 01 00 00 00 00 A1
0,1 Volt
But real voltage 256,1 Volt

Can you modify code? Eg, if voltage<50v, then return = voltage + 256?
Thanks!

Greening
Posts: 2
Joined: Thu Dec 13, 2018 3:33 am

Re: Peaceview PZEM-004T Energy Module

Thu Dec 13, 2018 3:43 am

Massi wrote:
Tue Mar 08, 2016 8:48 am
PPPS: i've removed from the code the debug and logging code, so maybe i left some typo or formal mistake..

Code: Select all


.....

	def readVoltage(self):
		self.ser.write(serial.to_bytes(self.readVoltageBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				tension = unpacked[2]+unpacked[3]/10.0
				return tension
		else:
			raise serial.SerialTimeoutException("Timeout reading tension")
.....
Hello!
Working good, thanks!
But when voltage more than 256Volt, the device returns the code eg
A0 01 00 00 00 00 A1
0,1 Volt
But real voltage 256,1 Volt

Can you modify code? Eg, if voltage<50v, then return = voltage + 256?
Thanks!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12034
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Peaceview PZEM-004T Energy Module

Tue Mar 05, 2019 6:34 pm

ilovemipi wrote:
Sun Jan 07, 2018 5:39 pm
It's 5V but we have found that the "3.3V" ESP8266's are fine. Probably not safe with a Pi without a voltage divider though.
Immediately remove your signature please, it considered spam, if you don't I will start deleting your posts!

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23083
Joined: Sat Jul 30, 2011 7:41 pm

Re: Peaceview PZEM-004T Energy Module

Wed Mar 06, 2019 9:31 am

mahjongg wrote:
Tue Mar 05, 2019 6:34 pm
ilovemipi wrote:
Sun Jan 07, 2018 5:39 pm
It's 5V but we have found that the "3.3V" ESP8266's are fine. Probably not safe with a Pi without a voltage divider though.
Immediately remove your signature please, it considered spam, if you don't I will start deleting your posts!
Done by admin.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

bandaangosta
Posts: 3
Joined: Tue Jun 14, 2016 4:57 pm

Re: Peaceview PZEM-004T Energy Module

Mon Jul 08, 2019 7:00 pm

For those of you coming with a brand new PZEM-004t module, be advised that communications protocol has changed to Modbus-RTU over UART. This means that, very sadly, widely shared code by Massi no longer works.
It's actually simpler now for the final user, as there are modbus libraries for Python available that handle the lower-level parts.

Here is a working example (also as a gist). It is running on a Raspberry Pi 3 using a USB-UART dongle:

Code: Select all

# 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 slave
serial = serial.Serial(
                       port='/dev/PZEM_sensor',
                       baudrate=9600,
                       bytesize=8,
                       parity='N',
                       stopbits=1,
                       xonxoff=0
                      )

master = modbus_rtu.RtuMaster(serial)
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)
print('Energy [Wh]: ', energy)
print('Frequency [Hz]: ', frequency)
print('Power factor []: ', powerFactor)
print('Alarm : ', alarm)

# Changing alarm W value
# master.execute(1, cst.WRITE_SINGLE_REGISTER, 1, output_value=100)

try:
    master.close()
    if slave.is_open:
        slave.close()
except:
    pass
Hope it helps.
Twitter: @bandaangosta

Return to “Automation, sensing and robotics”