Skip to content

Commit

Permalink
Update pzem_reader.py
Browse files Browse the repository at this point in the history
  • Loading branch information
croutonso committed May 4, 2023
1 parent 7fefbf2 commit d04778c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pzem_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
BAUD_RATE = 9600
TIMEOUT = 1
PORT = '/dev/ttyUSB0'

def read_pzem_data():
# Initialize the connection to the PZEM device
instrument = minimalmodbus.Instrument(PORT, DEVICE_ADDRESS)
instrument.serial.baudrate = 9600
instrument.serial.bytesize = 8
Expand All @@ -16,6 +18,7 @@ def read_pzem_data():
instrument.serial.timeout = 1

try:
# Read measurement data
voltage = instrument.read_register(0x0000, number_of_decimals=2, functioncode=4)
current = instrument.read_register(0x0001, number_of_decimals=2, functioncode=4)
power_low = instrument.read_register(0x0002, functioncode=4)
Expand All @@ -25,11 +28,19 @@ def read_pzem_data():
energy_high = instrument.read_register(0x0005, functioncode=4)
energy = (energy_high << 16) + energy_low

# Read alarm status
high_voltage_alarm = instrument.read_register(0x0006, functioncode=4)
low_voltage_alarm = instrument.read_register(0x0007, functioncode=4)

print(f"Voltage: {voltage} V")
print(f"Current: {current} A")
print(f"Power: {power * 0.1} W")
print(f"Energy: {energy} Wh")

# Print alarm statuses
print(f"High Voltage Alarm: {'Alarm' if high_voltage_alarm == 0xFFFF else 'Clear'}")
print(f"Low Voltage Alarm: {'Alarm' if low_voltage_alarm == 0xFFFF else 'Clear'}")

except minimalmodbus.IllegalRequestError as e:
print(f"Error: {e}")

Expand Down

0 comments on commit d04778c

Please sign in to comment.