-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
80 lines (63 loc) · 1.89 KB
/
Copy pathrequest.py
File metadata and controls
80 lines (63 loc) · 1.89 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
# Import the required Modules
import speech_recognition as sr
import requests
import pyttsx3
URL = "https://cloud.boltiot.com/remote/<API_KEY>/digitalWrite?{}deviceName=BOLT1119308"
# Initialize the engine and recognizer
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def main():
# Listen to the user
mic = sr.Microphone()
with mic as source:
print("Listening....")
engine.say("What is your command")
engine.runAndWait()
recognizer.adjust_for_ambient_noise(source, duration=0.5)
try:
audio = recognizer.listen(source, timeout=3, phrase_time_limit=6)
command = recognizer.recognize_google(audio, language='en-IN').lower().strip()
print(command)
except sr.WaitTimeoutError:
write_and_speak('Timeout Error, Try again')
main()
except sr.UnknownValueError:
write_and_speak('Cannot understand, Try again')
main()
except sr.RequestError:
write_and_speak('No internet connection or Invalid Key')
return
# command for ON
if 'turn on' in command or 'switch on' in command or 'open' in command:
lookup_device(command, state='HIGH')
# command for OFF
elif 'turn off' in command or 'switch off' in command or 'close' in command:
lookup_device(command, state='LOW')
else:
write_and_speak('Invalid command')
def write_and_speak(text: str):
print(text)
engine.say(text)
engine.runAndWait()
def lookup_device(command, state):
pins = {
'light': 0,
'fan': 1,
'door': 2,
'projector': 3,
'curtains': 4
}
for device, pin in pins.items():
if device in command:
perform_action(device, pin, state)
return
def perform_action(device, pin, state):
assert state in ['HIGH', 'LOW']
eq = {'HIGH':'On', 'LOW':'Off'}
res = requests.get(URL.format(f'pin={pin}&state={state}&'))
if res.json()['success'] == None:
write_and_speak('The server is Offline')
else:
write_and_speak(f'Turning {eq[state]} the {device}')
if __name__ == '__main__':
main()