-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile_driver.py
More file actions
271 lines (248 loc) · 9.03 KB
/
mobile_driver.py
File metadata and controls
271 lines (248 loc) · 9.03 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
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
import speech_recognition as sr
import pyttsx3
import socket
import sys
import pickle
HEADERSIZE = 10
commander = {'R_LED': 0, 'Y_LED': 0, 'W_LED': 0, 'B_LED': 0, 'BUZZER_1': 0, 'BUZZER_5': 0, 'DISPLAY': '', 'KILL': 0}
listener = sr.Recognizer()
engine = pyttsx3.init()
engine.setProperty('rate', 150)
voices = engine.getProperty('voices')
def watch_for_call():
while True:
watch_word = take_initial_command()
if watch_word == 'hello ':
talk("Hello...I am your Personal Assistant")
talk("how can i help you?")
while True:
run_neptune()
def connect_to_server():
#TO WORK WITH PORT FORWARDING ON 7052
# "127.0.0.1" # The server's hostname or IP address
host = '127.0.0.1'
port = 7052 #random
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
watch_for_call()
def talk(text):
engine.say(text)
engine.runAndWait()
def take_initial_command():
try:
with sr.Microphone() as source:
watch = 'empty'
listener.adjust_for_ambient_noise(source)
print('listening for watch word...')
voice = listener.listen(source)
watch = listener.recognize_google(voice)
watch = watch.lower()
print(watch)
except KeyboardInterrupt:
exit(0)
except:
pass
return watch
def take_command():
try:
with sr.Microphone() as source:
command = 'empty'
listener.adjust_for_ambient_noise(source)
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'neptune' in command:
command = command.replace('neptune', '')
print(command)
except KeyboardInterrupt:
exit(0)
except:
pass
return command
def led_dict_logic(command):
if 'led' in command:
command.replace('led', '')
if 'yellow' in command:
command.replace('yellow','')
if 'on' in command:
commander.update({'Y_LED':1})
print(commander)
send_to_server(commander)
return "Turned on Yellow LED."
elif 'off' in command:
commander.update({'Y_LED':0})
print(commander)
send_to_server(commander)
return "Turned off Yellow LED."
elif 'blink' in command:
commander.update({'Y_LED_B':1, 'Y_LED':0})
dict_updated = True
return "Blinking Yellow LED 5 times..."
else:
return "I did not understand what to do with the Yellow LED."
elif 'red' in command:
command.replace('red','')
if 'on' in command:
commander.update({'R_LED':1})
print(commander)
send_to_server(commander)
return "Turned on Red LED."
elif 'off' in command:
commander.update({'R_LED':0})
send_to_server(commander)
return "Turned off Red LED."
elif 'blink' in command:
commander.update({'R_LED_B':1, 'R_LED':0})
dict_updated = True
return "Blinking Red LED 5 times..."
else:
return "I did not understand what to do with the Red LED."
elif 'white' in command:
command.replace('white','')
if 'on' in command:
commander.update({'W_LED':1})
print(commander)
send_to_server(commander)
return "Turned on White LED."
elif 'off' in command:
commander.update({'W_LED':0})
print(commander)
send_to_server(commander)
return "Turned off White LED."
elif 'blink' in command:
commander.update({'W_LED_B':1, 'W_LED':0})
dict_updated = True
return "Blinking White LED 5 times..."
else:
return "I did not understand what to do with the White LED."
elif 'blue' in command:
command.replace('blue','')
if 'on' in command:
commander.update({'B_LED':1})
print(commander)
send_to_server(commander)
return "Turned on Blue LED."
elif 'off' in command:
commander.update({'B_LED':0})
print(commander)
send_to_server(commander)
return "Turned off Blue LED."
elif 'blink' in command:
commander.update({'B_LED_B':1, 'B_LED':0})
dict_updated = True
return "Blinking Blue LED 5 times..."
else:
return "I did not understand what to do with the Blue LED."
elif 'warm' in command:
command.replace('warm','')
if 'on' in command:
commander.update({'R_LED':1, 'Y_LED':1})
print(commander)
send_to_server(commander)
return "Turned on warm colours."
elif 'off' in command:
commander.update({'R_LED':0, 'Y_LED':0})
print(commander)
send_to_server(commander)
return "Turned off warm colours"
else:
return "Please specify an action"
elif 'cool' in command:
command.replace('cool','')
if 'on' in command:
commander.update({'B_LED':1, 'W_LED':1})
print(commander)
send_to_server(commander)
return "Turned on cool colours."
elif 'off' in command:
commander.update({'B_LED':0, 'W_LED':0})
print(commander)
send_to_server(commander)
return "Turned off cool colours"
else:
return "Please specify an action"
elif 'all' in command:
command.replace('all','')
if 'on' in command:
commander.update({'R_LED':1, 'Y_LED':1, 'B_LED':1, 'W_LED':1})
print(commander)
send_to_server(commander)
return "Turned on LEDs"
elif 'off' in command:
commander.update({'R_LED':0, 'Y_LED':0, 'B_LED':0, 'W_LED':0})
print(commander)
send_to_server(commander)
return "Turned off LEDs"
else:
return "I'm not sure what you want all the LEDs to do."
else:
return "Did not understand which colour you want to perform action on."
def buzzer_dict_logic(command):
if 'buzzer' in command:
command.replace('buzzer','')
if 'buzz' in command:
command.replace('buzz','')
if 'one time' in command:
commander.update({'BUZZER_1':1})
print(commander)
send_to_server(commander)
return "Beep"
else:
commander.update({'BUZZER_5':1})
print(commander)
send_to_server(commander)
return "Beep Beep Beep Beep Beep"
else:
return "I'm sorry I don't understand what you want me to do with the buzzer."
def display_dict_logic(command):
if 'display' in command:
string = command.replace('display', '')
string = string.lstrip()
commander.update({'DISPLAY': string.upper()})
print(commander)
send_to_server(commander)
return "Data sent to LCD"
def run_neptune():
command = take_command()
print(command)
if 'empty' == command:
talk('')
elif 'display' in command:
talk("sending to LCD")
res = display_dict_logic(command.lower())
print(res)
elif 'shutdown' in command:
commander.update({'KILL':1})
send_to_server("KILL")
elif 'led' in command:
res = led_dict_logic(command)
print(res)
elif 'buzz' in command:
res = buzzer_dict_logic(command)
print(res)
elif 'bye' in command:
talk("Until we meet again")
watch_for_call()
else:
talk('I did not catch that')
''' NOTE: THIS CODE HAS A BUG
def send_to_server(command):
print("SENDING TO SERVER")
if command == "KILL":
#send KILL request to server pi
msg = pickle.dumps(command, -1)
msg = bytes(f"{len(msg):<{HEADERSIZE}}", 'utf-8')+msg
s.sendall(msg)
s.close()
exit("SUCCESSFUL RUN OF PROGRAM")
msg = pickle.dumps(command, -1)
msg = bytes(f"{len(msg):<{HEADERSIZE}}", 'utf-8')+msg
s.sendall(msg)
print("SENT TO SERVER")
command.update({'DISPLAY':'', 'BUZZER_1':0, 'BUZZER_5':0})
reply = s.recv(1024)
print(reply.decode('utf-8'))
'''
if __name__ == "__main__":
connect_to_server()