croutonso / PZEM017modbus

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.
GNU General Public License v3.0
5 stars 0 forks source link

No comunication error?? #3

Open multilectical opened 8 months ago

multilectical commented 8 months ago

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?

shakidc commented 5 months ago

I got the same problem here, I try to change the slave address within 0x01 - 0xF7 (based on the datasheet) but still got the same error. I've checked the wiring and it's just fine. I'm using Raspberry Pi 3B+ and RS485 to USB converter here. I've read that this kind of error can be happened if the device is offline, but do you have any way to check whether the PZEM-017 is on or not? Because there wasn't any indicator on it.

shakidc commented 5 months ago

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?

I've solved this error by change the RS485 to USB connector from this one:

image

to this one:

image

It worked just fine after I changed the connector. Turn out for some reason (Idk what the specific case), this sensor only work with this kind of connector (I've tried the other one on every machine and OS that I have but it still didn't work). Hope my answer can help you with it.

multilectical commented 5 months ago

hmm, interesting and glad you got that sorted. though I did have the 2nd one all along. I'll try using different converters/cables and see what happens.