29 lines
717 B
Python
29 lines
717 B
Python
import speech_recognition as sr
|
|
import RPi.GPIO as GPIO
|
|
import time
|
|
|
|
LED_PIN = 17
|
|
|
|
# GPIO setup
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setup(LED_PIN, GPIO.OUT)
|
|
|
|
def trigger_action(command):
|
|
if "light" in command.lower():
|
|
GPIO.output(LED_PIN, GPIO.HIGH)
|
|
print("Light ON")
|
|
elif "dark" in command.lower():
|
|
GPIO.output(LED_PIN, GPIO.LOW)
|
|
print("Light OFF")
|
|
|
|
recognizer = sr.Recognizer()
|
|
with sr.Microphone() as source:
|
|
print("Say a command...")
|
|
audio = recognizer.listen(source)
|
|
try:
|
|
cmd = recognizer.recognize_sphinx(audio)
|
|
print("Command: {}".format(cmd))
|
|
trigger_action(cmd)
|
|
except sr.UnknownValueError:
|
|
print("Could not understand audio.")
|