-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
236 lines (189 loc) · 7.1 KB
/
main.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
from ultrasonics import *
from gate import *
from camera import *
from Button import *
from CameraMotor import *
from RPLCD import CharLCD
import RPi.GPIO as GPIO
import time
import threading
import urllib.request
import json
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) #do not show any warnings
GPIO_Button = 17
GPIO_LDR = 16
GPIO_LED_IN = 23
GPIO_LED_OUT = 25
GPIO.setup(GPIO_LDR,GPIO.IN)
GPIO.setup(GPIO_Button,GPIO.IN)
GPIO.setup(GPIO_LED_IN,GPIO.OUT)
GPIO.setup(GPIO_LED_OUT,GPIO.OUT)
lcd = CharLCD(numbering_mode=GPIO.BCM ,cols=16, rows=2, pin_rs=27, pin_e=22, pins_data=[5, 6, 19, 26])
innerPlates = []
previous_state = "PassOut"
current_state = "Idle"
mode = "Automatic"
gate_state = "Closed"
gateCheckerurl = "http://dweet.io/get/latest/dweet/for/gateDoorChecker"
lockCheckerURl = "http://dweet.io/get/latest/dweet/for/manualSesame"
networkLock = 0
#s0 = Idle
#s1 = CheckCameraIn
#s2 = OpenGateIn
#s3 = PassIn
#s4 = CloseGate
#s5 = CheckCameraOut
#s6 = OpenGateOut
#s7 = PassOut
#s8 = Manual
#s9 = Network
def networkCommandChecker():
global networkLock
global gate_state
global mode
while True:
response = urllib.request.urlopen(lockCheckerURl)
data = json.loads(response.read().decode('utf-8'))
status = data['with'][0]['content']['manual']
if mode == 'Automatic':
if status == 'fo32FNLV64KL913VRN16FELn':
networkLock = 1
gateResponse = urllib.request.urlopen(gateCheckerurl)
gateData = json.loads(gateResponse.read().decode('utf-8'))
gateStatus = gateData['with'][0]['content']['gate']
if gateStatus == 'oTBBTSemsadrvmervvGate140220222023BTBBTBn':
gate_state = "Open"
OpenGate()
else:
gate_state = "Closed"
CloseGate()
else:
networkLock = 0
time.sleep(2)
def button_pressed_callback(channel):
global current_state
global mode
global gate_state
if Manual():
print("you pressed key for 3 sec, manual mode on.")
if mode == "Manual":
if gate_state == "Open":
print("please close the gate first!")
lcd.clear()
lcd.write_string("close the gate!")
time.sleep(2)
lcd.clear()
elif gate_state == "Closed":
mode = "Automatic"
lcd.clear()
lcd.write_string(" Atomatic Mode!")
time.sleep(2)
lcd.clear()
elif mode == "Automatic":
mode = "Manual"
lcd.clear()
lcd.write_string(" Manual Mode!")
time.sleep(2)
lcd.clear()
else :
if mode == "Manual" :
if gate_state == "Closed" :
OpenGate()
gate_state = "Open"
elif gate_state == "Open" :
CloseGate()
gate_state = "Closed"
GPIO.add_event_detect(GPIO_Button, GPIO.RISING, callback=button_pressed_callback, bouncetime=100)
netThread = threading.Thread(target=networkCommandChecker)
netThread.start()
while 1:
if mode == "Automatic" and networkLock == 0 :
if current_state == "Idle":
if previous_state != current_state:
lcd.clear()
lcd.write_string("...")
previous_state = "Idle"
print("state is: "+current_state)
if CheckUlt1():
current_state = "CheckCameraIn"
continue
elif CheckUlt2():
current_state = "CheckCameraOut"
elif current_state == "CheckCameraIn":
lcd.clear()
lcd.write_string(" Checking....")
previous_state = "CheckCameraIn"
print("state is: "+current_state)
inputScanner()
if GPIO.input(GPIO_LDR):
GPIO.output(GPIO_LED_IN,True)
recognizer,plate = CameraRecognized()
if recognizer:
OpenGate()
innerPlates.append(plate)
lcd.clear()
lcd.write_string("Plate: " + plate)
time.sleep(2)
lcd.clear()
current_state = "OpenGateIn"
else :
lcd.clear()
lcd.write_string(" not valid!")
time.sleep(2)
lcd.clear()
current_state = "Idle"
GPIO.output(GPIO_LED_IN,False)
elif current_state == "OpenGateIn":
previous_state = "OpenGateIn"
print("state is: "+current_state)
if CheckUlt1() == 0 and CheckUlt2() == 1:
current_state = "PassIn"
elif current_state == "PassIn":
previous_state = "PassIn"
print("state is: "+current_state)
if CheckUlt1() == 0 and CheckUlt2() == 0:
current_state = "CloseGate"
elif current_state == "CloseGate":
previous_state = "CloseGate"
print("state is: "+current_state)
CloseGate()
current_state = "Idle"
elif current_state == "CheckCameraOut":
lcd.clear()
lcd.write_string(" Checking....")
previous_state = "CheckCameraOut"
print("state is: "+current_state)
outputScanner()
if GPIO.input(GPIO_LDR):
GPIO.output(GPIO_LED_OUT,True)
recognizer,plate = CameraRecognized()
if recognizer:
OpenGate()
if plate in innerPlates:
innerPlates.remove(plate)
lcd.clear()
lcd.write_string("Plate: " + plate)
time.sleep(2)
lcd.clear()
current_state = "OpenGateOut"
else :
lcd.clear()
lcd.write_string(" not valid!")
time.sleep(2)
lcd.clear()
current_state = "Idle"
GPIO.output(GPIO_LED_OUT,False)
elif current_state == "OpenGateOut":
previous_state = "OpenGateOut"
print("state is: "+current_state)
if CheckUlt1() == 1 and CheckUlt2() == 0:
current_state = "PassOut"
elif current_state == "PassOut":
previous_state = "PassOut"
print("state is: "+current_state)
if CheckUlt1() == 0 and CheckUlt2() == 0:
current_state = "CloseGate"
else:
print("Error")
current_state = "Idle"