Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1241

Python • Send IR Command with Python

$
0
0
Hello everyone guys!! I need your help for a project I'm trying to do, namely: I created a voice assistant in Python so that it recognizes my voice and sends IR commands (I want to control a hifi device). The problem is that I can't integrate the command into the script, meaning when I say play it executes the command but nothing happens. The command I should send was captured with PiIR which works well and which, launched from the terminal, is the following: piir play --gpio 17 --file denon.json play. Does anyone know how to integrate this command into the script so that if he hears play he executes that command? I leave you the code I created. I already have permissions to have access to gpios too. Waiting for your kind hand I greet you :)

Code:

  #!/usr/bin/env python3import speech_recognition as srimport pyttsx3import subprocessfrom datetime import datetime# Inizializza il motore TTS e imposta la voce italiana femminileengine = pyttsx3.init()engine.setProperty('voice', 'it+f3')  # Assicurati che questa voce sia disponibile sul tuo sistemadef speak(text):    engine.say(text)    engine.runAndWait()def listen():    r = sr.Recognizer()    with sr.Microphone() as source:        print("In ascolto...")        r.adjust_for_ambient_noise(source, duration=1)        audio = r.listen(source)    try:        text = r.recognize_google(audio, language="it-IT")        print("Hai detto: ", text)        return text.lower()    except sr.UnknownValueError:        print("Non ho capito il comando.")    except sr.RequestError as e:        print("Errore nel servizio di riconoscimento; {0}".format(e))    return ""def main():    speak("Ciao, Dimmi come posso aiutarti.")    while True:        command = listen()        if command == "":            continue        if "ciao" in command:            speak("Ciao, come posso aiutarti?")        elif "orario" in command:            now = datetime.now().strftime("%H:%M")            speak("L'orario attuale è " + now)        elif "play" in command:            speak("Eseguo il comando play")            subprocess.run(["piir", "play", "--gpio", "17", "--file", "denon.json", "play"])        elif "esci" in command or "addio" in command:            speak("Arrivederci, a presto!")            break        else:            speak("Comando non riconosciuto, riprova.")if __name__ == "__main__":    main()

Statistics: Posted by Dende90 — Thu Feb 20, 2025 1:36 pm



Viewing all articles
Browse latest Browse all 1241

Trending Articles