説明なし

mq303a.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@ics.com>
  3. # Copyright (c) 2015 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import time, sys, signal, atexit
  24. # Load alcohol sensor module
  25. import pyupm_mq303a as upmMq303a
  26. # Instantiate an mq303a sensor on analog pin A0
  27. # This device uses a heater powered from an analog I/O pin.
  28. # If using A0 as the data pin, then you need to use A1, as the heater
  29. # pin (if using a grove mq303a). For A1, we can use the D15 gpio,
  30. # setup as an output, and drive it low to power the heater.
  31. myAlcoholSensor = upmMq303a.MQ303A(0, 15)
  32. ## Exit handlers ##
  33. # This function stops python from printing a stacktrace when you hit control-C
  34. def SIGINTHandler(signum, frame):
  35. raise SystemExit
  36. # This function lets you run code on exit, including functions from myAlcoholSensor
  37. def exitHandler():
  38. print "Exiting"
  39. sys.exit(0)
  40. # Register exit handlers
  41. atexit.register(exitHandler)
  42. signal.signal(signal.SIGINT, SIGINTHandler)
  43. print "Enabling heater and waiting 2 minutes for warmup."
  44. # give time updates every 30 seconds until 2 minutes have passed
  45. # for the alcohol sensor to warm up
  46. def warmup(iteration):
  47. totalSeconds = (30 * iteration)
  48. time.sleep(30)
  49. print totalSeconds, "seconds have passed"
  50. warmup(1)
  51. warmup(2)
  52. warmup(3)
  53. warmup(4)
  54. notice = ("This sensor may need to warm "
  55. "until the value drops below about 450.")
  56. print notice
  57. # Print the detected alcohol value every second
  58. while(1):
  59. val = myAlcoholSensor.value()
  60. msg = "Alcohol detected "
  61. msg += "(higher means stronger alcohol): "
  62. print msg + str(val)
  63. time.sleep(1)