Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #! /usr/bin/python3
  2. # encoding: utf-8
  3.  
  4. import serial
  5. import struct
  6.  
  7. # ----------------------------------------------------------------
  8. # Config
  9.  
  10. BAUD_RATE = 9600
  11. PORT = "/dev/ttyACM0"
  12. KWH_PER_IMPULSE = 0.1 # 100 Wh/imp
  13.  
  14. # ----------------------------------------------------------------
  15.  
  16. if __name__ == "__main__":
  17. serial.Serial() as s:
  18. s.baudrate = BAUD_RATE
  19. s.port = PORT
  20. s.timeout = None # wait forever
  21. s.open()
  22.  
  23. while True:
  24. # expecting the pulse count as a big endian (AVR) uint32_t
  25. raw = s.read(4) # e.g. b"@\xe2\x01\x00" (hex 40 e2 01 00)
  26. count = struct.unpack("<I", raw) # e.g. 123456
  27. energy = count * KWH_PER_IMPULSE # e.g. 12345.6 kWh
  28.  
  29. print("counter=%i imp, energy=%.01f kWh" %(count, energy))
Add Comment
Please, Sign In to add comment