├── mosquitto_sub.sh ├── LICENSE.md ├── README.md └── Mqtt.py /mosquitto_sub.sh: -------------------------------------------------------------------------------- 1 | mosquitto_sub -d -t hal9000/+/+ 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 Djordje Ungar. http://djordjeungar.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jasper-Module-MQTT 2 | 3 | Jasper MQTT Module for my [HAL9000 Raspberry PI Instructable](http://www.instructables.com/id/RaspberryPI-HAL9000/) 4 | 5 | When triggered it publishes a simple MQTT event using the following topic: 6 | ``` 7 | hal9000/*device*/*index* 8 | ``` 9 | Available devices: 10 | * device 11 | * room 12 | * light 13 | * sensor 14 | * door 15 | 16 | Available indexes: 17 | * one 18 | * two 19 | * three 20 | * ... 21 | * ten 22 | 23 | Available topic messages: 24 | * on 25 | * off 26 | * true 27 | * false 28 | * open 29 | * close 30 | 31 | 32 | ## Steps to install MQTT Module 33 | 34 | * Install the python Mosquitto package: 35 | ``` 36 | sudo pip install paho-mqtt 37 | ``` 38 | * run the following commands in order: 39 | ``` 40 | git clone https://github.com/ArtBIT/jasper-module-mqtt.git 41 | cp jasper-module-mqtt/Mqtt.py 42 | #i.e. cp jasper-module-mqtt/Mqtt.py /usr/local/lib/jasper/client/modules/ 43 | ``` 44 | * Edit `~/.jasper/profile.yml` and add the follwing at the bottom: 45 | ``` 46 | mqtt: 47 | hostname: 'your.mqtt.broker.hostname.or.ip' 48 | port: 1883 49 | protocol: 'MQTTv31' # Note: this should be either MQTTv31 or MQTTv311, I had problems with Ubuntu broker and MQTTv311 50 | ``` 51 | * Restart the Pi: 52 | ``` 53 | sudo reboot 54 | ``` 55 | ## Congrats, JASPER MQTT Module is now installed and ready for use. 56 | Here are some examples: 57 | ``` 58 | YOU: Light one off 59 | JASPER: *publishes an mqtt event* topic:hal9000/light/one message:off 60 | YOU: Door two close 61 | JASPER: *publishes an mqtt event* topic:hal9000/door/two message:close 62 | ``` 63 | 64 | -------------------------------------------------------------------------------- /Mqtt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8-*- 2 | # vim: set expandtab:ts=4:sw=4:ft=python 3 | import re 4 | 5 | import paho.mqtt.publish as publish 6 | import paho.mqtt.client as mqtt 7 | 8 | NUMBERS = ["ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", 9 | "SEVEN", "EIGHT", "NINE", "TEN"] 10 | DEVICES = ["DEVICE", "ROOM", "LIGHT", "SENSOR", "DOOR"] 11 | PAYLOADS = ["ON", "OFF", "OF", "TRUE", "FALSE", "OPEN", "CLOSE", "STATUS"]+NUMBERS 12 | 13 | WORDS = ["MOSQUITTO"] + DEVICES + NUMBERS + PAYLOADS 14 | PRIORITY = 4 15 | 16 | 17 | def handle(text, mic, profile): 18 | """ 19 | Responds to user-input, typically speech text, by sending a 20 | mosquitto publish event 21 | Arguments: 22 | text -- user-input, typically transcribed speech 23 | mic -- used to interact with the user (for both input and output) 24 | profile -- contains information related to the user (e.g., phone 25 | number) 26 | """ 27 | words = text.split(' ') 28 | if words[0] not in DEVICES: 29 | return mic.say(words[0]+" not found in the list of devices") 30 | if words[1] not in NUMBERS: 31 | return mic.say(words[1]+" not found in the list of valid indexes") 32 | if words[2] not in PAYLOADS: 33 | return mic.say(words[2]+" is not found in the list of valid payloads") 34 | topic = '/'.join(['hal9000']+words[0:2]) 35 | payload = words[2] 36 | if payload == 'OF': 37 | payload = 'OFF' 38 | if 'protocol' in profile['mqtt'] and profile['mqtt']['protocol'] == 'MQTTv311': 39 | protocol = mqtt.MQTTv311 40 | else: 41 | protocol = mqtt.MQTTv31 42 | publish.single(topic.lower(), payload=payload.lower(), client_id='hal9000', 43 | hostname=profile['mqtt']['hostname'], port=profile['mqtt']['port'], 44 | protocol=protocol) 45 | 46 | 47 | def isValid(text): 48 | """ 49 | Returns True if the input is related to the meaning of life. 50 | Arguments: 51 | text -- user-input, typically transcribed speech 52 | """ 53 | 54 | regex = "(" + "|".join(DEVICES) + ") (" + "|".join(NUMBERS) + ") (" + "|".join(PAYLOADS) + ")" 55 | return bool(re.search(regex, text, re.IGNORECASE)) 56 | --------------------------------------------------------------------------------