Guest User

Untitled

a guest
Oct 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import serial
  3. import datetime
  4. import time
  5.  
  6. port = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=2.0)
  7.  
  8. port.write(b'\x42\x4D\xE4\x00\x01\x01\x74')
  9. def read_pm_line(_port):
  10. rv = b''
  11. while True:
  12. ch1 = _port.read()
  13. if ch1 == b'\x42':
  14. ch2 = _port.read()
  15. if ch2 == b'\x4d':
  16. rv += ch1 + ch2
  17. rv += _port.read(28)
  18. return rv
  19.  
  20.  
  21. while True:
  22. try:
  23. rcv = read_pm_line(port)
  24. res = {'timestamp': datetime.datetime.now(),
  25. 'apm10': rcv[4] * 256 + rcv[5],
  26. 'apm25': rcv[6] * 256 + rcv[7],
  27. 'apm100': rcv[8] * 256 + rcv[9],
  28. 'pm10': rcv[10] * 256 + rcv[11],
  29. 'pm25': rcv[12] * 256 + rcv[13],
  30. 'pm100': rcv[14] * 256 + rcv[15],
  31. 'gt03um': rcv[16] * 256 + rcv[17],
  32. 'gt05um': rcv[18] * 256 + rcv[19],
  33. 'gt10um': rcv[20] * 256 + rcv[21],
  34. 'gt25um': rcv[22] * 256 + rcv[23],
  35. 'gt50um': rcv[24] * 256 + rcv[25],
  36. 'gt100um': rcv[26] * 256 + rcv[27]
  37. }
  38. print('===============\n'
  39. 'PM1.0(CF=1): {}\n'
  40. 'PM2.5(CF=1): {}\n'
  41. 'PM10 (CF=1): {}\n'
  42. 'PM1.0 (STD): {}\n'
  43. 'PM2.5 (STD): {}\n'
  44. 'PM10 (STD): {}\n'
  45. '>0.3um : {}\n'
  46. '>0.5um : {}\n'
  47. '>1.0um : {}\n'
  48. '>2.5um : {}\n'
  49. '>5.0um : {}\n'
  50. '>10um : {}'.format(res['apm10'], res['apm25'], res['apm100'],
  51. res['pm10'], res['pm25'], res['pm100'],
  52. res['gt03um'], res['gt05um'], res['gt10um'],
  53. res['gt25um'], res['gt50um'], res['gt100um']))
  54. time.sleep(1)
  55. except KeyboardInterrupt:
  56. port.write(b'\x42\x4D\xE4\x00\x00\x01\x73')
  57. break
Add Comment
Please, Sign In to add comment