-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlog.py
More file actions
65 lines (54 loc) · 3.04 KB
/
log.py
File metadata and controls
65 lines (54 loc) · 3.04 KB
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
65
import os
import RPi.GPIO as GPIO
import time
from datetime import datetime
logfile = open("/home/pi/GarageWeb/static/log.txt","a")
logfile.write(datetime.now().strftime(" Program Starting -- %Y/%m/%d -- %H:%M -- Hello! \n"))
logfile.close()
print(datetime.now().strftime(" Program Starting -- %Y/%m/%d -- %H:%M -- Hello! \n"))
print " Control + C to exit Program"
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
time.sleep(1)
TimeDoorOpened = datetime.strptime(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S') #Default Time
DoorOpenTimer = 0 #Default start status turns timer off
DoorOpenTimerMessageSent = 1 #Turn off messages until timer is started
try:
while 1 >= 0:
time.sleep(1)
if DoorOpenTimer == 1: #Door Open Timer has Started
currentTimeDate = datetime.strptime(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')
if (currentTimeDate - TimeDoorOpened).seconds > 900 and DoorOpenTimerMessageSent == 0:
print "Your Garage Door has been Open for 15 minutes"
DoorOpenTimerMessageSent = 1
if GPIO.input(16) == GPIO.HIGH and GPIO.input(18) == GPIO.HIGH: #Door Status is Unknown
logfile = open("/home/pi/GarageWeb/static/log.txt","a")
logfile.write(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Opening/Closing \n"))
logfile.close()
print(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Opening/Closing \n"))
while GPIO.input(16) == GPIO.HIGH and GPIO.input(18) == GPIO.HIGH:
time.sleep(.5)
else:
if GPIO.input(16) == GPIO.LOW: #Door is Closed
logfile = open("/home/pi/GarageWeb/static/log.txt","a")
logfile.write(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Closed \n"))
logfile.close()
print(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Closed"))
DoorOpenTimer = 0
if GPIO.input(18) == GPIO.LOW: #Door is Open
logfile = open("/home/pi/GarageWeb/static/log.txt","a")
logfile.write(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Open \n"))
logfile.close()
print(datetime.now().strftime("%Y/%m/%d -- %H:%M:%S -- Door Open"))
#Start Door Open Timer
TimeDoorOpened = datetime.strptime(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')
DoorOpenTimer = 1
DoorOpenTimerMessageSent = 0
except KeyboardInterrupt:
logfile = open("/home/pi/GarageWeb/static/log.txt","a")
logfile.write(datetime.now().strftime(" Log Program Shutdown -- %Y/%m/%d -- %H:%M -- Goodbye! \n"))
logfile.close()
print(datetime.now().strftime(" Log Program Shutdown -- %Y/%m/%d -- %H:%M -- Goodbye! \n"))
GPIO.cleanup()