Power measurement and logger using Rpi+PZEM016+influxDB+Grafana

Bhuris Sridurongrit
5 min readAug 18, 2020

--

Hello, I’m Bhuris, This is my first Medium story I will show my practice including the fixed issue.

Key Words: Electrical power measurement, Modbus RTU interface with Rpi,

check this out → https://github.com/bhuris/modbusreadpzem016

WARNING !!!

THIS PROJECT WORKS WITH MAINS VOLTAGE !!!

YOUR SAFETY AND YOUR PROPERTY IS YOUR RESPONSIBILITY. !!!

Read this → https://hackaday.com/2016/05/16/looking-mains-voltage-in-the-eye-and-surviving-part-2/

Introduction

I need the telemetry system and logging of my local grid usage in a part of my home automation project with my requirements below

  • Real-time logging (Interval 0.5–1 S)
  • High Reliable for 24/7
  • Low Power consumption
  • Able to connect to time series database (this case in influx DB)
  • simplicity

Hardware Need

  • Raspberry Pi 3B+ or 4 and USB Charger
  • PZEM 016 Module
  • USB to RS485
  • Misc (Enclosure Box, wiring cable, control wiring cable, etc)

Prerequisites

  • Influx database, Grafana
  • some Node-red and Python Familiar
  • Basic Linux commands

Hardware review

this is very handy and … module from China they arrive in a week !!

Set Up Raspberry pi

  1. Use Raspberry Pi Imager to flash the SD card for Raspbian 32bit
  2. Add ssh file(without any file extension) for enable ssh
  3. Insert SD card to raspberry pi
  4. Power Up and connect Ethernet cable to RPi
  5. Find the Rpi IP address from DHCP Lease section on you Firewall/Router
  6. ssh to raspberry pi (enter ssh pi@<your-pi-ip-address>) then enter default password ‘raspberry’
  7. Install node-red and setup
  8. copy Python File to the desired directory

9. Insert USB-RS485 to RPi and Start Terminal then run ‘lsusb’ command and you should see a USB device.

pi@iotnode554b:~ $ lsusb
Bus 001 Device 004: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 001 Device 005: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@iotnode554b:~ $ ls /dev/tty*
/dev/tty /dev/tty19 /dev/tty3 /dev/tty40 /dev/tty51 /dev/tty62
/dev/tty0 /dev/tty2 /dev/tty30 /dev/tty41 /dev/tty52 /dev/tty63
/dev/tty1 /dev/tty20 /dev/tty31 /dev/tty42 /dev/tty53 /dev/tty7
/dev/tty10 /dev/tty21 /dev/tty32 /dev/tty43 /dev/tty54 /dev/tty8
/dev/tty11 /dev/tty22 /dev/tty33 /dev/tty44 /dev/tty55 /dev/tty9
/dev/tty12 /dev/tty23 /dev/tty34 /dev/tty45 /dev/tty56 /dev/ttyAMA0
/dev/tty13 /dev/tty24 /dev/tty35 /dev/tty46 /dev/tty57 /dev/ttyprintk
/dev/tty14 /dev/tty25 /dev/tty36 /dev/tty47 /dev/tty58 /dev/ttyUSB0
/dev/tty15 /dev/tty26 /dev/tty37 /dev/tty48 /dev/tty59
/dev/tty16 /dev/tty27 /dev/tty38 /dev/tty49 /dev/tty6
/dev/tty17 /dev/tty28 /dev/tty39 /dev/tty5 /dev/tty60
/dev/tty18 /dev/tty29 /dev/tty4 /dev/tty50 /dev/tty61

10. Set port and Modbus RTU slave address (default is 0x01) on python file

port='/dev/ttyUSB0'
unit = '0x01'

11. Connect RS485 between PZEM and USB to RS485 dongle

  • wiring main voltage into PZEM and clamp the CT to power wire

12. Run a test

pi@iotnode554b:~/Desktop/eepanelpgm $ python3 readmodbus.py
{"volt": 226.20000000000002, "amp": 7.088, "realpower": 1566.9, "energy": 24.899, "freq": 49.900000000000006, "pwfac": 0.98, "reactive": 318.17261534742113, "apparent": 1598.8775510204082, "powerangle": 0.20033484232311968, "impedance": 31.824954184089954, "rinline": 31.188455100408156, "xinline": 6.333085919932656, "status": "NORMAL JA"}

13. Create new influx DB User and database

> influx> CREATE DATABASE "eepanelgridmeas"
> user create -n johnkerry -p PaSsWoRd -o example-org
## (Optional) Set Retention Policy for limiting database oversize> CREATE RETENTION POLICY "one_day_only" ON "eepanelgridmeas" DURATION 23h60m REPLICATION 1 DEFAULT
> SHOW RETENTION POLICIES ON "eepanelgridmeas"

For more Influx command Link

14. Set up InfluxDB on Node-red

  • enter you own Measurement (it’s automatically created on that database)
  • add a firewall rule to allows for port 8086

15. Add new data source on Grafana

16. Layout you dashboard

17. Add some time series chart for “ aesthetic ”

this nasty box is not an “ aesthetic ” one LOL

Recommend: Add a fuse between PT line for additional safety!

If you have a question or suggestion please let me know on GitHub !!

Issue

Wifi Connection lose after a long time and AP restart

Cause: WLAN power management feature set has been set to on by default

Then: disable this feature to prevent Wlan turn off automatically

### backup original file 
> sudo cp /etc/network/interfaces /etc/network/interfaces.backup ### Now edit the original interfaces file:
> sudo nano /etc/network/interfaces
## add this code to file
## Assuming you haven’t edited this file before all you need to do is delete its entire contents, and copy the following into it:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
wireless-power off
iface default inet dhcp
## The important block of code here is:
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
wireless-power off
iface default inet dhcp
## Thx to this blog https://thepihut.com/blogs/raspberry-pi-tutorials/disable-wifi-power-management

Rpi won’t start when using switching power supply

Cause: SMPS is usually generating the inference to sensitive device

then: insert 0.1uF ceramic between DC bus and use a shielded cable, also take a distance btw device and SMPS

check out this → https://github.com/bhuris/modbusreadpzem016

--

--

Responses (1)