PiExa- ->Raspberry Pi + Amazon Alexa: A step-by-step guide to build your own hands-free Alexa with Raspberry Pi

Naveen Manwani
Towards Data Science
9 min readJun 16, 2018

--

Motivation Monk: Read everyday something no one else is reading, Think everyday something no one else is thinking. It is bad for the mind to be always part of unanimity.

PiExa ?? I know you all must be wondering what this writer is upto, what is PiExa why would this term has anything to do with Amazon Alexa and Raspberry Pi. Well, my fellow readers all your explicit questions, doubts will be answered through the end of this article.

Objective:This article is a step by step comprehensive guide to build your own hands-free Amazon Alexa with Raspberry Pi 3, using Alexa Voice Service (AVS). Through this article I’ll demonstrates how to access and test AVS using their Java sample app (running on a Raspberry Pi), a Node.js server, and a third-party wake word engine. So, by the end of this article if you follow along with me you might have your own hands free Alexa up and talking to you.

Nomenclature:Purposefully I coined the term “PiExa” which clearly reflects the amalgamation of Raspberry Pi with Amazon Alexa.

Flashback:Few weeks back when I built my own open source natural language processing based voice assistant Mycroft for Raspberry Pi — Picroft , I started thinking is it possible to make my own Alexa using Raspberry Pi, with a cheap speaker and an Universal Serial Bus (USB) microphone in the same way as I built Picroft.

My curious mind and few google searches took me to the Alexa repository which allowed me to convert my simple thought into a reality.

So, without any further diddle-daddle let’s start the step by step execution of building your own Alexa with Raspberry Pi 3.

Hardware Requirement:

  1. Raspberry Pi 3 (Recommended) or Pi 2 Model B (Supported).
  2. Micro-USB power cable for Raspberry Pi.
  3. Micro SD Card (Minimum 8 GB) — You need an operating system to get started. NOOBS (New Out of the Box Software) is an easy-to-use operating system install manager for Raspberry Pi. You can download and install it on your SD card ver easily (for instructions check here).
  4. USB 2.0 Mini Microphone — Raspberry Pi does not have a built-in microphone; to interact with Alexa you’ll need an external one to plug in.
  5. External Speaker with 3.5mm audio cable.
  6. A USB Keyboard & Mouse, and an external HDMI Monitor.
  7. Internet connection (Ethernet or WiFi)

After you collect all the ingredients (hardware) for this project, start following the below mentioned steps.

Step 1: Setting up your Pi

Note: Unless you already have Raspbian Stretch or Jessie installed on your Pi, please follow the guide — Setting up the Raspberry Pi which will walk you through downloading and installing Raspbian and connecting the hardware (if you’re unfamiliar with Raspberry Pi, I highly recommend you to follow the guide above to get your Pi up and ready before moving further).

If you have installed Raspbian Stretch Lite on your Pi, you will need to install Java in order to get the Java Sample App up and running. As long as you have an internet connection, you can use the following commands to do so:

sudo apt-get update
sudo apt-get install oracle-java8-jdk

Step 2: Register for an Amazon developer account

Unless you already have one, go ahead and create a free developer account at developer.amazon.com. Make sure you review the AVS Terms and Agreements here.

Step 3: Create a device and security profile

Follow the steps here to register your product and create a security profile.

Make note of the following parameters. You’ll need these in Step 5 below.

  • ProductID,
  • ClientID, and
  • ClientSecret

Important: Make sure your Allowed Origins and Allowed Return URLs are set under Security Profile > Web (see Create a device and security profile):

Step 4: Clone the sample app

Open terminal, and type the following:

cd Desktop
# (Information: Currently the GitHub link is down due to some maintenance issue)
git clone https://github.com/alexa/alexa-avs-sample-app.git

Step 5: Update the install script with your credentials

Before you run the install script, you need to update the script with the credentials that you got in step 3 — ProductID, ClientID, ClientSecret. Type the following in terminal:

cd ~/Desktop/alexa-avs-sample-app
nano automated_install.sh

Paste the values for ProductID, ClientID, and ClientSecret that you got from Step 3 above.

The changes should look like this:

  • ProductID="RaspberryPi3"
  • ClientID="amzn.xxxxx.xxxxxxxxx"
  • ClientSecret="4e8cb14xxxxxxxxxxxxxxxxxxxxxxxxxxxxx6b4f9"

Type ctrl-X and then Y, and then press Enter to save the changes to the file.

Step 6: Run the install script

You are now ready to run the install script. This will install all dependencies, including the two wake word engines from Sensory and KITT.AI.

Note: The install script will install all project files in the folder that the script is run from.

To run the script, open terminal and navigate to the folder where the project was cloned. Then run the following command:

cd ~/Desktop/alexa-avs-sample-app
. automated_install.sh

You’ll be prompted to answer a few simple questions, answer these questions and complete all necessary prerequisites before continuing.

When the wizard starts, it takes about 25-30 minutes to complete all the process so meanwhile go grab a Tea or a coffee to pass your time.

Step 7: Run your web service, sample app and wake word engine

Now that installation is complete, you’ll need to run three commands in 3 separate terminal windows:

  1. Terminal Window 1: to run the web service for authorization
  2. Terminal Window 2: to run the sample app to communicate with AVS
  3. Terminal Window 3: to run the wake word engine which allows you to start an interaction using the phrase “Alexa”.

Note: These commands must be run in order.

Terminal Window 1

Open a new terminal window and type the following commands to bring up the web service which is used to authorize your sample app with AVS:

cd ~/Desktop/alexa-avs-sample-app/samples
cd companionService && npm start

The server is now running on port 3000 and you are ready to start the client.

See API Overview > Authorization to learn more about authorization.

Let’s walk through the next few steps relevant to Window 2.

  1. When you run the client, a window should pop up with a message that says -

Please register your device by visiting the following URL in a web browser and following the instructions: https://localhost:3000/provision/d340f629bd685deeff28a917. Would you like to open the URL automatically in your default browser?

Click on “Yes” to open the URL in your default browser.

2. If you’re running Raspbian with Pixel desktop (and with Chromium browser), you may get a warning from the browser. You can get around it by clicking on Advanced -> Proceed to localhost(unsafe).

3. You’ll be taken to a Login with Amazon web page. Enter your Amazon credentials.

4. You’ll be taken to a Dev Authorization page, confirming that you’d like your device to access the Security Profile created earlier.

Click Okay.

5. You will now be redirected to a URL beginning with https://localhost:3000/authresponsefollowed by a query string. The body of the web page will say device tokens ready.

6. Return to the Java application and click the OK button. The client is now ready to accept Alexa requests.

Terminal Window 3

Note: Skip this step to run the same app without a wake word engine.

This project supports two third-party wake word engines: Sensory’s TrulyHandsFree and KITT.AI’s Snowboy. The -e parameter is used to select the agent and supports two values for {{engine}}: kitt_ai and sensory.

Open a new terminal window and use the following commands to bring up a wake word engine from Sensory or KITT.AI. The wake word engine will allow you to initiate interactions using the phrase “Alexa”.

To use the Sensory wake word engine, type -

cd ~/Desktop/alexa-avs-sample-app/samples
cd wakeWordAgent/src && ./wakeWordAgent -e sensory

or, type this to use KITT.AI’s wake word engine -

cd ~/Desktop/alexa-avs-sample-app/samples
cd wakeWordAgent/src && ./wakeWordAgent -e kitt_ai

Now you have a working hands-free AVS prototype!

Use the following resources to learn more about available wake word engines:

Step 8: Talk to Alexa

You can now talk to Alexa by simply using the wake word “Alexa”. Try the following -

Say “Alexa”, then wait for the beep. Now say “what’s the time?”

If you prefer, you can also click on the “Listen” button, instead of using the wake word. Click once on the “Listen” button, after releasing the click, wait for the audio cue before beginning to speak. It may take a second or two before you hear the audio cue.

Step 9: How to Log Out of the Sample App

Follow these instructions to log out of the AVS java sample app:

  1. Quit the AVS java sample app (CTRL + C).
  2. Open /samples/javaclient/config.json and clear your sessionId. It should look like this:

"sessionId": ""

3. Delete the refresh_tokens file in samples/companionService.

The next time you log in you will be prompted to authenticate.

Demo Time

Below are the videos with and without wake word “Alexa” so just sit back and enjoy:

Part 1: without wake word “Alexa”

https://www.youtube.com/watch?v=T1uyG9YNWbk (video 1)

Part 2: with wake word “Alexa”

https://www.youtube.com/watch?v=a2XvL1L65tM (video 2)

Since, now you all know each and every step from top to bottom to build your own hands-free Amazon Alexa with Raspberry pi 3, using Alexa Voice Service (AVS). Therefore I want to encourage all my fellow readers, Deep Learning / Machine Learning Practitioners, hobbyists and Enthusiast to come up with your own hands-free Amazon Alexa with Raspberry pi 3 and publish a LinkedIn or a twitter post on it (**but don’t forget to tag me :) . I’ll be eagerly waiting for your posts.

REFERENCES:

  1. To know more about Alexa Voice Service check here.
  2. To learn more about Voice Assistant check here.
  3. The repository I used to bring this project into existence can be found here.(Information: Currently the GitHub link is down due to some maintenance issue)

Thank you for your attention

You using your time to read my work means the world to me. I fully mean that.

If you liked this story, go crazy with the applaud( 👏) button! It will help other people find my work.

Also, follow me on Medium , Linkedin , Twitter if you want to! I would love that.

--

--

Electronics Engineer by degree, ML engineer by interest, Hardware tinkerer by choice