-
Notifications
You must be signed in to change notification settings - Fork 1
/
PIRdbWriteGate.py
executable file
·59 lines (42 loc) · 1.18 KB
/
PIRdbWriteGate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library
#!/usr/bin/python
import sys
import MySQLdb
from time import sleep
from datetime import datetime
import RPi.GPIO as GPIO
# Set RPi GPIO Mode
GPIO.setmode(GPIO.BCM)
# Setup GPIO in and out pins
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
# End GPIO setup
count = 0
try:
while True:
if GPIO.input(PIR_PIN):
count = count + 1
#print count
# Open database connection
db = MySQLdb.connect("HOSTNAME","USERNAME","PASSWORD","DATABASE")
# prepare a cursor object using cursor() method
cursor = db.cursor()
curr_date = datetime.now()
# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO PIRSTATS (datetime, gatecount) VALUES ('%s', '%d')" % (curr_date.isoformat(' '), count)
if (curr_date.minute % 10 == 0) and (curr_date.second == 0):
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# Disconnect from database server
db.close()
sleep(1)
except KeyboardInterrupt:
print ("\nCtrl-C pressed cleaning up GPIO")
GPIO.cleanup()
sys.exit(0)