Use PZEM-016 with MODWRT

From lechacal
Jump to: navigation, search

We will demonstrate how to use the ModWrt to interface with the PZEM-016 modulw and forward data to EmonCMS.



This is a electric meter unit with serial modbus RTU interface. This is for single phase measurements and produces Voltage, Power, Frequency, Energy and Power Factor.

Connections are straight forward and well explained in the manual. The manual for the PZEM-016 can be found here.

Only two wires are needed to connect to the ModWRT. Just connect the A and B terminals. Leave the 5v and Ground unconnected.

Modwrt Config

By default the PZEM-016 as a slave ID of 0x01 and comms as 9600,8,N,1. So we will setup the RTU page as shown below.


In the Streams configuration page make sure you have one instance setup to forward to Emoncms.

In the Dataflow page configure each registers as shown in this picture below.


On the Emoncms platform all data will be depicted like this.


You will notice that most data are multiplied by a factor of 10 or 100. Before storing the data on Emoncms apply a process Calibration x to the data. Basically Voltage x0.1 / Power x0.1 / Frequence x0.1 / Power Factor x0.01.

Configuring PZEM-016

ModWRT has the ability to communication to any generic Modbus RTU device. We have build the modbus_raw_send command for this purpose.

For this you will need to connect to the Modwrt with ssh. Use the putty program for this.

Basic reading test

Here is a simple command to make sure the communication with the RTU device works correctly:

modbus_raw_send 9600-8-N-1 01 04 00 00 00 01

The reply will look as follow:

root@OpenWrt:~# modbus_raw_send 01 04 00 00 00 01
This function isn't supported on your platform
Opening /dev/ttyATH0 at 9600 bauds (N, 8, 1)
Sending request using RTS signal
Waiting for a confirmation...
Request for slave 1 ignored (not -1)

Ignore any warning on this returned message. The most important is the <01><04><02><09><BF><FE><D0> confirming a correct communication.

Reset Energy

This is the command to reset the Energy accumulator back to 0.

modbus_raw_send 9600-8-N-1 01 42

Change Slave ID

The default slave ID is 01. If you have several devices you will need to have them using different slave ID.

To change the slave ID type the command as follow

modbus_raw_send 9600-8-N-1 01 06 00 02 00 05

A valid reply will include:


Note: There is a tipo in the manual which states incorrectly this should be 01 04 00 02 00 05.

The slave ID is now 05. For any other ID just replace the trailing 05 with anything you like.

Holding registers

This is a list of holding registers that we have found. Manual does not show that.

0x0001 Power Alarm Threshold
0x0002 Slave ID
0x0003 Baud rate (got <25><80> = 9600)
0x0004 ? got <08><98> = 2200
0x0005 ? got <27><10> = 10000
0x0006 ? got <55><F0> = 22000