-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.py
303 lines (231 loc) · 7.95 KB
/
all.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import RPi.GPIO as GPIO
import time
from time import ctime
from time import sleep
import httplib, urllib
import math
import datetime
coords = {'longitude' : 0.34, 'latitude' : 51.1 }
WORKING = 0
SUCCESS = 1
TIMEOUT = 2
SWITCHABORT = 3
FORWARD = 1
REVERSE = 2
STOPMOTOR = 3
MAX_TIME = 30
CLOSE_TIME = 10 #time to take to close
OPEN_TIME = 10 #time to open
OPEN_DISTANCE = 70
CLOSED_DISTANCE = 20
debugmode = True #Used to stop pushover messages being sent
#Pushover Keys
APP_TOKEN = "ap1zxxfpdcnbkkfk5fk9daoob78hpb"
USER_ID = "uZ8cbsrmeoMiMJEU6MzHTVKNwrPUr2"
#GPIO PINS
GPIO_MOTOR_FORWARD = 21
GPIO_MOTOR_REVERSE = 15
GPIO_TRIG = 23 #Distance sensor
GPIO_ECHO = 24 #Distance sensor
GPIO_UP_SWITCH = 29
def debugprint(message):
if (debugmode == True):
print (message)
return
class Sun:
def getSunriseTime( self, coords ):
return self.calcSunTime( coords, True )
def getSunsetTime( self, coords ):
return self.calcSunTime( coords, False )
def getCurrentUTC( self ):
now = datetime.datetime.now()
return [ now.day, now.month, now.year ]
def calcSunTime( self, coords, isRiseTime, zenith = 90.8 ):
# isRiseTime == False, returns sunsetTime
day, month, year = self.getCurrentUTC()
longitude = coords['longitude']
latitude = coords['latitude']
TO_RAD = math.pi/180
#1. first calculate the day of the year
N1 = math.floor(275 * month / 9)
N2 = math.floor((month + 9) / 12)
N3 = (1 + math.floor((year - 4 * math.floor(year / 4) + 2) / 3))
N = N1 - (N2 * N3) + day - 30
#2. convert the longitude to hour value and calculate an approximate time
lngHour = longitude / 15
if isRiseTime:
t = N + ((6 - lngHour) / 24)
else: #sunset
t = N + ((18 - lngHour) / 24)
#3. calculate the Sun's mean anomaly
M = (0.9856 * t) - 3.289
#4. calculate the Sun's true longitude
L = M + (1.916 * math.sin(TO_RAD*M)) + (0.020 * math.sin(TO_RAD * 2 * M)) + 282.634
L = self.forceRange( L, 360 ) #NOTE: L adjusted into the range [0,360)
#5a. calculate the Sun's right ascension
RA = (1/TO_RAD) * math.atan(0.91764 * math.tan(TO_RAD*L))
RA = self.forceRange( RA, 360 ) #NOTE: RA adjusted into the range [0,360)
#5b. right ascension value needs to be in the same quadrant as L
Lquadrant = (math.floor( L/90)) * 90
RAquadrant = (math.floor(RA/90)) * 90
RA = RA + (Lquadrant - RAquadrant)
#5c. right ascension value needs to be converted into hours
RA = RA / 15
#6. calculate the Sun's declination
sinDec = 0.39782 * math.sin(TO_RAD*L)
cosDec = math.cos(math.asin(sinDec))
#7a. calculate the Sun's local hour angle
cosH = (math.cos(TO_RAD*zenith) - (sinDec * math.sin(TO_RAD*latitude))) / (cosDec * math.cos(TO_RAD*latitude))
if cosH > 1:
return {'status': False, 'msg': 'the sun never rises on this location (on the specified date)'}
if cosH < -1:
return {'status': False, 'msg': 'the sun never sets on this location (on the specified date)'}
#7b. finish calculating H and convert into hours
if isRiseTime:
H = 360 - (1/TO_RAD) * math.acos(cosH)
else: #setting
H = (1/TO_RAD) * math.acos(cosH)
H = H / 15
#8. calculate local mean time of rising/setting
T = H + RA - (0.06571 * t) - 6.622
#9. adjust back to UTC
UT = T - lngHour
UT = self.forceRange( UT, 24) # UTC time in decimal format (e.g. 23.23)
#10. Return
hr = self.forceRange(int(UT), 24)
min = round((UT - int(UT))*60,0)
return {
'status': True,
'decimal': UT,
'hr': hr,
'min': min
}
def forceRange( self, v, max ):
# force v to be >= 0 and < max
if v < 0:
return v + max
elif v >= max:
return v - max
return v
def howfar():
#GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_TRIG,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)
GPIO.output(GPIO_TRIG, False)
debugprint ("Waiting For Sensor To Settle")
time.sleep(0.1)
GPIO.output(GPIO_TRIG, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIG, False)
while GPIO.input(GPIO_ECHO)==0:
pulse_start = time.time()
while GPIO.input(GPIO_ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"
return distance
#end of function howfar
def pushover(push_text):
title = "Hen Hotel :" +ctime()
conn = httplib.HTTPSConnection("api.pushover.net:443")
if debugmode == False:
conn.request("POST", "/1/messages.json",urllib.urlencode({"token": APP_TOKEN ,"user":
USER_ID, "message": push_text,"title": title}),
{ "Content-type": "application/x-www-form-urlencoded" })
print "Pushover: " + push_text
return # End of function push
def timer():
ticks = time.time()
print ticks
return ticks
def motor(direction):
if direction == FORWARD:
GPIO.setup(GPIO_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(GPIO_MOTOR_REVERSE, GPIO.OUT)
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.HIGH)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.LOW)
debugprint("Motor:Forward")
elif direction == REVERSE:
GPIO.setup(GPIO_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(GPIO_MOTOR_REVERSE, GPIO.OUT)
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.LOW)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.HIGH)
debugprint("Motor: Reverse")
elif direction == STOPMOTOR:
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.LOW)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.LOW)
debugprint("Motor: Stop")
else:
debugprint ("Invalid input to motor function")
return #enf of function mortor
def manualswitch():
return 0
def closeDoor():
status = WORKING
finishtime = timer() + CLOSE_TIME
print finishtime
motor(REVERSE) #start the motor closing
while status == WORKING:
if timer() >= finishtime: #timer expired
debugprint ("TIMER EXPIRED")
status = TIMEOUT
pushover("Time Out - Check Ramp")
elif howfar() <= CLOSED_DISTANCE: #Distance sensor shows closed
print "Sensor states closed"
status = SUCCESS #All good
elif manualswitch() != 0:
pushover("Manual Abort")
debugprint ("Manual Abort")
status = SWITCHABORT
motor(STOPMOTOR)
return(status)
def openDoor():
status = WORKING
finishtime = timer() + OPEN_TIME
print finishtime
motor(FORWARD) #start the motor closing
while status == WORKING:
if timer() >= finishtime: #timer expired
debugprint ("TIMER EXPIRED")
status = TIMEOUT
pushover("Time Out - Check Ramp")
elif howfar() >= OPEN_DISTANCE: #Distance sensor shows closed
print "Sensor states open"
status = SUCCESS #All good
elif manualswitch() != 0:
pushover("Manual Abort")
debugprint ("Manual Abort")
status = SWITCHABORT
motor(STOPMOTOR)
return(status)
#The Main App starts here
print("Starting")
"""
if (howfar() == CLOSED_DISTANCE)
if (opendoor() == SUCCESS) #Get into a know state at startup.
door_status = open
else:
pushover("Check the system, opendoor failed on startup")
GPIO.cleanup()
raise SystemExit
quit = False
while (quit == False)
sun = Sun()
timenow = time()
if (timenow > sun.getSunriseTime(coords))
print sun.getSunriseTime(coords)
# Sunrise time UTC (decimal, 24 hour format)
print sun.getSunriseTime( coords )['hr']
print sun.getSunriseTime( coords )['min']
print sun.getSunsetTime( coords )['hr']
print sun.getSunsetTime( coords )['min']
"""
GPIO.setmode(GPIO.BOARD)
print "close door"
closeDoor()
sleep(3)
print "open door"
openDoor()
print("Finishing")