pilight/py/speech_coltrol_led.py
2025-12-06 14:44:35 +01:00

29 lines
743 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 "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.")