minimal fixes to temendously terrible logic on the part of the original author

This commit is contained in:
Brennen Raimer
2019-01-18 13:47:05 -05:00
parent 1f77452e11
commit 0ae49b9afe
3 changed files with 21 additions and 22 deletions

View File

@@ -26,8 +26,7 @@ mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
try: try:
while True: while True:
curr_date = datetime.now().strftime("%Y-%m-%d") curr_date = datetime.now()
curr_time = datetime.now().strftime("%H:%M:%S")
# Read current LDR value from ADC # Read current LDR value from ADC
#curr_ldr = mcp.read_adc(0) #curr_ldr = mcp.read_adc(0)
@@ -43,9 +42,9 @@ try:
cursor = db.cursor() cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database. # 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) sql = "INSERT INTO LDRSTATS (datetime, gatecount) VALUES ('%s', '%d')" % (curr_date.isoformat(' '), 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"): if (curr_date.minute % 10 == 0) and (curr_date.second == 0):
try: try:
# Execute the SQL command # Execute the SQL command
cursor.execute(sql) cursor.execute(sql)

View File

@@ -3,7 +3,9 @@
#!/usr/bin/python #!/usr/bin/python
import sys import sys
import time, MySQLdb import MySQLdb
from time import sleep
from datetime import datetime
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
# Set RPi GPIO Mode # Set RPi GPIO Mode
@@ -31,15 +33,13 @@ try:
# prepare a cursor object using cursor() method # prepare a cursor object using cursor() method
cursor = db.cursor() cursor = db.cursor()
curr_date = time.strftime("%Y-%m-%d") curr_date = datetime.now()
curr_time = time.strftime("%H:%M:%S")
# Prepare SQL query to INSERT a record into the database. # 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) sql = "INSERT INTO PIRSTATS (datetime, gatecount) VALUES ('%s', '%d')" % (curr_date.isoformat(' '), 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"): if (curr_date.minute % 10 == 0) and (curr_date.second == 0):
try: try:
# Execute the SQL command # Execute the SQL command
cursor.execute(sql) cursor.execute(sql)
@@ -52,7 +52,7 @@ try:
# Disconnect from database server # Disconnect from database server
db.close() db.close()
time.sleep(1) sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
print ("\nCtrl-C pressed cleaning up GPIO") print ("\nCtrl-C pressed cleaning up GPIO")
GPIO.cleanup() GPIO.cleanup()

View File

@@ -1,7 +1,9 @@
# Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library # Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library
import sys import sys
import time, MySQLdb import MySQLdb
from datetime import datetime
from time import sleep
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
ultraSonicConst = "95" ultraSonicConst = "95"
@@ -28,14 +30,13 @@ cursor = db.cursor()
try: try:
while True: while True:
curr_date = time.strftime("%Y-%m-%d") curr_date = datetime.now()
curr_time = time.strftime("%H:%M:%S")
GPIO.output(TRIG, False) GPIO.output(TRIG, False)
time.sleep(0.5) sleep(0.5)
GPIO.output(TRIG, True) GPIO.output(TRIG, True)
time.sleep(0.00001) sleep(0.00001)
GPIO.output(TRIG, False) GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0: while GPIO.input(ECHO) == 0:
@@ -62,11 +63,10 @@ try:
# Prepare SQL query to INSERT a record into the database. # 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 (curr_date.minute % 10 == 0) and (curr_date.second == 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: try:
# Create SQL Query # Create SQL Query
sql = "INSERT INTO ULTRASTATS (date, time, gatecount) VALUES ('%s', '%s', '%d')" % (curr_date, curr_time, count) sql = "INSERT INTO ULTRASTATS (datetime, gatecount) VALUES ('%s', '%d')" % (curr_date.isoformat(' '), count)
# Execute the SQL command # Execute the SQL command
cursor.execute(sql) cursor.execute(sql)
# Commit your changes in the database # Commit your changes in the database
@@ -75,7 +75,7 @@ try:
# Rollback in case there is any error # Rollback in case there is any error
db.rollback() db.rollback()
time.sleep(0.5) sleep(0.5)
except KeyboardInterrupt: except KeyboardInterrupt:
db.close() db.close()
print("\nCtrl-C pressed cleaning up GPIO") print("\nCtrl-C pressed cleaning up GPIO")