Skip to content

Instantly share code, notes, and snippets.

@JohnPersano
Last active February 13, 2016 12:03
Show Gist options
  • Save JohnPersano/a49ba15fd3f1cd7695d3 to your computer and use it in GitHub Desktop.
Save JohnPersano/a49ba15fd3f1cd7695d3 to your computer and use it in GitHub Desktop.
Jasper Fan Module
# -*- 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))
@ram1505
Copy link

ram1505 commented Feb 13, 2016

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:

sudo: /home/pi/jasper/gpio/Fan/Fan: command not found
ERROR:client.brain:Failed to execute module
Traceback (most recent call last):
  File "/home/pi/jasper/client/brain.py", line 73, in query
    module.handle(text, self.mic, self.profile)
  File "/home/pi/jasper/client/modules/fan.py", line 34, in handle
    status = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-status"])
  File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['sudo', '/home/pi/jasper/gpio/Fan/Fan', '17', '-status']' returned non-zero exit status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment