-
Notifications
You must be signed in to change notification settings - Fork 1
/
LDRdbWriteGate.py
executable file
·64 lines (49 loc) · 1.53 KB
/
LDRdbWriteGate.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
60
61
62
63
64
# Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library
#import time, MySQLdb
import sys
import MySQLdb
from datetime import datetime
from time import sleep
# Import Adafruit Libraries
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Set LDR constant
#lrdConst = "25"
# Set current date and time
# count = int(sys.argv[1])
count = 0
# Hardware SPI config
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
try:
while True:
curr_date = datetime.now()
# Read current LDR value from ADC
#curr_ldr = mcp.read_adc(0)
#print mcp.read_adc(0)
# Check if current ldr value is less than or greater than ldrConst
if mcp.read_adc(0) > 150:
count = count + 1
# Open database connection
db = MySQLdb.connect("HOSTNAME","USERNAME","PASSWORD","DATABASE")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO LDRSTATS (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()
# Pause for three tenths of a second (will adjust later to find good timing)
sleep(0.33)
except KeyboardInterrupt:
print("\nCtr-C pressed cleaning up GPIO")
sys.exit(0)