Last active
February 13, 2016 12:03
-
-
Save JohnPersano/a49ba15fd3f1cd7695d3 to your computer and use it in GitHub Desktop.
Jasper Fan Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import random | |
import re | |
import subprocess | |
import os | |
WORDS = ["FAN", "YES", "NO", "ON", "OFF"] | |
GPIO_PIN_FAN = "17"; | |
def handle(text, mic, profile): | |
# Look for keyword 'ON' in speech, if present turn fan on | |
if("ON" in text): | |
mic.say("Turning fan on.") | |
status = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-on"]) | |
# Fan program will return a '0' if the fan has been turned on | |
if not("0" in status): | |
mic.say("Im sorry...I could not turn the fan on...please try again later") | |
# Look for keyword 'OFF' or 'OF' in speech, if present turn fan off | |
elif("OFF" in text or "OF" in text): | |
mic.say("Turning fan off.") | |
status = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-off"]) | |
# Fan program will return a '1' if the fan has been turned off | |
if not("1" in status): | |
mic.say("Im sorry...I could not turn the fan off...please try again later") | |
# If no keywords in speech, check fan status and prompt appropriate user input | |
else: | |
mic.say("Checking fan status.") | |
status = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-status"]) | |
# If the status of the GPIO pin is '0', the fan is on (relay module works opposite) | |
if("0" in status): | |
mic.say("The fan is on...would you like me to turn it off?") | |
def response(text): | |
print text | |
if("YES" in text): | |
mic.say("Turning fan off.") | |
subStatus = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-off"]) | |
# Fan program will return a '1' if the fan has been turned off | |
if not("1" in subStatus): | |
mic.say("Im sorry...I could not turn the fan off...please try again later") | |
else: | |
mic.say("Leaving fan on.") | |
# Listen for a response to the question | |
response(mic.activeListen()) | |
# If the status of the GPIO pin is '1', the fan is off (relay module works opposite) | |
elif("1" in status): | |
mic.say("The fan is off...would you like me to turn it on?") | |
def response(text): | |
if("YES" in text): | |
mic.say("Turning fan on.") | |
subStatus = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-on"]) | |
if not("0" in subStatus): | |
mic.say("Im sorry...I could not turn the fan on...please try again later") | |
else: | |
mic.say("Leaving fan off.") | |
# Listen for a response to the question | |
response(mic.activeListen()) | |
else: | |
mic.say("Something went wrong while fetching the fan status.") | |
def isValid(text): | |
return bool(re.search(r'\bfan\b', text, re.IGNORECASE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, how do I use this module with Jasper? I placed the above fan.py under my /jasper/client/module and when I called the keyword, I get the following error: