Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python3
- # encoding: utf-8
- import serial
- import struct
- # ----------------------------------------------------------------
- # Config
- BAUD_RATE = 9600
- PORT = "/dev/ttyACM0"
- KWH_PER_IMPULSE = 0.1 # 100 Wh/imp
- # ----------------------------------------------------------------
- if __name__ == "__main__":
- serial.Serial() as s:
- s.baudrate = BAUD_RATE
- s.port = PORT
- s.timeout = None # wait forever
- s.open()
- while True:
- # expecting the pulse count as a big endian (AVR) uint32_t
- raw = s.read(4) # e.g. b"@\xe2\x01\x00" (hex 40 e2 01 00)
- count = struct.unpack("<I", raw) # e.g. 123456
- energy = count * KWH_PER_IMPULSE # e.g. 12345.6 kWh
- print("counter=%i imp, energy=%.01f kWh" %(count, energy))
Add Comment
Please, Sign In to add comment