Building your own Amazon Alexa

James Hamann
Jul 7, 2017 · 6 min read
Amazon Alexa

Fairly recently I did a post on creating a Smart Mirror, after purchasing a microphone I’ve managed to create my own Alexa. It’s remarkably simple and easy to do.

I’ll assume you’ve got a Rasberry Pi, a USB microphone and an audio line out cable to connect up to your speaker. If you’ve got a bluetooth speaker, great because you can also use that.

Getting Started

Firstly you’re going to need to setup an Amazon Developer account if you don’t already have one, this will give you access to Alexa Voice Services (AVS).

Next you’re going to need to setup your device and create a security profile. When you log in hit the “Alexa” tab and you should be greeted with this page.

After clicking the “Get Started” button under Alexa Voice Service, you’ll be asked to register a product. Be sure to choose Device.

You’ll then be prompted to fill in a few details.

The Device ID is a simple identifier for the product and the Display Name is what is displayed to users. After hitting next, you’ll be brought to the Security Profile Tab.

You’ll be asked to enter the name and description of the profile. As the screenshot shows, I used the name “Alexa Voice Service Sample App Security Profile.” Creating this will generate you with a Client ID and Client Secret. Now hit the “Web Settings” tab, you’re going to need to add https://localhost:3000 to Allowed Origins and https://localhost:3000/authresponse to Allowed Return URLs.

Next you’ll be filling in the device details, the category you’ll choose would be “Other” and the image chosen for upload is only really for your reference, so it can be anything you like as the product we’re creating is for private use and isn’t going to be made available to the general public.

Last thing to do regarding the setup, would be to head over to this link and enable your security profile. Choose the one you created earlier, when prompted for a privacy policy URL, you can fake one like http://example.com, then hit save. You’ll now have access to your Client ID and Client Secret, make a note of these or keep them safe as we’ll be using them in the next section.

Setting up your Pi

Now you’re ready to setup and install everything on your Pi. Open up the terminal, cd to a suitable place, Desktop for example, and clone the sample app.

$ cd Desktop
$ git clone https://github.com/alexa/alexa-avs-sample-app.git

Before running the install script, you’re going to need to set the Client ID, Secret and Device ID in the install script.

$ nano automated_install.sh

If you want a quick crash course on nano, head hear.

Copy and paste the values for the three fields above, then hit ctrl-x, y and then enter to save the changes.

Now you can run the install script which will install all dependancies, as well as two wake word engines Sensory and KITT.AI.

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

You’ll be prompted to answer a few yes/no questions and then the install will get started. This takes around 30–40 minutes, so time to grab a coffee and let it do it’s thing.

Running Alexa

Everything should now be installed and ready to run. Open two separate terminal windows.

In the first window you’re going to run the web service used to authenticate your app with AVS.

// Terminal Window 1$ cd ~/Desktop/alexa-avs-sample-app/samples
$ cd companionService && npm start

Your app should now be running on port 3000.

In the second terminal you’ll run the sample app that communicates with AVS.

// Terminal Window 2$ cd ~/Desktop/alexa-avs-sample-app/samples
$ cd javaclient && mvn exec:exec

When executing this command, you’ll get a pop up dialog box regarding authentication, click “Yes”, which will open the URL in your browser. When you arrive at the page, you may get a warning stating the connection is not private. To pass through this, click the “Advanced” button at the bottom and choose “Proceed to localhost (unsafe)”. You’ll then be prompted to login with your Amazon Developer credentials and agree to their terms and conditions. Once this is done, you’ll notice the browser will refresh and read “device tokens ready”. Head back to the desktop and hit “Ok” on the dialog box pop-up.

Now you’re ready to ask Alexa anything you like! You’ll have to hit the “listen” button in order for her to be woken up. If you’d prefer to initiate the wake word engine, which would allow you to say “Alexa” to “wake” her, then follow the next set of instructions.

Setting up the Wake Word Engine

The sample app comes with two wake word engines, Sensory and KITT.AI, for some reason Sensory did not work for me, but KITT.AI did, so I’ll suggest using that if your setup is the same as mine (Raspberry Pi 3 running Jessie). Enter the following command to initiate KITT.AI.

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

Once this is up and running just say “Alexa” and you’ll hear her audio cue that acknowledges she’s heard you.

And you’ve built yourself a working Alexa! If you ever wish to log out, just follow these instructions.

Also, if you’re setting this up as a standalone Alexa device, and not an add on to a Smart Mirror app, like I have, it may be worth looking at how to setup SSH and VNC so you can get rid of the monitor, keyboard and mouse.

Further Configuration

You’ll notice when using the wake word “Alexa” and asking her what time it is, she’ll default to her “Home” address in Seattle. To change this, I downloaded the Amazon Alexa App, logged in using my developer account credentials and was able to change all the settings from home address, to whether I preferred metric or imperial units and everything in between. You can also set your sports team, change your flash briefing and pretty much most things you can do on an Echo. However, annoyingly you can’t link her to Spotify or any music streaming service, she’ll only play radio, due to Amazon’s current policies. Here’s an example issue from github, but I’ve noticed a few of these dated as recent as a month ago, so it seems unlikely to change in the short term.

Other than that, she’s great. It’s still a bit gimmicky, but it’s kinda nice to be able to get out the shower and ask her for a flash briefing, check your schedule and what the weather is going to be like, without having to pick up a device.

In the future, I think it’d be pretty cool to develop your own skill and control other voice control connected devices using AWS IoT. Your device would pick up a command, authorise it, then send it through to the rules engine, which would interpret and route to the relevant AWS Services, whilst also sending the message through to your other connected application.

Basically what this diagram represents.

The IoT sector is exciting and fast growing, so it’s always valuable and fun to get some experience in building something up yourself.

If you have any questions regarding the setup, or need any help, please do leave a comment or shoot me a message and I’d be happy to help!

Thanks for reading, hit 💚 if you like what you read and be sure to follow to keep up to date with my future posts.

James Hamann

Written by

Software Developer https://jameshamann.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade