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 24import time, sys, signal, atexit 25 26# Load alcohol sensor module 27import pyupm_mq303a as upmMq303a 28 29# Instantiate an mq303a sensor on analog pin A0 30# This device uses a heater powered from an analog I/O pin. 31# If using A0 as the data pin, then you need to use A1, as the heater 32# pin (if using a grove mq303a). For A1, we can use the D15 gpio, 33# setup as an output, and drive it low to power the heater. 34myAlcoholSensor = upmMq303a.MQ303A(0, 15) 35 36 37## Exit handlers ## 38# This function stops python from printing a stacktrace when you hit control-C 39def SIGINTHandler(signum, frame): 40 raise SystemExit 41 42# This function lets you run code on exit, including functions from myAlcoholSensor 43def exitHandler(): 44 print "Exiting" 45 sys.exit(0) 46 47# Register exit handlers 48atexit.register(exitHandler) 49signal.signal(signal.SIGINT, SIGINTHandler) 50 51 52print "Enabling heater and waiting 2 minutes for warmup." 53 54# give time updates every 30 seconds until 2 minutes have passed 55# for the alcohol sensor to warm up 56def warmup(iteration): 57 totalSeconds = (30 * iteration) 58 time.sleep(30) 59 print totalSeconds, "seconds have passed" 60warmup(1) 61warmup(2) 62warmup(3) 63warmup(4) 64 65notice = ("This sensor may need to warm " 66"until the value drops below about 450.") 67print notice 68 69# Print the detected alcohol value every second 70while(1): 71 val = myAlcoholSensor.value() 72 msg = "Alcohol detected " 73 msg += "(higher means stronger alcohol): " 74 print msg + str(val) 75 time.sleep(1) 76