Skip to content

Instantly share code, notes, and snippets.

@cyrus007
Created June 15, 2015 07:38
Show Gist options
  • Save cyrus007/9b68b4606977f1335792 to your computer and use it in GitHub Desktop.
Save cyrus007/9b68b4606977f1335792 to your computer and use it in GitHub Desktop.
Jasper module to control garagedoor via MQTT
# -*- coding: utf-8-*-
import logging
import re
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
WORDS = ["GARAGE", "OPEN", "CLOSE", "STATUS"]
PRIORITY = 4
MQTTHOST = "<my_host>"
def handle(text, mic, profile):
"""
Handle garage events based on user input
Arguments:
text -- user-input, typically transcribed speech
mic -- used to interact with the user (for both input and output)
profile -- contains information related to the user (e.g., phone number)
"""
def on_connect(client, userdata, flags, rc):
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("<status_topic>")
def on_message(client, userdata, msg):
mic.say("The garage is "+str(msg.payload))
client.disconnect()
logger = logging.getLogger(__name__)
logger.debug("Garage: got text=" + text)
if re.search(r'\bclose\b', text, re.IGNORECASE):
logger.debug("Garage: publishing close to <cmd_topic>")
publish.single("<cmd_topic>", "close", hostname=MQTTHOST, client_id="jasper")
mic.say("Closing the garage now.")
if re.search(r'\bopen\b', text, re.IGNORECASE):
logger.debug("Garage: publishing open to <cmd_topic>")
publish.single("<cmd_topic>", "open", hostname=MQTTHOST, client_id="jasper")
mic.say("Opening the garage now.")
if re.search(r'\bstatus\b', text, re.IGNORECASE):
logger.debug("Garage: publishing status to <cmd_topic>")
publish.single("<cmd_topic>", "status", hostname=MQTTHOST, client_id="jasper")
client = mqtt.Client(client_id="jasper")
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTTHOST, 1883)
client.loop_forever()
def isValid(text):
"""
Returns True if input is related to garage
Arguments:
text -- user-input, typically transcribed speech
"""
if re.search(r'\bopen the garage\b', text, re.IGNORECASE):
return True
elif re.search(r'\bclose the garage\b', text, re.IGNORECASE):
return True
elif re.search(r'\bstatus of garage\b', text, re.IGNORECASE):
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment