pilight/py/speech_coltrol_led.py

34 lines
1005 B
Python

import speech_recognition as sr
import RPi.GPIO as GPIO
import time
LED_PIN = 17 # Number of GPIO physical pin
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
# turn LED on or off with correct command
def trigger_action(command):
# turn LED on with command fire
if "fire" in command.lower():
GPIO.output(LED_PIN, GPIO.HIGH)
print("Light ON")
# turn LED off with command night
elif "night" in command.lower():
GPIO.output(LED_PIN, GPIO.LOW)
print("Light OFF")
recognizer = sr.Recognizer()
with sr.Microphone() as source:
# audio input
print("Say a command...")
audio = recognizer.listen(source)
try:
# runs the live recored audio through PocketSphinx. Send the iterpreted audio as text to function trigger_action
cmd = recognizer.recognize_sphinx(audio)
print("Command: {}".format(cmd))
trigger_action(cmd)
except sr.UnknownValueError:
print("Could not understand audio.")