DaveSemm
Posts: 162
Joined: Thu Jun 21, 2012 1:16 pm

Peaceview PZEM-004T Energy Module

Mon Nov 02, 2015 8:56 pm

I wanted to add energy monitoring to my home automation setup, without too much custom hardware, but it had to be relatively cheap. I found this module on Alibaba. It is about $10, and lets you measure voltage (80 ~ 260VAC), current (up to 100A!), power and energy. It can store the accumulated energy if the power goes off, so it can be used as a smart meter.

The documentation is terrible, but by trial and error I finally got it to work. (An hour ago I was going to ask for help.) It still needs some work but I can read voltage now and when I resume tomorrow I'll be able to read current, power and energy.

I'm just curious if anyone else has used this module & if it is accurate, if it works reliably, etc.

DaveSemm
Posts: 162
Joined: Thu Jun 21, 2012 1:16 pm

Re: Peaceview PZEM-004T Energy Module

Wed Nov 04, 2015 8:11 am

The module works well. I didn't realize that it stores the energy consumption on its own flash - I assumed my code would have to store it for when the power goes off. I did a quick test app (it comes with no software), and after figuring out how to get python to read and format a byte stream, it worked:

pi@Pi-LAN-84-F2 ~/pyserial-2.7 $ python testserial.py
Volts Amps W Wh
233.6 0.0 0 128
230.7 0.0 1542 129 --> Turn kettle on
223.2 7.52 1662 130
223.2 7.42 1649 132
223.1 7.38 1644 133
223.3 7.38 1632 134
222.0 7.36 1636 136
222.0 7.36 1634 137
222.5 7.36 1636 139
222.2 7.36 1639 140
222.4 7.37 1636 141
222.4 7.36 1634 143
222.2 7.37 1637 144
222.4 7.37 1635 145
232.6 5.24 1636 145 --> Kettle turned off
232.6 0.0 0 145

(I was a bit surprised at the 10V drop when I turned on the kettle! Also, you might notice that VxA is not exactly W. I don't know if this is because I do the readings a second apart, or because of power factor, or because of something else. I'll look into it.)

So far, I'm happy with this unit.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Wed Nov 04, 2015 8:40 am

I WANT THIS!
:)

is serial link something can cover up to ten meters? i'd like to measure power consumption in my home, so i have to put this in the derivation box..

There's something wrong with your readings..

ie:
230.7 0.0 1542 129

the third number is not A*V :) it seems calculated at a different time..

even here
223.2 7.52 1662 130

1662 is not exactly A*V..

need to study this :) where is the cos(fi)?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Wed Nov 04, 2015 10:05 am

so it seems that the third nuber is active power so cos(fi) is inside it.. and that the last number is the sum of energy used.. quite useless, isn't it?
and i fear that you can't get a "full" reading of the sensor (so current+voltage+active power at the same time), since you have to do 3 different requests to the sensor.
Am i wrong?

btw i'll get one as soon as possible :)

are you going to share your code?

DaveSemm
Posts: 162
Joined: Thu Jun 21, 2012 1:16 pm

Re: Peaceview PZEM-004T Energy Module

Thu Nov 05, 2015 9:10 am

I will share the code soon; it's just a hack at this point - all it does is read & print. I need to tidy it up and make it into a module I can call from an app that will display the values in real time on an LCD display, and publish it on a web service.

For whatever reason, I put a 1 second delay between each request, so it is possible that the V & A values changed enough to give a different W value. I'll do it again tonight without the delays and see if it comes out exactly right.

I'm also going to check if I can increase the length of the wire to the pickup coil without affecting the measurements. My "control centre" is about 5 meters away from the DB board. Alternatively, I'll mount the module in a case next to the DB board and increase the length of the USB cable to the Pi.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Fri Nov 06, 2015 8:59 am

DaveSemm wrote:I will share the code soon; it's just a hack at this point - all it does is read & print. I need to tidy it up and make it into a module I can call from an app that will display the values in real time on an LCD display, and publish it on a web service.
I'm available to help you in any step, if you want (and if i'm able LOL)
my cc has just been cloned so i can't buy it atm, but i'll take it asap :)
increase the length of the USB cable to the Pi.
have you tried connecting directly the cables to TX RX gpios?

Edit to add:
probably the third value will never be exactly V*I.
first because you'll never be able to get reading in the exact same time.
Second because it seems to me that it's the "active power", so it is V*I*cos(fi) (and this is where you can find the power factor, indeed)

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Thu Dec 03, 2015 4:15 pm

Strange question: to give wireless function to the sensor, is it enough to connect one of these?

http://www.dx.com/p/jy-mcu-arduino-blue ... mBpnHYvfIU

and power, ok, but is this working even as a slave device (so can i connect to the sensor via bluetooth)?

suso-mc
Posts: 2
Joined: Mon Mar 07, 2016 9:43 pm

Re: Peaceview PZEM-004T Energy Module

Mon Mar 07, 2016 9:51 pm

I'm going crazy whith this device. Can I take a look to your code?
Thanks

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Tue Mar 08, 2016 8:48 am

suso-mc wrote:I'm going crazy whith this device. Can I take a look to your code?
Thanks
if you are trying to read it with the raspi UART.. well, i fear you have to give up..
Otherwise, here is my full "python3" code

PS: remember that even if you give power on the "serial side" of the device, it will work only if it's connected to mains..

PPS: my code is user on a bluetooth com port, that's why i use the "set address" command to check if the sensor is ready (it takes a while for the bt connection to go up). Remeber to change the com port accordingly to your setup

PPPS: i've removed from the code the debug and logging code, so maybe i left some typo or formal mistake..

Code: Select all

#!/usr/bin/env python3 
#coding=utf-8

import serial
import time
import struct

class BTPOWER:

	setAddrBytes 		=	[0xB4,0xC0,0xA8,0x01,0x01,0x00,0x1E]
	readVoltageBytes 	= 	[0xB0,0xC0,0xA8,0x01,0x01,0x00,0x1A]
	readCurrentBytes 	= 	[0XB1,0xC0,0xA8,0x01,0x01,0x00,0x1B]
	readPowerBytes 		= 	[0XB2,0xC0,0xA8,0x01,0x01,0x00,0x1C]
	readRegPowerBytes 	= 	[0XB3,0xC0,0xA8,0x01,0x01,0x00,0x1D]

	def __init__(self, com="/dev/rfcomm0", timeout=10.0):
		self.ser = serial.Serial(
			port=com,
			baudrate=9600,
			parity=serial.PARITY_NONE,
			stopbits=serial.STOPBITS_ONE,
			bytesize=serial.EIGHTBITS,
			timeout = timeout
		)
		if self.ser.isOpen():
			self.ser.close()
		self.ser.open()

	def checkChecksum(self, _tuple):
		_list = list(_tuple)
		_checksum = _list[-1]
		_list.pop()
		_sum = sum(_list)
		if _checksum == _sum%256:
			return True
		else:
			raise Exception("Wrong checksum")
			
	def isReady(self):
		self.ser.write(serial.to_bytes(self.setAddrBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				return True
		else:
			raise serial.SerialTimeoutException("Timeout setting address")

	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")

	def readCurrent(self):
		self.ser.write(serial.to_bytes(self.readCurrentBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				current = unpacked[2]+unpacked[3]/100.0
				return current
		else:
			raise serial.SerialTimeoutException("Timeout reading current")

	def readPower(self):
		self.ser.write(serial.to_bytes(self.readPowerBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				power = unpacked[1]*256+unpacked[2]
				return power
		else:
			raise serial.SerialTimeoutException("Timeout reading power")

	def readRegPower(self):
		self.ser.write(serial.to_bytes(self.readRegPowerBytes))
		rcv = self.ser.read(7)
		if len(rcv) == 7:
			unpacked = struct.unpack("!7B", rcv)
			if(self.checkChecksum(unpacked)):
				regPower = unpacked[1]*256*256+unpacked[2]*256+unpacked[3]
				return regPower
		else:
			raise serial.SerialTimeoutException("Timeout reading registered power")

	def readAll(self):
		if(self.isReady()):
			return(self.readVoltage(),self.readCurrent(),self.readPower(),self.readRegPower())

	def close(self):
		self.ser.close()

		
if __name__ == "__main__":
	try:
		sensor = BTPOWER()
		print("Checking readiness")
		print(sensor.isReady())
		print("Reading voltage")
		print(sensor.readVoltage())
		print("Reading current")
		print(sensor.readCurrent())
		print("Reading power")
		print(sensor.readPower())
		print("reading registered power")
		print(sensor.readRegPower())
		print("reading all")
		print(sensor.readAll())
	finally:
		sensor.close()

suso-mc
Posts: 2
Joined: Mon Mar 07, 2016 9:43 pm

Re: Peaceview PZEM-004T Energy Module

Tue Mar 08, 2016 10:16 pm

Thanks. I'll try to use yor code (modifying some items) in my system. I`ll notice you if it works.
Salu2

samashazli
Posts: 1
Joined: Mon May 22, 2017 11:09 am

Re: Peaceview PZEM-004T Energy Module

Mon May 22, 2017 11:21 am

Can someone tell me why the pzem gets random 0 values on current, voltage and energy?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Mon May 22, 2017 12:17 pm

samashazli wrote:Can someone tell me why the pzem gets random 0 values on current, voltage and energy?
mine does not

antonysalvin
Posts: 4
Joined: Sun May 28, 2017 8:42 am

Re: Peaceview PZEM-004T Energy Module

Sun May 28, 2017 8:48 am

Hi Massi,

Changed my com port to ttyUSB0 after finding the com port. I've only changed the com port as you instructed. I'm getting the following error.
Traceback (most recent call last):
File "energy.pyw", line 107 in <module>
print(sensor.readVoltage())
file "energy.pyw", line 58, in readVoltage
raise serial.SerialTimeoutException("Timeout reading tension")
serial.SerialTimeoutException("Timeout reading tension")


Please help me out

neelamnaveen86
Posts: 1
Joined: Mon Jul 17, 2017 5:49 am

Re: Peaceview PZEM-004T Energy Module

Mon Jul 17, 2017 5:58 am

hi massi i am having hard time getting this thing to work with my raspberry pi i am tried connecting using UART, USB to TTL but nothing works given the limited documentation its hard to troubleshoot it so i kindly request you to please provide complete wiring diagram to mains in detail and also please explain if this works with USB to ttl

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Mon Jul 17, 2017 2:52 pm

hello,
is this of any help for your wiring?

https://www.circuitspecialists.com/cont ... /ac004.pdf

as far as i can remember (sorry but my pzem04 is left alone with a remote bluetooth serial dongle inside the electrical panel and i have no access to it..) i could not get it to work with the serial port of the raspberry, i had to use a dedicated USB to serial adapter, so yes: it should work.
But i can also remember i struggled a lot before getting it to work :)

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Fri Jan 05, 2018 1:47 pm

ilovemipi wrote:
Fri Jan 05, 2018 1:21 pm
I have been using the Peacefair Pzem-004T for the last year or so and have found them to be very reliable.
So far I have only used them with ESP8266's but moving forward I might hook it up to a Pi.
Yes, they are.
I've been using it for a while too, but with a HC-06 module.
The only thing i hate of this little and cheap thing is that you can't have a simultaneous reading of data.
You have to make sequential serial call for current - voltage - power so they are never read at the very same time.

antonysalvin
Posts: 4
Joined: Sun May 28, 2017 8:42 am

Re: Peaceview PZEM-004T Energy Module

Fri Jan 05, 2018 2:24 pm

Hi ilovemipi,

Can you share detail line by line for in python for adding pzem004t to raspberry pi (including wiring).
I tired several time and is never able to succeed :cry:

antonysalvin
Posts: 4
Joined: Sun May 28, 2017 8:42 am

Re: Peaceview PZEM-004T Energy Module

Fri Jan 05, 2018 2:39 pm

Yes. I viewed his and tried to replicate with USB serial cable. But it's not replying back to pi. Dont know where the error is

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Fri Jan 05, 2018 2:41 pm

antonysalvin wrote:
Fri Jan 05, 2018 2:39 pm
Yes. I viewed his and tried to replicate with USB serial cable. But it's not replying back to pi. Dont know where the error is
did you connect the sensor to mains?
it's not going to work if you connect only the serial port :)
(be careful with mains..)

antonysalvin
Posts: 4
Joined: Sun May 28, 2017 8:42 am

Re: Peaceview PZEM-004T Energy Module

Fri Jan 05, 2018 2:46 pm

Yes. I tried with mains only. Even connected hc-05 bt module but my pi is not having constant connection with bt saying no useful function found

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Peaceview PZEM-004T Energy Module

Sun Jan 07, 2018 5:18 pm

could you please remind me if the UART of the pzem is 3,3V Logic or 5V logic?
Thanks

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: 4
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: 4
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!

Return to “Automation, sensing and robotics”