diff --git a/py/speech_coltrol_led.py b/py/speech_coltrol_led.py new file mode 100644 index 0000000..af508f5 --- /dev/null +++ b/py/speech_coltrol_led.py @@ -0,0 +1,28 @@ +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 "turn on the light" in command.lower(): + GPIO.output(LED_PIN, GPIO.HIGH) + print("Light ON") + elif "turn off the light" 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.")