Integrated TheHWCave's Pzem004T v3.0 library with my v1.0 library to poll meters every 10 seconds via and posts to a remote mySQL database. Utilizes systemctl to manage python processes for each meter connected (Ex: to Raspberry Pi).
Works with both Pzem004T v1.0 and Pzem004T v3.0 modules
- Tested on Raspberry PI 3 and 3+
- I do NOT reccomend Pi Zeros v1 or v2 as the USB issues do not resolve without manual power cycling
- First create a virtual environment
python3 -m venv venv
- Activate the virtual environment
source venv/bin/activate
- Install the required librariess
- MySQL-Connector-Python
sudo pip3 install mysql-connector-python
- MySQL-Connector-Python
-
Connect the PZEM meter per specs and connect the serial interface to a Serial-to-USB adapter into a Raspbery Pi.
-
Create a meter config file (Ex: meter1.py) See below.
-
Create a systemctl file to manage this process for you. See below
##If you have multiple meters You can use the serial addressing method of the Pzem library to be sure the USB addresses in the config are pointing to the right meter.
#meter1.py
from pzemTools.pzem_postv3 import run
import os
table = 'mysql_table'
meter_port = '/dev/ttyUSB0'
meter_addr = 1
meter_ver = 'v3'
mysql_host = 'my.host'
mysql_db = 'mysqldb'
mysql_user = 'myUser'
mysql_pw = 'myPassword'
run( table, mysql_host, mysql_db, mysql_user, mysql_pw, meter_port, meter_addr, meter_ver)
if __name__ == '__main__':
meter1.py()
create service file
sudo nano /lib/systemd/system/meter1.service
with contents
[Unit]
Description=record pzem meter data to database(s)
[Service]
ExecStart=/home/pi/pzemBackend/venv/bin/python3 /home/pi/pzemBackend/meter1.py
Restart=always
RestartSec=10 User=pi
[Install]
WantedBy=multi-user.target
enable service
sudo systemctl enable meter1.service
start/stop/restart/status
sudo systemctl start meter1.service