Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2017 Dr. M. Luetzelberger <webmaster@raspberryblog.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. # MA 02110-1301, USA.
  20. #
  21. #
  22.  
  23. from __future__ import print_function
  24. import serial, struct, time
  25. import psycopg2
  26. import signal
  27. from datetime import datetime, tzinfo
  28.  
  29. ser = serial.Serial()
  30. ser.port = "COM21"
  31. ser.baudrate = 9600
  32. ser.open()
  33. ser.flushInput()
  34.  
  35. #Using vrstats because lol
  36. conn_string = "host='localhost' dbname='vrstats' user='kevin' port=5432 password='postgrespassword'"
  37. conn = psycopg2.connect(conn_string)
  38. cursor = conn.cursor()
  39. insert_stmt = """INSERT INTO aqi (pm25, pm10, ts) VALUES (%s, %s, %s);"""
  40.  
  41. def exit_handler(signum, frame):
  42. import sys
  43. cursor.close()
  44. conn.close()
  45. sys.exit(0)
  46. signal.signal(signal.SIGINT, exit_handler)
  47. signal.signal(signal.SIGILL, exit_handler)
  48. signal.signal(signal.SIGABRT, exit_handler)
  49. signal.signal(signal.SIGBREAK, exit_handler)
  50. signal.signal(signal.SIGTERM, exit_handler)
  51.  
  52. def insert(pm25, pm10, ts):
  53. statement = cursor.mogrify(insert_stmt, (pm25, pm10, ts))
  54. cursor.execute(statement)
  55. conn.commit()
  56.  
  57. def process_frame(d):
  58. r = struct.unpack('<HHxxBBB', d[2:])
  59. pm25 = r[0]/10.0
  60. pm10 = r[1]/10.0
  61. checksum = sum(ord(v) for v in d[2:8])%256
  62. valid = (checksum==r[2] and r[3]==0xab)
  63. if (valid):
  64. insert(pm25, pm10, datetime.utcnow())
  65. print("PM 2.5: {} ug/m^3 PM 10: {} ug/m^3 CRC={}".format(pm25, pm10, "OK" if valid else "NOK"))
  66.  
  67. def sensor_read():
  68. byte = 0
  69. while byte != "\xaa":
  70. byte = ser.read(size=1)
  71. d = ser.read(size=10)
  72. if d[0] == "\xc0":
  73. process_frame(byte + d)
  74.  
  75. # 0xAA, 0xB4, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0xAB
  76. def sensor_wake():
  77. bytes = ['\xaa', #head
  78. '\xb4', #command 1
  79. '\x06', #data byte 1
  80. '\x01', #data byte 2 (set mode)
  81. '\x01', #data byte 3 (sleep)
  82. '\x00', #data byte 4
  83. '\x00', #data byte 5
  84. '\x00', #data byte 6
  85. '\x00', #data byte 7
  86. '\x00', #data byte 8
  87. '\x00', #data byte 9
  88. '\x00', #data byte 10
  89. '\x00', #data byte 11
  90. '\x00', #data byte 12
  91. '\x00', #data byte 13
  92. '\xff', #data byte 14 (device id byte 1)
  93. '\xff', #data byte 15 (device id byte 2)
  94. '\x05', #checksum
  95. '\xab'] #tail
  96.  
  97. for b in bytes:
  98. ser.write(b)
  99.  
  100. # xAA, 0xB4, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB
  101. def sensor_sleep():
  102. bytes = ['\xaa', #head
  103. '\xb4', #command 1
  104. '\x06', #data byte 1
  105. '\x01', #data byte 2 (set mode)
  106. '\x00', #data byte 3 (sleep)
  107. '\x00', #data byte 4
  108. '\x00', #data byte 5
  109. '\x00', #data byte 6
  110. '\x00', #data byte 7
  111. '\x00', #data byte 8
  112. '\x00', #data byte 9
  113. '\x00', #data byte 10
  114. '\x00', #data byte 11
  115. '\x00', #data byte 12
  116. '\x00', #data byte 13
  117. '\xff', #data byte 14 (device id byte 1)
  118. '\xff', #data byte 15 (device id byte 2)
  119. '\x05', #checksum
  120. '\xab'] #tail
  121.  
  122. for b in bytes:
  123. ser.write(b)
  124.  
  125. def main():
  126. while (True):
  127. print("Sensor waking.")
  128. sensor_wake()
  129. time.sleep(15)
  130. ser.flushInput()
  131. print("Reading data from sensor.")
  132. sensor_read()
  133. time.sleep(5)
  134. print("Entering sleep mode.")
  135. sensor_sleep()
  136. time.sleep(10)
  137.  
  138. if __name__ == '__main__':
  139. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement