Skip to content

Commit

Permalink
First push
Browse files Browse the repository at this point in the history
  • Loading branch information
jbnunn committed Feb 28, 2016
0 parents commit c10a9f4
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
@@ -0,0 +1,60 @@
# Alexa-Keurig

## Description
Control your Keurig B60 through your Amazon Echo, using the Alexa Skills Kit and AWS IOT service. You'll create an Alexa Skills Kit (ASK) app that fires off requests to AWS Lambda. You will register a Rasberry Pi as a [device](https://docs.aws.amazon.com/iot/latest/developerguide/create-thing.html) in AWS IoT. The Alexa app will call a function in Lambda, which will send a request to the Pi to activate a servo, which pushes down the brew button. Yes, it's a hack, but it's fun.

## Usage
1. Create an Alexa Skills Kit (ASK) app, using the intent schema and sample utterances in this repo. Choose an invocation name like _my Keurig_.
2. Use the code in `main.py` as your Lambda function that the ASK skill will call. Substitute `amzn1.echo-sdk-ams.app.<your-alexa-skills-id>` with the ID of the ASK skill you created. When you create the function, set the "Event Source" to the Alexa Skills Kit.
4. Modify your ASK skill with the [ARN](http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) of your newly created Lambda function.
5. Wire up a Raspberry Pi according to the schematic in [RPiKeurig_bb.png](/RPiKeurig_bb.png). The servo should be glued in place underneath the plastic silver top (see *Caveats*), so that when it fires, it rotates to push one of the brew buttons. I use the largest button.

![View of servo](keurig1.jpg)
![Keurig from the side](keurig2.jpg)

6. Test your interactions with the ASK console. When you've got it working, try it on your Echo: `Alexa, ask my Keurig to brew me a cup of coffee.`

## Caveat
- *YOU WILL MOST LIKELY DAMAGE YOUR KEURIG--AND VOID YOUR WARRANTY--IF YOU DO THIS.* There are YouTube videos on how to do the dismantling, but _this is the hardest part._ You only need access to the buttons underneath the plastic silver top that covers the LCD and buttons.

## Final Product
![Video of the final product](brewing.mov)

## Alexa Skills Kit Documentation
The documentation for the Alexa Skills Kit is available on the [Amazon Apps and Services Developer Portal](https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/).

## Resources
Here are a few direct links to Alexa and Lambda documentation:

- [Getting Started](https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/getting-started-guide)
- [Invocation Name Guidelines](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/choosing-the-invocation-name-for-an-alexa-skill)
- [Developing an Alexa Skill as an AWS Lambda Function](https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/docs/developing-an-alexa-skill-as-a-lambda-function)

## Disclaimers
The authors claim no responsibility for damages to your Keurig or property by use of the code within. You may incur charges using AWS Lambda, but there is a free tier available for up to 1M requests per month that you may be eligible for--check [AWS Lambda Pricing](https://aws.amazon.com/lambda/pricing/) for details.

## License
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
Binary file added RPiKeurig.fzz
Binary file not shown.
Binary file added RPiKeurig_bb.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added brewing.mov
Binary file not shown.
103 changes: 103 additions & 0 deletions device.py
@@ -0,0 +1,103 @@
#!/usr/bin/python3

# required libraries
import sys
import ssl
import paho.mqtt.client as mqtt
import time
import json
import RPi.GPIO as GPIO

def main():
mqttc = mqtt.Client(client_id="RPi")

mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_subscribe = on_subscribe

mqttc.tls_set("./certs/rootCA.pem",
certfile="./certs/cert.pem",
keyfile="./certs/privateKey.pem",
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)

mqttc.connect("<your-endpoint-here>.iot.us-east-1.amazonaws.com", port=8883)
print("Connect is done")
mqttc.loop_forever()


# called while client tries to establish connection with the server
def on_connect(mqttc, obj, flags, rc):

if rc == 0:
print ("Subscriber Connection status code: " +
str(rc) + " | Connection status: successful")
elif rc == 1:
print ("Subscriber Connection status code: " + str(rc) +
" | Connection status: Connection refused")

mqttc.subscribe("$aws/things/coffee/shadow/update/accepted", 1)

# called when a topic is successfully subscribed to
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " +
str(granted_qos) + "data" + str(obj))

# called when a message is received by a topic
def on_message(mqttc, obj, msg):
print("Received message from topic: " + msg.topic)
print(str(msg.payload))
thingdata = json.loads(str(msg.payload))

if 'desired' in thingdata['state']:
brewstate = thingdata['state']['desired']['brew'].lower()

if brewstate == "on" or brewstate == "brew" or brewstate == "start":
print("Will brew!")
payload = '{"state" : { "reported" : { "brew" : "on" } } }'
mqttc.publish("$aws/things/coffee/shadow/update", payload)
start_brew()
elif brewstate == "off" or brewstate == "stop":
print("Stoping brew!")
payload = '{"state" : { "reported" : { "brew" : "off" } } }'
mqttc.publish("$aws/things/coffee/shadow/update", payload)
stop_brew()
else:
print("Invalid state")
payload = '{"state" : { "reported" : { "brew" : "off" } } }'
mqttc.publish("$aws/things/coffee/shadow/update/rejected", payload)
stop_brew()

def start_brew():
p.start(7.5)
GPIO.output(LIGHTPIN, GPIO.HIGH)

p.ChangeDutyCycle(9.3)
time.sleep(.5)
p.ChangeDutyCycle(8.0)
time.sleep(1)

stop_brew()

def stop_brew():
GPIO.output(LIGHTPIN, GPIO.LOW)

if __name__ == '__main__':
SERVOPIN = 18
LIGHTPIN = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(SERVOPIN, GPIO.OUT)
GPIO.setup(LIGHTPIN, GPIO.OUT)

p = GPIO.PWM(SERVOPIN, 50)
p.ChangeDutyCycle(9.3)

try:
main()
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
sys.exit()
13 changes: 13 additions & 0 deletions intent_schema.json.txt
@@ -0,0 +1,13 @@
{
"intents": [
{
"intent": "BrewIntent",
"slots": []
},
{
"intent": "HelpIntent",
"slots": []
}

]
}
Binary file added keurig1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added keurig2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 117 additions & 0 deletions main.py
@@ -0,0 +1,117 @@
import boto3
import json
import urllib
import urllib2

def lambda_handler(event, context):

if event['session']['application']['applicationId'] != "amzn1.echo-sdk-ams.app.<your-alexa-skills-id>":
print "Invalid Application ID"
else:
sessionAttributes = {}

if event['session']['new']:
onSessionStarted(event['request']['requestId'], event['session'])
if event['request']['type'] == "LaunchRequest":
speechlet = onLaunch(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)
elif event['request']['type'] == "IntentRequest":
speechlet = onIntent(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)
elif event['request']['type'] == "SessionEndedRequest":
speechlet = onSessionEnded(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)

# Return a response for speech output
return(response)

# Called when the session starts
def onSessionStarted(requestId, session):
print("onSessionStarted requestId=" + requestId + ", sessionId=" + session['sessionId'])

# Called when the user launches the skill without specifying what they want.
def onLaunch(launchRequest, session):
# Dispatch to your skill's launch.
getWelcomeResponse()

# Called when the user specifies an intent for this skill.
def onIntent(intentRequest, session):
intent = intentRequest['intent']
intentName = intentRequest['intent']['name']

# Dispatch to your skill's intent handlers
if intentName == "BrewIntent":
return brewIntent(intent)
elif intentName == "HelpIntent":
return getWelcomeResponse()
else:
print "Invalid Intent (" + intentName + ")"
raise

# Called when the user ends the session.
# Is not called when the skill returns shouldEndSession=true.
def onSessionEnded(sessionEndedRequest, session):
# Add cleanup logic here
print "Session ended"

def brewIntent(intent):
repromptText = "You can say brew me a cup of coffee"
shouldEndSession = True

speechOutput = "Ok, I'm brewing your coffee"
cardTitle = speechOutput

# Make a call to AWS IoT to update the shadow. The Raspberry Pi
# connected to the Keurig watches the shadow topic for instructions
# to start the brew
client = boto3.client('iot-data')
response = client.update_thing_shadow(
thingName='coffee',
payload=b'{"state" : { "desired" : { "brew" : "start" }}}'
)

repromptText = "I didn't understand that."
shouldEndSession = True

return(buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))


def getWelcomeResponse():
sessionAttributes = {}
cardTitle = "Welcome"
speechOutput = """You can ask me to brew you a cup of coffee by saying brew me a cup of coffee"""

# If the user either does not reply to the welcome message or says something that is not
# understood, they will be prompted again with this text.
repromptText = speechOutput
shouldEndSession = True

return (buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))

# --------------- Helpers that build all of the responses -----------------------
def buildSpeechletResponse(title, output, repromptText, shouldEndSession):
return ({
"outputSpeech": {
"type": "PlainText",
"text": output
},
"card": {
"type": "Simple",
"title": "SessionSpeechlet - " + title,
"content": "SessionSpeechlet - " + output
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": repromptText
}
},
"shouldEndSession": shouldEndSession
})

def buildResponse(sessionAttributes, speechletResponse):
return ({
"version": "1.0",
"sessionAttributes": sessionAttributes,
"response": speechletResponse
})
35 changes: 35 additions & 0 deletions sample_utterancs.txt
@@ -0,0 +1,35 @@
BrewIntent pour my coffee
BrewIntent pour me a cup
BrewIntent pour me a coffe
BrewIntent start brewer
BrewIntent start brewing
BrewIntent start the brewer
BrewIntent start my brewer
BrewIntent brew me a cup of coffee
BrewIntent brew some hot chocolate
BrewIntent brew me some hot chocolate
BrewIntent brew my hot chocolate
BrewIntent pour me a cup of coffee
BrewIntent give me a cup of cofffee
BrewIntent pour me a cup of hot chocolate
BrewIntent pour me a cup of hot water
BrewIntent pour me a cup of water
BrewIntent make some hot chocolate
BrewIntent make me some hot chocolate
BrewIntent pour some hot water
HelpIntent help
HelpIntent help me
HelpIntent what can I ask you
HelpIntent get help
HelpIntent to help
HelpIntent to help me
HelpIntent what commands can I ask
HelpIntent what commands can I say
HelpIntent what can I do
HelpIntent what can I use this for
HelpIntent what questions can I ask
HelpIntent what can you do
HelpIntent what do you do
HelpIntent how do I use you
HelpIntent how can I use you
HelpIntent what can you tell me

0 comments on commit c10a9f4

Please sign in to comment.