-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoor.py
More file actions
230 lines (223 loc) · 6.24 KB
/
Copy pathdoor.py
File metadata and controls
230 lines (223 loc) · 6.24 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import RPi.GPIO as GPIO
import time
import boto3
import Adafruit_ADS1x15
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
pwm = GPIO.PWM(12, 100)
pwm.start(5)
currentServoPos = 0
angleStep = 3
easyOpen = False
def calcAngle(ang):
return ang/180.0*22.0+2.5
setServo(0)
setServo(180)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('LightsStatus')
def getLock():
try:
response = table.get_item(
Key={
'InfoId': 'Lights'
}
)
except Exception as e:
print(e.response['Error']['Message'])
else:
item = response['Item']
return item['lockState'] == 'true'
def getAuto():
try:
response = table.get_item(
Key={
'InfoId': 'Lights'
}
)
except Exception as e:
print(e.response['Error']['Message'])
else:
item = response['Item']
return item['autoBrightness'] == 'true'
def setBrightness(brightness):
if brightness<0:
brightness = 0
if brightness>100:
brightness = 100
print("brightness to: "+str(brightness))
response = table.update_item(
Key={
'InfoId': 'Lights'
},
UpdateExpression="set brightness = :b",
ExpressionAttributeValues={
':b': str(brightness)
},
ReturnValues="UPDATED_NEW"
)
return response['Attributes']['brightness']
def getBrightness():
try:
response = table.get_item(
Key={
'InfoId': 'Lights'
}
)
except:
print(e.response['Error']['Message'])
else:
item = response['Item']
return int(item['brightness'])
def setLock(lock):
global wasLocked
wasLocked = lock
if lock:
lockS = 'true'
else:
lockS = 'false'
response = table.update_item(
Key={
'InfoId': 'Lights'
},
UpdateExpression="set lockState = :l",
ExpressionAttributeValues={
':l': str(lockS)
},
ReturnValues="UPDATED_NEW"
)
if response['Attributes']['lockState'] == 'true':
return 'LOCKED'
else:
return 'UNLOCKED'
def setServo(ang):
time.sleep(.05)
setLED(True, True, False)
if ang==-1:
pwm.ChangeDutyCycle(0)
return
global angleStep
global currentServoPos
#print(currentServoPos)
if abs(ang-currentServoPos)>angleStep:
if ang-currentServoPos>0:
currentServoPos = currentServoPos+angleStep
pwm.ChangeDutyCycle(calcAngle(currentServoPos))
else:
currentServoPos = currentServoPos-angleStep
pwm.ChangeDutyCycle(calcAngle(currentServoPos))
setServo(ang)
else:#Within stepSize degrees of our target
pwm.ChangeDutyCycle(calcAngle(ang))
setServo(-1)#Turn off servo after we dont need it moving anymore
def getSomeoneHome():
try:
response = table.get_item(
Key={
'InfoId': 'Lights'
}
)
except Exception as e:
print(e.response['Error']['Message'])
else:
item = response['Item']
return item['home']=='True'
def lock():
global easyOpen
setServo(lockAng)
setServo(neuAng)
setLock(True)
if easyOpen:
setLED(False, False, True)
else:
setLED(True, False, False)
def unlock():
setServo(unlockedAng)
setServo(neuAng)
setLock(False)
setLED(False, True, False)
def setLED(red, green, blue):
GPIO.output(17, red)
GPIO.output(27, green)
GPIO.output(22, blue)
wasLocked = False
timeDoorOpen = 0
lockAng = 0
neuAng = 90
unlockedAng = 180
setServo(lockAng)
setServo(unlockedAng)
setServo(neuAng)
lock()
ticker = 0
timeCovered = 0
targetVal = 1700
tol = 200
brightnessTimes = {17: 100, 18: 75, 19: 50, 20: 20, 21:10, 22:5, 23: 2, 0: 1}
doorClosed = False
try:
while True:
adcVal = adc.read_adc(3, gain=GAIN)
ambi = adc.read_adc(0, gain=GAIN)
doorClosed = GPIO.input(23)
if ticker%5==0:# once every time iterations, or .5 seconds
currentLock = getLock()
if currentLock and not wasLocked:
lock()
if not currentLock and wasLocked:
time.sleep(2)
#timeDoorOpen = timeDoorOpen + 20
nowHour = time.localtime()[3]
if nowHour in brightnessTimes:
#setBrightness(brightnessTimes[nowHour])
pass
unlock()
wasLocked = currentLock
now = datetime.datetime.now()
print("Door open time: "+str(timeDoorOpen)+", Time Covered:"+str(timeCovered)+", Locked:"+str(wasLocked)+", ADC:"+str(adcVal)+", easyOpen:"+str(easyOpen)+", ambi:"+str(ambi)+", hour: "+str(now.hour))
brightness = getBrightness()
if ticker%20==0 and getAuto() and (brightness>10 or (12 < now.hour < 20) and doorClosed):
if ambi<targetVal-tol:
setBrightness(brightness+2)
elif ambi>targetVal+tol:
setBrightness(brightness-2)
if adcVal<100:
timeCovered = timeCovered + 1
else:
timeCovered = 0
if timeCovered>=60 or (timeCovered>1 and easyOpen):
easyOpen = False
unlock()
if GPIO.input(24):
if wasLocked:
timeDoorOpen = timeDoorOpen + 10
doorClosed = False
unlock()
else:
lock()
time.sleep(1)
if GPIO.input(25):
easyOpen = True
unlock()
ticker = ticker + 1
if timeDoorOpen>10 and doorClosed:
#time.sleep(.2)
lock()
if not doorClosed:
timeDoorOpen = timeDoorOpen + 1
else:
timeDoorOpen = 0
time.sleep(.1)
except Exception as e:
print(e)
finally:
pwm.stop()
GPIO.cleanup()