Skip to content

Instantly share code, notes, and snippets.

@akramhussein
Created October 25, 2017 10:19
Show Gist options
  • Save akramhussein/64dbd6993ed1251bab5cf52328c90e2e to your computer and use it in GitHub Desktop.
Save akramhussein/64dbd6993ed1251bab5cf52328c90e2e to your computer and use it in GitHub Desktop.
Snips.ai - always listen MQTT app
import json
import paho.mqtt.client as mqtt
# MQTT client to connect to the bus
mqtt_client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
print('Connected')
mqtt_client.subscribe('#') # subscribe to all messages
mqtt_client.publish('hermes/asr/toggleOn') # Start recording audio
# Process a message as it arrives
def on_message(client, userdata, msg):
print ' - {}'.format(msg.topic)
if msg.topic == 'hermes/asr/toggleOff':
mqtt_client.publish('hermes/asr/toggleOn')
elif msg.topic == 'hermes/asr/textCaptured':
# hermes/asr/textCaptured: {"text":"hi my name is John","likelihood":0.9222572,"seconds":5.284}
if len(msg.payload) > 0:
data = json.loads(msg.payload)
text = data.get('text')
if text:
print '* {}'.format(text)
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect('localhost', 9898)
mqtt_client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment