-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.py
177 lines (151 loc) · 5 KB
/
lock.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import RPi.GPIO as GPIO
from pathlib import Path
import time, logging
##////////////////logging Setup/////////////////
# create logger with the name 'SleepyServer'
logger = logging.getLogger('DoorLock')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('DoorLock.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
##this part is to easily seperate lines before the new logs are created - not really needed
startingstring = " \n" + " \n"
logger.addHandler(fh)
logger.addHandler(ch)
logger.info(startingstring + startingstring + startingstring )
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
#print("Lock Loading...")
logger.info("Lock Loading...")
##////////////////
##////////////////Declaring pins & motor control stuff/////////////////
GPIO.setmode(GPIO.BOARD)
pins = [13,11,15,12]
for pin in pins:
GPIO.setup(pin,GPIO.OUT)
### Motor Variables
rotations = 50
max_rotations = 90 # this is the max it should ever rotate
timings = 0.001
#Setting the sequence for rotation
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
##///////////////
##////////////////Check if file exists/////////////////
try:
my_file = Path("status.txt")
if my_file.is_file():
#print("file exists")
logger.info("file exists")
else:
#print("status.txt does not exists and will be created")
logger.info("status.txt does not exists and will be created")
f = open("status.txt", "x")
f.close()
f = open("status.txt", "w")
f.write("unlocked")
f.close()
except:
print("Unable to find or create file status.txt ")
logger.info("Unable to find or create file status.txt ")
##////////////////
##////////////////Lock code/////////////////
def Lock():
logger.info("I locked the door")
#print("I locked the door")
f = open("status.txt", "w")
f.write("locked")
f.close()
#How many times to do a rotation
if rotations > 0 and rotations <= max_rotations:
for i in range(rotations):
#start from the start of the sequence and move up
for step in range(0,8,+1):
#loops through the pins
for pin in range(4):
#Sets the pins as per the sequence
GPIO.output(pins[pin], halfstep_seq[step][pin])
time.sleep(timings)
for pin in range(4):
#Switches all pins off to see if it helps with heat issue
GPIO.output(pins[pin], 0)
def Unlock():
logger.info("I un-locked the door")
#print("I un-locked the door")
f = open("status.txt", "w")
f.write("unlocked")
f.close()
#How many times to do a rotation
if rotations > 0 and rotations <= max_rotations:
for i in range(rotations):
#start from the end of the sequence and move down
for step in range(8,0,-1):
step = step-1
#loops through the pins
for pin in range(4):
#Sets the pins as per the sequence
GPIO.output(pins[pin], halfstep_seq[step][pin])
time.sleep(timings)
for pin in range(4):
#Switches all pins off to see if it helps with heat issue
GPIO.output(pins[pin], 0)
##////////////////If file status was locked on last startup/////////////////
try: # this code builds in redundancy, if you dont have your key simply cycle the power to the device and it will unlock
f = open("status.txt", "rt")
contents = f.read()
f.close()
time.stop(1)
if contents == "locked":
logger.info("Locked on last shutdown, unlocking door")
Unlock()
f = open("status.txt", "w")
f.write("unlocked")
f.close()
else:
#print("lock was already unlocked...")
logger.info("lock was already unlocked...")
except:
#print("Unable to detect status.txt on boot")
logger.info("Unable to detect status.txt on boot")
##////////////////
##////////////////Alternate lock/////////////////
def alternate_lock():
f = open("status.txt", "rt")
contents = f.read()
f.close()
try:
if contents == "locked":
Unlock()
elif contents == "unlocked":
Lock()
except:
#print("Was not able to read file status.txt")
logger.info("Was not able to read file status.txt")
'''
if __name__ == "__main__":
##////////////////Main/////////////////
try:
while True:
alternate_lock()
time.sleep(1)
alternate_lock()
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
'''