Travel my Rhasspy response to a python program

I’m doing a project that envolves a raspberry, using rhasspy with debian. With rhasspy i need to use speech-to-text, using a microphone, and when i said for example: “Left” and rhasspy recognize [GetLeft], i need to get this text as an response in my python program using MQTT (internal or external). My question is, how could i made imediate connection of the response i get on rhasspy, and get it in my python code, because with the MQTT isnt working idk, but maybe i need to use an websocket or something? like websocket + mqtt? Basically i just want to have what i get on Rhasspy as an recognition, and travel it to my python code.
I know its a big question, but if someone could help me with that i will be very gratefull!!!

Hey I just went through this process myself so I’ll share what I did:

  1. Set Rhasspy to external MQTT
  2. Install Mosquitto man page | Eclipse Mosquitto mosquitto MQTT broker (
    apt-get install mosquitto )
  3. Add this to your python code:

#pip install paho-mqtt
#paho-mqtt · PyPI
import paho.mqtt.client as mqtt

#MQTT broker settings
broker_address = “localhost”
broker_port = 1883
subscribe_get_intent = “hermes/intent/#”
subscribe_get_not_recognized = “hermes/nlu/intentNotRecognized”

def on_connect(client, userdata, flags, rc):
“”“Called when connected to MQTT broker.”“”
client.subscribe(subscribe_get_intent)
client.subscribe(subscribe_get_not_recognized)

def on_disconnect(client, userdata, flags, rc):
“”“Called when disconnected from MQTT broker.”“”
client.reconnect()

def on_message(client, userdata, msg):
“”“Called each time a message is received on a subscribed topic.”“”
if “YOUR INTENT’S TOPIC” in str(msg.topic):
try:
#this will have the json intent data
nlu_intent = json.loads(msg.payload)
print(nlu_intent)
except Exception as e:
print(f"Failed to parse intents: {e}")

#Run the MQTT client loop
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect(“localhost”, 1883)
client.loop_forever()

I believe how this works is mosquitto will read port localhost:1883 (1883 = default port for unencrypted mqtt) and be a publisher for events. Your Rhasspy will show these values when you enable external mqtt as well. When you connect from your python code, you can view events published by rhasspy through MQTT because rhasspy is doing the same thing from within its code (I assume).

Hope that helps, lmk if you have trouble

1 Like

Thanks a lot for your help!

My rhasspy is runing with debian.
I set Rhasspy to external MQTT like you said, and then when i run “rhasspy --profile en” in my terminal, it shows a loop of errors, and i couldnt connect again to my localhost12101 of rhasspy.
I dont know how i could acess rhasspy again, since it doesnt allow me to come back because of the errors of connection of the external MQTT.
Do i really need to use external mqtt? couldnt i use the internal mqtt and adjust the program you gave to me?

EDIT (minutes later): I got acess again to the localhost, i have located the profile.json of Rhasspy and turn:
“mqtt”: {
“enabled”: “false”
},
This means that if i dont have a connection of the external MQTT i couldnt acess Rhasspy, so should i use internal MQTT or should i try and fix the issue to use the external MQTT?

EDIT 2 (more minutes later): I tried your program with the internal MQTT and it worked very well, thanks a lot!!! (You just forgot to “import json” at the start), and i correct some errors with CHAT GPT, being the final result this program:

import json
import paho.mqtt.client as mqtt

MQTT broker settings

broker_address = “localhost”
broker_port = 12183 #internal MQTT
subscribe_get_intent = “hermes/intent/#”
subscribe_get_not_recognized = “hermes/nlu/intentNotRecognized”

def on_connect(client, userdata, flags, rc):
# Called when connected to MQTT broker
client.subscribe(subscribe_get_intent)
client.subscribe(subscribe_get_not_recognized)

def on_disconnect(client, userdata, flags, rc):
# Called when disconnected from MQTT broker
client.reconnect()

def on_message(client, userdata, msg):
# Called each time a message is received on a subscribed topic
if “GetLeft” in str(msg.topic):
try:
# This will have the JSON intent data
nlu_intent = json.loads(msg.payload)
print(nlu_intent)
except Exception as e:
print(f"Failed to parse intents: {e}")

Create the MQTT client instance

client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message

Connect to the MQTT broker

client.connect(broker_address, broker_port)

Run the MQTT client loop

client.loop_forever()

Response when i do run and talk to the mic (by the way, my wakeword is Blanky):
{‘input’: ‘left’, ‘intent’: {‘intentName’: ‘GetLeft’, ‘confidenceScore: 1.0},
siteId’: ‘default’, ‘id’: None, ‘slots’: [], ‘sessionId’: ‘default-Blanky-e267b8
4b-ba25-4dae-9c56-cfr799c1b44b’, ‘customData’: ‘Blanky’, ‘asrTokens’: [[{‘value’
: Left, confidence’: 1.0, ‘rangeStart’: 0, 'rangeEnd": 4, ‘time’: None}]], 'a
srConfidence: 1.0, ‘rawInput’: ‘left’, ‘wakewordId’: ‘Blanky’, ‘lang’: None}

THANKS A LOT FOR ALL YOUR HELP!!!

1 Like

Awesome so glad you got it working!!

1 Like