Initial commit

This commit is contained in:
squash-
2017-07-17 11:48:21 -04:00
parent 40270f8b36
commit 4425437828
3 changed files with 215 additions and 0 deletions

69
LDRdbWriteGate.py Executable file
View File

@@ -0,0 +1,69 @@
#!/usr/bin/python
# Written By Johnathan Cintron and Devlyn Courtier
# Copyright (c) 2016
#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().strftime("%Y-%m-%d")
curr_time = datetime.now().strftime("%H:%M:%S")
# 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 (date, time, gatecount) VALUES ('%s', '%s', '%d')" % (curr_date, curr_time, count)
if any( [datetime.now().strftime("%M") == "00", int(datetime.now().strftime("%M")) == 10, int(datetime.now().strftime("%M")) == 20, int(datetime.now().strftime("%M")) == 30, int(datetime.now().strftime("%M")) == 40, int(datetime.now().strftime("%M")) == 50] ) and (datetime.now().strftime("%S") == "00"):
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)

60
PIRdbWriteGate.py Executable file
View File

@@ -0,0 +1,60 @@
# Written By Johnathan Cintron and Devlyn Courtier
# Copyright (c) 2016
#!/usr/bin/python
import sys
import time, MySQLdb
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 = time.strftime("%Y-%m-%d")
curr_time = time.strftime("%H:%M:%S")
# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO PIRSTATS (date, time, gatecount) VALUES ('%s', '%s', '%d')" % (curr_date, curr_time, count)
if any( [time.strftime("%M") == "00", int(time.strftime("%M")) == 10, int(time.strftime("%M")) == 20, int(time.strftime("%M")) == 30, int(time.strftime("%M")) == 40, int(time.strftime("%M")) == 50]) and (time.strftime("%S") == "00"):
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()
time.sleep(1)
except KeyboardInterrupt:
print ("\nCtrl-C pressed cleaning up GPIO")
GPIO.cleanup()
sys.exit(0)

86
UltraSonicdbWriteGate.py Executable file
View File

@@ -0,0 +1,86 @@
#!/usr/bin/python
# Written By Johnathan Cintron and Devlyn Courtier
# Copyright (c) 2016
import sys
import time, MySQLdb
import RPi.GPIO as GPIO
ultraSonicConst = "95"
count = 0
# Set RPi GPIO Mode
GPIO.setmode(GPIO.BCM)
# Set GPIO pins
TRIG = 23
ECHO = 24
# Setup GPIO in and out
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
# End GPIO setup
# Open Database Connection
db = MySQLdb.connect("HOSTNAME","USERNAME","PASSWORD","DATABASE")
# Prepare a cursor object
cursor = db.cursor()
try:
while True:
curr_date = time.strftime("%Y-%m-%d")
curr_time = time.strftime("%H:%M:%S")
GPIO.output(TRIG, False)
time.sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
if distance < 120:
count = count + 1
#print count
#print distance
# Open database connection
# prepare a cursor object using cursor() method
# Prepare SQL query to INSERT a record into the database.
#if any( [int(time.strftime("%H")) == 8, int(time.strftime("%H")) == 15, int(time.strftime("%H")) == 22] ) and (int(time.strftime("%M")) == 0):
if any( [time.strftime("%M") == "00", int(time.strftime("%M")) == 10, int(time.strftime("%M")) == 20, int(time.strftime("%M")) == 30, int(time.strftime("%M")) == 40, int(time.strftime("%M")) == 50]) and (time.strftime("%S") == "00"):
try:
# Create SQL Query
sql = "INSERT INTO ULTRASTATS (date, time, gatecount) VALUES ('%s', '%s', '%d')" % (curr_date, curr_time, count)
# 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()
time.sleep(0.5)
except KeyboardInterrupt:
db.close()
print("\nCtrl-C pressed cleaning up GPIO")
GPIO.cleanup()
sys.exit(0)