Git Product home page Git Product logo

croutonso / pzem017modbus Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 175 KB

Python-based utility for interfacing with the PZEM-017 DC Energy Monitoring Device. Easily read, modify settings, and monitor energy consumption of your solar or battery systems. Developed with assistance from ChatGPT by OpenAI.

License: GNU General Public License v3.0

Python 100.00%
dc-monitoring energy energy-monitor modbus pzem-017 raspberry-pi solar solar-energy battery-monitor energy-management python

pzem017modbus's Introduction

Picture of PZEM-017 dive from amazon

PZEM-017 Modbus Interface

This repository contains two Python scripts for interfacing with PZEM-017 Modbus energy monitoring devices. The scripts allow you to read the data from the devices and change their parameters.

Table of Contents

Requirements

  • Raspberry Pi with Raspbian OS or any Linux system
  • Python 3
  • minimalmodbus Python library
  • pySerial Python library
  • PZEM-017 energy monitoring device
  • USB to RS485 converter (e.g., FTDI USB-RS485 Cable)

Installation

  1. Install Python 3 from the official website.

  2. Install the required Python libraries by running the following command in your terminal or command prompt:

    pip install minimalmodbus pyserial
    
  3. Clone this repository or download the Python scripts pzem_reading.py and change_settings.py.

git clone https://github.com/croutonso/PZEM017modbus.git
  1. Connect the PZEM device to your Raspberry Pi or Linux system using a USB to RS485 converter.

Usage

pzem_reading.py

This script reads data from the PZEM-017 device and displays the voltage, current, power, and energy values.

  1. Open pzem_reading.py in a text editor and set the DEVICE_ADDRESS, PORT, and other parameters according to your device and connection.

  2. Save the changes and close the text editor.

  3. Open a terminal, navigate to the directory containing pzem_reading.py, and run the following command:

    python pzem_reading.py
    
  4. The script will display the voltage, current, power, and energy values.

change_settings.py

This script allows you to change the parameters of the PZEM-017 device, such as high and low voltage alarm thresholds, slave address, and current range (PZEM-017 only).

  1. Open change_settings.py in a text editor and set the SLAVE_ADDRESS, DEVICE_PORT, and other parameters according to your device and connection.

  2. Save the changes and close the text editor.

  3. Open a terminal, navigate to the directory containing change_settings.py, and run the following command:

    python change_settings.py
    
  4. The script will display a menu with options to change various device parameters.

FAQ

Q: Does the slave address matter?

A: The slave address is important to avoid conflicts between multiple Modbus devices connected to the same computer. Each device should have a unique slave address to prevent clashes and ensure proper communication.

Q: What should I do if I already have another Modbus device connected to my computer?

A: Make sure that each Modbus device has a unique slave address. You can change the slave address of the PZEM devices using the change_settings.py script. Follow the steps in the Usage section to run the script and change the slave address.

Q: What if the script does not work or shows an error message?

A: Check the following:

  • Ensure that the PZEM device is properly connected to your computer.
  • Verify that the DEVICE_ADDRESS, PORT, and other parameters in the script are correctly set according to your device and connection.
  • Confirm that you have installed the required Python libraries (minimalmodbus and pySerial).

Q: Where can I find more information about the PZEM-004T and PZEM-017 devices?

A: You can find more information about the devices on their respective product pages:

Q: How do I identify the correct port for my device on my Raspberry Pi or Linux system?

A: You can run the following command to list available serial ports:

dmesg | grep tty

Look for lines containing ttyUSB or ttyAMA. The port name should look like /dev/ttyUSB0 or /dev/ttyAMA0. Use this port name in the PORT variable in the Python scripts.

Q: Can I use these scripts on a Windows system?

A: Yes, these scripts can be used on a Windows system. You will need to adjust the PORT variable in the Python scripts to the appropriate COM port for your device (e.g., COM3). You can check available COM ports in the Device Manager under "Ports (COM & LPT)".

Q: How do I install the minimalmodbus and pySerial libraries on a Raspberry Pi or Linux system?

A: You can install the libraries using the following command:

pip3 install minimalmodbus pyserial

Make sure you are using pip3 for Python 3 installations.

Q: How do I run the scripts with Python 3 on a Raspberry Pi or Linux system?

A: You can run the scripts with Python 3 using the following command:

python3 script_name.py

Replace script_name.py with the name of the script you want to run, such as pzem_reading.py or change_settings.py.

pzem017modbus's People

Contributors

croutonso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pzem017modbus's Issues

Reset Energy error

Hi, croutonso;

Thanks for the great program, it works fantastically. There is a minor problem, when using change_settings.py -> 5 Reset Energy, receiving following error,

Select the parameter to change:

  1. Set High Voltage Alarm Threshold (Current: 300.00 V, Default: 300 V)
  2. Set Low Voltage Alarm Threshold (Current: 7.00 V, Default: 7 V)
  3. Set Slave Address (Current: 2, Default: 1)
  4. Set Current Range (Current: 50 A, Default: 0)
  5. Reset Energy
  6. Exit
    Enter your choice: 5
    An unexpected error occurred: The payload should be bytes. Given: ''

Any advise/help would be much appreciated.

Regards,
Wade

No comunication error??

Hi, I've been trying to get this working on a rock64 SBC (running Armbian 23.11.1 bookworm) and keep getting this message:

onion@karakung:~/PEMZ/PZEM017modbus$ ../bin/python3 pzem_reader.py 
Traceback (most recent call last):
  File "/home/onion/PEMZ/PZEM017modbus/pzem_reader.py", line 52, in <module>
    read_pzem_data()
  File "/home/onion/PEMZ/PZEM017modbus/pzem_reader.py", line 22, in read_pzem_data
    voltage = instrument.read_register(0x0000, number_of_decimals=2, functioncode=4)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/onion/PEMZ/lib/python3.11/site-packages/minimalmodbus.py", line 484, in read_register
    returnvalue = self._generic_command(
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/onion/PEMZ/lib/python3.11/site-packages/minimalmodbus.py", line 1276, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/onion/PEMZ/lib/python3.11/site-packages/minimalmodbus.py", line 1353, in _perform_command
    response_bytes = self._communicate(request_bytes, number_of_bytes_to_read)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/onion/PEMZ/lib/python3.11/site-packages/minimalmodbus.py", line 1521, in _communicate
    raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)

I see the device on /dev/ttyUSB0, but can't figure out why it doesn't see it. I'm thinking the DEVICE_ADDRESS setting may be wrong but can't see where to find the correct setting there.

here's the settings from my pzem_reader.py:

DEVICE_ADDRESS = 0x01
BAUD_RATE = 9600
TIMEOUT = 1
PORT = '/dev/ttyUSB0'

any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.