Alexa End Point Made Simple

#1

I just wanted to pass this along as I found it a great alternative for Alexa Endpoint establishment directly to the Pi.

If you are setting up Alexa to control your device, you will hit a point in the setup to configure your Pi as an endpoint. You don’t have to actually set up an apache server. You can handle it in python and a free service called PageKite (although they’d like you to donate which I will!).

To do so:
First, go to PageKite.net and set yourself up with a kite. This will give you URL that will never change that will allow Alexa’s cloud service to route through it to your Pi.

Set your Pi to automatically run the Pagekite client as a service as instructed here:
http://pagekite.net/wiki/Howto/GNULinux/DebianPackage/
http://pagekite.net/wiki/Howto/GNULinux/ConfigureYourSystem/

Now, to program your endpoint directly on your Pi, install python Flask if you haven’t done so with this:
sudo apt-get install python2.7-dev python-dev python-pip
sudo pip install Flask flask-ask

At this point, the sky is the limit to control your Matrix with an Alexa skill written in python. I wrote a separate C++ file to do all the heavy lifting, but have a simple python script to broker the commands and Alexa responses.

Here is an example python script that handles a skill with Intents called Start, Stop Door, and Reset:

from flask import Flask
from flask_ask import Ask, statement, convert_errors
import logging
import os
app = Flask(__name__)
ask = Ask(app, '/')
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
@ask.intent('Start', mapping={'status': 'status'})
def get_basket(status):
    os.system("/home/pi/cc all &")
    return statement('Sure, Sean.  Wally is taking the hook {} now.  Please stay clear.'.format(status))

@ask.intent('Stop', default={'name':'nothing'})
def stop_cc(name):
    os.system("/home/pi/cc stop &")
    return statement('Okay.  Wally is stopping all motors now.  He apologizes for this problem.')

@ask.intent('Door', mapping={'command':'command'})
def door_commands(command):
    if command =="open":
       os.system("/home/pi/cc opendoor &")
    if command =="close":
       os.system("/home/pi/cc closedoor &")
    return statement("Okay, Wally, please {} your door.  You're doing great.".format(command))

@ask.intent('Reset', default={'reset_slot':'nothing'})
def reset_cc(reset_slot):
    os.system("/home/pi/cc reset &")
    return statement("Okay.  Sorry for the trouble.  I'll have Walley reset to his closed position. He apologizes for being such an inconvenience and promises to do better.  Please don't turn him into $


if __name__ == '__main__':
        port = 5000 #the custom port you want
        app.run(host='0.0.0.0', port=port)

Hope this helps your next project!
Sean

3 Likes
#2

I improved my approach with a C++ endpoint versus a Python endpoint. This allows one more control over the matrix, Alexa, and the Pi.

You can find the blog on it here: https://www.element14.com/community/community/project14/homeautomation/blog/2019/03/10/home-automation-mojo-you-must-know-part-5-the-house-hub

1 Like