-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalServer.py
More file actions
240 lines (170 loc) · 8.99 KB
/
Copy pathFinalServer.py
File metadata and controls
240 lines (170 loc) · 8.99 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
231
232
233
234
235
236
237
238
239
240
import os
import socket
import sys
from _thread import *
import RPi.GPIO as GPIO
import time
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
red_pin = 14
orange_pin = 15
blue_pin = 18
red_flag = False
orange_flag = False
blue_flag = False
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup (red_pin, GPIO.OUT)
GPIO.setup (orange_pin, GPIO.OUT)
GPIO.setup (blue_pin, GPIO.OUT)
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 8000
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5)
def threaded_client(conn):
while True:
global serial_input
temp = serial_input[2] + serial_input[3]
humidity = serial_input[4] + serial_input[5]
data = conn.recv(1024)
reply = data.decode('utf-8')
global red_flag
global orange_flag
global blue_flag
if not data:
break
print ('\n\nYou: ' + reply)
################### RED ###################
if 'red' in reply or 'Red' in reply or 'read' in reply:
if 'on' in reply:
GPIO.output(red_pin, GPIO.HIGH)
conn.send(str.encode('RED light is ON!'))
red_flag = True
print ('Pi: RED light is ON!')
break;
#os.system("espeak \"Switching on the red light\"")
elif 'off' in reply or 'of' in reply:
GPIO.output(red_pin, GPIO.LOW)
conn.send(str.encode('RED light is OFF!'))
red_flag = False
print ('Pi: RED light is OFF!')
break;
#os.system("espeak \"Switching off the red light\"")
else:
if red_flag is True:
conn.send(str.encode('RED light status: ON'))
print ('Pi: RED light is ON!')
break;
else:
conn.send(str.encode('RED light status: OFF'))
print ('Pi: RED light is OFF!')
break;
############### ORANGE #########################
elif 'Orange' in reply or 'orange' in reply:
if 'on' in reply:
GPIO.output(orange_pin, GPIO.HIGH)
conn.send(str.encode('ORANGE light is ON!'))
orange_flag = True
print ('Pi: ORANGE light is ON!')
break;
#os.system("espeak \"Switching on the orange light\"")
elif 'off' in reply or 'of' in reply:
GPIO.output(orange_pin, GPIO.LOW)
conn.send(str.encode('ORANGE light is OFF!'))
orange_flag = False
print ('Pi: ORANGE light is OFF!')
break;
#os.system("espeak \"Switching off the orange light\"")
else:
if orange_flag is True:
conn.send(str.encode('ORANGE light status: ON'))
print ('Pi: ORANGE light is ON!')
break;
else:
conn.send(str.encode('ORANGE light status: OFF'))
print ('Pi: ORANGE light is OFF!')
break;
###################### BLUE ##############################
elif 'blue' in reply or 'Blue' in reply:
if 'on' in reply:
GPIO.output(blue_pin,GPIO.HIGH)
conn.send(str.encode('BLUE light is ON!'))
blue_flag = True
print ('Pi: BLUE light is ON!')
break;
#os.system("espeak \"Switching on the blue light\"")
elif 'off' in reply or 'of' in reply:
GPIO.output(blue_pin, GPIO.LOW)
conn.send(str.encode('BLUE light is OFF!'))
blue_flag = False
print ('Pi: BLUE light is OFF!')
break;
#os.system("espeak \"Switching off the blue light\"")
else:
if blue_flag is True:
conn.send(str.encode('BLUE light status: ON'))
print ('Pi: BLUE light is ON!')
break;
else:
conn.send(str.encode('BLUE light status: OFF'))
print ('Pi: BLUE light is OFF!')
break;
##################################################################
elif 'all' in reply or 'All' in reply or 'al' in reply:
if 'on' in reply:
GPIO.output(red_pin, GPIO.HIGH)
GPIO.output(blue_pin, GPIO.HIGH)
GPIO.output(orange_pin, GPIO.HIGH)
conn.send(str.encode('ALL lights are ON!'))
red_flag = True
blue_flag = True
orange_flag = True
print ('Pi: ALL lights are ON!')
break;
#os.system("espeak \"Switching on the red light\"")
elif 'off' in reply or 'of' in reply:
GPIO.output(red_pin, GPIO.LOW)
GPIO.output(blue_pin, GPIO.LOW)
GPIO.output(orange_pin, GPIO.LOW)
conn.send(str.encode('ALL lights are OFF!'))
red_flag = False
blue_flag = False
orange_flag = False
print ('Pi: ALL lights are OFF!')
break;
#os.system("espeak \"Switching off the red light\"")
###############################################################
elif 'temperature' in reply:
print ('Pi: Temperature is ' + temp + ' degrees')
conn.send(str.encode('Temperature: ' + temp + ' degrees'))
elif 'humidity' in reply:
print('Pi: Humidity is ' + humidity + '%')
conn.send(str.encode('Humidity: ' + humidity + '%'))
elif 'thank you' in reply:
print('Pi: You\'re welcome')
conn.send(str.encode('You\'re welcome'))
else:
print('Pi: What was that?')
conn.send(str.encode('What was that?'))
conn.close()
def socketinput(a):
global conn
global addr
conn, addr = s.accept()
#print ('Got connection from ' + addr[0] + ':' + str(addr[1]))
start_new_thread(threaded_client,(conn,))
while True:
serial_input = str(ser.readline())
room = serial_input[6] + serial_input[7] + serial_input[8]
if room[0] == '9' or room[0] == '8':
GPIO.output(blue_pin,GPIO.HIGH)
GPIO.output(orange_pin,GPIO.HIGH)
GPIO.output(red_pin,GPIO.HIGH)
red_flag = True
orange_flag = True
blue_flag = True
start_new_thread(socketinput,('abc',))