Advertisement
Guest User

Untitled

a guest
May 26th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4.  
  5. # Python app to run a Cozir Sensor
  6.  
  7.  
  8.  
  9. import serial
  10.  
  11. import time
  12.  
  13. import signal
  14.  
  15. import sys
  16.  
  17. import pigpio
  18.  
  19.  
  20.  
  21. def handler(signum,frame):
  22.  
  23. print 'You pressed CTRL+C! Exiting...'
  24.  
  25. pi.bb_serial_read_close(RXD)
  26.  
  27. pi.wave_delete(wid)
  28.  
  29. pi.stop()
  30.  
  31. sys.exit(0)
  32.  
  33.  
  34.  
  35. TXD=17
  36.  
  37. RXD=27
  38.  
  39.  
  40.  
  41. pi = pigpio.pi() # Connect to local host.
  42.  
  43. print "pigpio Connected!"
  44.  
  45.  
  46.  
  47. pi.bb_serial_read_open(RXD, 9600)
  48.  
  49.  
  50.  
  51. pi.set_mode(TXD,pigpio.OUTPUT)
  52.  
  53. pi.wave_clear()
  54.  
  55.  
  56.  
  57. #Set Cozir mode to polling
  58.  
  59. pi.wave_add_serial(TXD,38400,b"K 2\r\n")
  60.  
  61. wid = pi.wave_create()
  62.  
  63. pi.wave_send_once(wid)
  64.  
  65. time.sleep(1)
  66.  
  67.  
  68.  
  69. #Send command to read CO2 measurement
  70.  
  71. pi.wave_add_serial(TXD,38400,b"Z\r\n")
  72.  
  73. wid = pi.wave_create()
  74.  
  75.  
  76.  
  77. #signal handles for keyboard interrupt
  78.  
  79. signal.signal(signal.SIGINT, handler)
  80.  
  81.  
  82.  
  83. while True:
  84.  
  85. try:
  86.  
  87. pi.wave_send_once(wid)
  88.  
  89. time.sleep(.01)
  90.  
  91. (count,resp) = pi.bb_serial_read(RXD)
  92.  
  93. print "Cozir Raw Response = " + str(resp)
  94.  
  95.  
  96. resp = resp[:8]
  97.  
  98. print ""
  99.  
  100. print ""
  101.  
  102. fltCo2 = float(resp[2:])
  103.  
  104. print "Co2 = " + str(fltCo2)
  105.  
  106. time.sleep(1)
  107.  
  108.  
  109.  
  110. except Exception,e:
  111.  
  112.  
  113.  
  114. print "Error: %s"%(e)
  115.  
  116. time.sleep(1)
  117.  
  118. continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement