Created
October 3, 2016 23:59
-
-
Save ari/9f9f87289c609e3ef103f01f6fcafaaf to your computer and use it in GitHub Desktop.
Door counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Run this application like this: | |
# /usr/bin/python /home/pi/doorCounter.py -n label -g 22 | |
# -g: GPIO pin to watch | |
# -n: label to send to Google Analytics | |
import RPi.GPIO as GPIO | |
import time | |
import urllib2 | |
import sys, getopt | |
import threading | |
import random | |
import os | |
import sys | |
GA_CODE='UA-xxxxxxxx-xx' | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv,"hn:g:",["name=","gpio_pin="]) | |
except getopt.GetoptError: | |
print 'doorCounter.py -n <door name> -g <GPIO pin>' | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print 'doorCounter.py -n <door name> -g <GPIO pin>' | |
sys.exit() | |
elif opt in ("-n", "--name"): | |
name = arg | |
elif opt in ("-g", "--gpio_pin"): | |
gpio_pin = int(arg) | |
if 'name' not in locals() or name == '': | |
sys.exit("You must pass a name.") | |
if 'gpio_pin' not in locals() or gpio_pin < 1 or gpio_pin > 27: | |
sys.exit("You must pass a GPIO pin.") | |
pidfile = ''.join(['/var/run/doorCounter_',name,'_',str(gpio_pin),'.pid']) | |
print "=============================" | |
print "Door counter started..." | |
print "Door name: ", name | |
print "GPIO pin: ", gpio_pin | |
print "=============================" | |
print "" | |
writePid(pidfile) | |
setup(gpio_pin) | |
monitor(gpio_pin, name) | |
def writePid(pidfile): | |
pid = str(os.getpid()) | |
file(pidfile, 'w').write(pid) | |
def setup(gpio_pin): | |
# Use BCM GPIO references | |
# instead of physical pin numbers | |
GPIO.setmode(GPIO.BCM) | |
# Set pin as input | |
GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
while GPIO.input(gpio_pin)==0: | |
time.sleep(0.10) | |
def send(name): | |
uuid = name + "." + str(random.randint(100000,9999999)) | |
urllib2.urlopen("""http://www.google-analytics.com/collect?v=1&tid=%s&cid=%s&t=event&ec=%s&ea=person""" % (GA_CODE,uuid, name) ).close | |
print "--sent" | |
def monitor(gpio_pin, name): | |
Current_State = 0 | |
Previous_State = 0 | |
try: | |
while True : | |
# Read PIR state | |
Current_State = GPIO.input(gpio_pin) | |
if Current_State==0 and Previous_State==1: | |
print "Sensor blocked..." | |
Previous_State = 0 | |
threading.Thread(target=send(name)).start() | |
elif Current_State==1 and Previous_State==0: | |
# Sensor has returned to ready state | |
print "--ready" | |
Previous_State = 1 | |
# Wait for 100 milliseconds | |
time.sleep(0.10) | |
except KeyboardInterrupt: | |
print "=============================" | |
print " Quitting" | |
print "=============================" | |
# Reset GPIO settings | |
GPIO.cleanup() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment