Develop Your Own Raspberry Pi Alexa Skill and Control Pi Remotely

Voice control is an increasingly important component in smart homes. For this purpose, Amazon provides skills that run on Amazon Echo Dot, Fire TV sticks, smartphones and other devices. In this way, Raspberry Pi Alexa skills can also be created in a simple manner.

The skills can be published optionally and even money can be made with them. In this tutorial, we will create an Alexa skill which will execute commands on our local Raspberry Pi. We will go through all the points of that process step by step.

Various other providers such as Google (“OK Google”), Apple (Siri) and Microsoft (Cortana) also have language assistants and offer opportunities to write your own skills. If you are interested, we can show those in future tutorials.

This tutorial includes building simple LED controls, but many other scenarios can be built through small adjustments.

Required Hardware Parts

The software runs completely on the Pi. We also need the hardware which will be operated by voice control. For this we will use the following:

An Amazon device with Alexa is also required. These are:

 

Preparation and Raspberry Pi Programming

Before we create the actual Alexa skill, we have to set up and configure a few things. First of all, we need an Amazon developer account. We first create an account on developer.amazon.com or log in with the existing Amazon account.

Then we take care of the setup on the Raspberry Pi.

Connecting the Raspberry Pi Hardware and Installing the Software

Before you start the Pi, connect a simple LED with the long end to GPIO 17 and the short end to Ground. Here you can see the pin assignment:

Raspberry Pi GPIO Pin Assignment

Raspberry Pi GPIO Pin Assignment

Ideally, you can also connect a small resistor (220Ω) between Ground and the LED.

Since this example is very simple, I would like to give you some better project ideas:

Installation

Now connect e.g. via SSH with the Raspberry Pi. First of all we install the required software. We use the Python packages Flask or Flask-Ask, which make it very easy to write Amazon Alexa skills:

python3 -m pip install Flask-Ask
pip3 install --upgrade setuptools
pip3 install 'cryptography<2.2'

As you can see, we used a version less than 2.2 of the package cryptography. With newer versions. there are often errors in connection with Flask.

Now let’s create the python file that contains our code:

sudo python3 alexaskill.py

This file has the following content:

Save the file with Ctrl + O and close the editor with Ctrl + X. Then we run the file:

python3 alexaskill.py

Accessibility Outside of the Local Network

We now have a Raspberry Pi server running, but this can only be accessed via the local network. It must be publicly accessible so that Amazon can call up our Raspberry Pi later.

We could either use a dDNS service and enable the ports in the router, or we could use ngrok. This has the advantage that we can save the configuration.

Open a new terminal (do not close the old one) and enter the following:

wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
unzip ngrok-stable-linux-arm.zip

Afterwards we can start:

./ngrok http 5000

NgrokWe then need the second URL (https) for Amazon.

Attention: As a non-registered user, the connection will be reset after 8 hours and you will get a new URL. You can change this by registering and providing your token:

./ngrok authtoken <TOKEN>

It is also possible to choose the subdomain yourself. You can find more information about ngrok in the documentation.

./ngrok http -subdomain=myveryownamazonalexaskill 5000

 

By the way: We can also start both commands (Python script and ngrok) via autostart.

 

Setting up the Raspberry Pi Alexa Skill in the Amazon Developer Account

We press the “Create Skill” button in the Amazon Developer Console. Here we assign a name (e.g. “Raspberry Pi”), choose the language and the “Custom” model. The hosting method is “Provision your own”.  Then we click on “Create Skill” in the top right corner.

Create Raspberry Pi Alexa Skill - New Skill

Then we press the plus sign next to “Slot Types” (Interaction Models) and create a new type called “STATUS”:

Create Raspberry Pi Alexa Skill - Slot Type

For this we create two “values”. These are given the names “on” and “off” and we also define all possible synonyms (these are the same synonyms that are also specified in the Python script):

Amazon Alexa Skill Slot Values

As the last configuration step, we create an “intent” (click on the plus sign) with the name “LightIntent” (the same name which was also specified in the script):

Create Raspberry Pi Alexa Skill - Add Intent

In the lower part, we first add the “intent slot”. Here we choose the type “STATUS” and also call it “status” (lower case):

Raspberry Pi Alexa Skill Intent Slot

In the upper part, you can now enter all possible voice commands, such as “turn off the light”. By double-clicking on a word, you can replace it with the slot:

Raspberry Pi Alexa Skill Light Intent

Think about different combinations that you want to use later. Usually, the call is e.g. as following:

Alexa, tell Raspberry Pi, [turn on light / turn off light / …]

The second part is therefore important and should be defined in this part. Save the model and press “Build Model”.

Here again the generated JSON file. You can also just paste them in

 

Last but not least, we have to specify the server URL to which the commands are sent. To do this, click on “Endpoint” on the left, select HTTPs and paste the URL of ngrok into “Default Region”. Also, choose the following option: “My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority“.

Amazon Alexa Skill Endpoint

Don’t forget to save it.

 

Test the New Raspberry Pi Alexa Skill – Locally and on Amazon Echo Devices

We now want to test the Raspberry Pi Alexa Skill. There are various options for this, which we will go into. Ideally, both are carried out in succession:

1. Amazon Developer Console – Test

In the Developer Console, there is the possibility to send commands via voice recording or text under the tab “Test”. To do this, you have to allow your browser to use your microphone. You can see that command on the right side.

2. Live Testing on Amazon Echo (Dot), Fire TV Stick, etc.

We want our developed skill to run permanently on Amazon devices. To do this, first, we have to add all devices to our account. How this works is, among other things, described here. You can see all Alexa devices either in the smartphone app (Android, iOS) or in the Amazon dashboard.

You can also easily add new devices in the apps. As soon as the device is linked to the account, the skill can already be used.

 

What projects do you have in mind? Further ideas and suggestions as comments are welcome.

7 Responses

  1. When I test this the py script just keeps returning “I’m sorry this command is not possible.”
    Checked everything many times.
    What am I missing?

    Reply
  2. Great tutorial! However, I cannot seem to get it to work. I have followed the entire tutorial and I am using the exact JSON code provided. WHen I try a command such as “ask raspberry pi light on”, “tell raspberry pi light on” or even “open raspberry pi”, I get the response “There was a problem communicating with the requested skill”
    It does not seem to be accessing the ngrok link that I have set up on the pi, since I don’t see any change in the ngrok terminal, or the alexaskill.py terminal (which should log something if the link was accessed). What could I be doing wrong?

    Reply
    • Can you manually try to reach your ngrok server (from inside and outside of your network)? This is where I would begin to search for the issue.

      Reply
      • Thank you for the reply. Yes, I can manually access the ngrok server by entering in the URL into a browser (both in and out of the local network). The browser in both cases reports “Method Not Allowed” and ngrok prints the two lines:
        GET /favicon.ico 404 NOT FOUND
        GET / 405 METHOD NOT ALLOWED

  3. Hi.. it works like a charm. But how i can turn on off all GPIO just saying a word like turn TV ON or turn IRRIGATION ON?

    Need to change the python of alexa skill or start a lot of skills?

    Reply
  4. I’m getting this error when running – python3 alexaskill.py…
    Traceback (most recent call last):
    File “alexaskill.py”, line 5, in
    from flask_ask import Ask, request, session, question, statement
    ImportError: No module named ‘flask_ask’
    I installed python3 -m pip install Flask-Ask
    Also tried changing the from flask_ask import Ask, request, session, question, statement to ..
    from flask-ask import Ask, request, session, question, statement

    Reply

Leave a Comment

Your email address will not be published.