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:
while True:
curr_date = datetime.now().strftime("%Y-%m-%d")
curr_time = datetime.now().strftime("%H:%M:%S")
curr_date = datetime.now()
# Read current LDR value from ADC
#curr_ldr = mcp.read_adc(0)
@@ -43,9 +42,9 @@ try:
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)
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:
# Execute the SQL command
cursor.execute(sql)

View File

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

View File

@@ -1,7 +1,9 @@
# Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library
import sys
import time, MySQLdb
import MySQLdb
from datetime import datetime
from time import sleep
import RPi.GPIO as GPIO
ultraSonicConst = "95"
@@ -14,7 +16,7 @@ GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
# Setup GPIO in and out
# Setup GPIO in and out
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
@@ -28,14 +30,13 @@ cursor = db.cursor()
try:
while True:
curr_date = time.strftime("%Y-%m-%d")
curr_time = time.strftime("%H:%M:%S")
curr_date = datetime.now()
GPIO.output(TRIG, False)
time.sleep(0.5)
sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.00001)
sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
@@ -49,7 +50,7 @@ try:
distance = pulse_duration * 17150
distance = round(distance, 2)
if distance < 120:
count = count + 1
@@ -62,11 +63,10 @@ try:
# 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"):
if (curr_date.minute % 10 == 0) and (curr_date.second == 0):
try:
# 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
cursor.execute(sql)
# Commit your changes in the database
@@ -75,7 +75,7 @@ try:
# Rollback in case there is any error
db.rollback()
time.sleep(0.5)
sleep(0.5)
except KeyboardInterrupt:
db.close()
print("\nCtrl-C pressed cleaning up GPIO")