-
Notifications
You must be signed in to change notification settings - Fork 1
/
PiRelay_Voice_Control.py
83 lines (72 loc) · 2.61 KB
/
PiRelay_Voice_Control.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
import speech_recognition as sr
import time, datetime
import PiRelay
import re
relay1 = PiRelay.Relay("RELAY1")
relay2 = PiRelay.Relay("RELAY2")
relay3 = PiRelay.Relay("RELAY3")
relay4 = PiRelay.Relay("RELAY4")
sample_rate = 48000
chunk_size = 2048
r = sr.Recognizer()
device_id = 0
mic_list = sr.Microphone.list_microphone_names()
for mic in mic_list:
if mic == 'Yeti Stereo Microphone: USB Audio (hw:0,0)':
device_id = mic_list.index(mic)
while True:
with sr.Microphone(device_index = device_id, sample_rate = sample_rate,
chunk_size = chunk_size) as source:
r.adjust_for_ambient_noise(source)
print("Say Something")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
text = text.lower()
print("you said: " + text)
if re.search("rel", text):
if re.search('on', text):
if re.search("1", text):
print("Turn On Relay 1")
relay1.on()
elif re.search("2", text):
print("Turn On Relay 2")
relay2.on()
elif re.search("3", text):
print("Turn On Relay 3")
relay3.on()
elif re.search("4", text):
print("Turn On Relay 4")
relay4.on()
elif re.search("all", text):
print("Turn On All Relay")
relay1.on()
relay2.on()
relay3.on()
relay4.on()
if re.search('off', text):
if re.search("1", text):
print("Turn Off Relay 1")
relay1.off()
elif re.search("2", text):
print("Turn Off Relay 2")
relay2.off()
elif re.search("3", text):
print("Turn Off Relay 3")
relay3.off()
elif re.search("4", text):
print("Turn Off Relay 4")
relay4.off()
elif re.search("all", text):
print("Turn Off All Relays")
relay1.off()
relay2.off()
relay3.off()
relay4.off()
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
except AssertionError:
print("Problem with Audio Source")
break