Created
July 8, 2014 18:51
-
-
Save JohnPersano/3781f501b74e039ed1b0 to your computer and use it in GitHub Desktop.
Jasper Temperature 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
import random | |
import re | |
import subprocess | |
import time | |
import serial | |
WORDS = ["TEMPERATURE", "TEMP"] | |
SERIAL_REQUEST = "T" | |
GPIO_PIN_FAN = "17" | |
THRESHOLD = 80 | |
def handle(text, mic, profile): | |
holdMessages = ["Give me a second while I check the sensor", | |
"Let me check the sensor for you.", | |
"Fetching data from sensor."] | |
mic.say(random.choice(holdMessages)) | |
serialCom = serial.Serial('/dev/ttyACM0', 9600, timeout = 1) | |
time.sleep(2) | |
serialCom.write(SERIAL_REQUEST) | |
output = serialCom.readline() | |
serialCom.close() | |
# If communication has completed without valid values | |
if("ERROR" in output): | |
mic.say("I'm sorry...I could not get a valid input from the sensor.") | |
# If communication has completed with valid values | |
else: | |
# Convert output to integers | |
temperature = int(output[:2]) | |
humidity = int(output[5:7]) | |
messages = ["The current temperature is %s degrees with a humidity of %s percent", | |
"It is currently %s degrees with a humidity of %s percent", | |
"Right now it is %s degrees with a humidity of %s percent"] | |
mic.say(random.choice(messages) % (temperature, humidity)) | |
if (temperature > THRESHOLD): | |
fanStatus = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-status"]) | |
if("1" in fanStatus): | |
fanMessages = ["Boy...it sure is hot...would you like me to turn the fan on?", | |
"Gee...it is sweltering...do you want me to cool you off?", | |
"Hmm...temperatures this high are no fun...do you want me to turn on the fan?", | |
"I cant help but notice how sweaty you are...would you like me to turn the fan on?", | |
"My circuts are getting sweaty...can I turn the fan on?"] | |
fanMessage = random.choice(fanMessages) | |
mic.say(fanMessage) | |
def response(text): | |
if("YES" in text): | |
mic.say("Turning fan on.") | |
fanOn = subprocess.check_output(["sudo", "/home/pi/jasper/gpio/Fan/Fan", GPIO_PIN_FAN, "-on"]) | |
if not("0" in fanOn): | |
mic.say("I'm sorry...I could not turn the fan on...please try again later.") | |
else: | |
mic.say("Leaving fan off.") | |
response(mic.activeListen()) | |
def isValid(text): | |
return bool(re.search(r'\b(temperature|temp)\b', text, re.IGNORECASE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment