Pi My Life Up

Raspberry Pi Alexa: Build your own Amazon Echo


This guide will show you how to build and set up your very own fully functional Raspberry Pi Alexa device that will respond to your voice commands just like Amazon Echo.

This tutorial will show you how to utilize Alexa on your Raspberry Pi to build your own Amazon Echo. This Raspberry Pi Amazon Echo utilizes Alexa so that it comes with all the other bells and whistles of Amazon’s Echo’s major functionality minus the music services like Spotify.

That meaning you have access to Alexa, Amazon’s own virtual assistant, something that is much like Microsoft’s Cortana, Apple’s Siri and Google Now. This integration with Alexa gives you the ability to do numerous different things such as unit conversions and checking sports scores.

All you need to build your own Amazon Echo is a Raspberry Pi with a USB Microphone, Speakers and also an Amazon Developer account so we can set up Alexa for our Raspberry Pi.

Equipment List

Below are all the bits and pieces that I used for this build your own Amazon Echo tutorial, you will need a microphone, speakers handy to be able to set this up.

Recommended:

Raspberry Pi

Micro SD Card

Power Supply

Network Connection

USB Microphone

Speakers

GUI Enabled (Access via VNC or Keyboard/Mouse)

Optional:

Raspberry Pi Case

Registering for an Amazon Developer Account

1. Before we get started with setting up the Raspberry Pi, we will first have to create our own Amazon Developer Account.

We can do this by going to Amazon’s Developer website and following the prompts. We won’t go through the exact steps to set up your account as it is self-explanatory.

Once you have created your account and logged in, we can then proceed with setting up everything we need to configure our Raspberry Pi Alexa service.

Upon logging in you will be greeted with the following screen, click on the Alexa tab.

2. On the next screen, we need to select Alexa Voice Service by clicking the Get Started Button underneath it.

3. Finally, we can start setting up our new device, on this new screen we need to click Register a Product (1.) then underneath that click Device (2.)

4. On this next screen, we need to define both our Device Type ID and our Display Name, you can set these to anything you like, but for our purposes, we will set the Device Type ID to Raspberry_Pi and Display Name to Raspberry Pi. Remember what you wrote down for your Device Type ID as we will need this later.

Once you have filled out this information, click the Next (2.) button.

5. On this screen, we need first to use the drop-down box to select “Create a new profile” (1.).
Once you have done that, we need to fill out the Security Profile Name and Security Profile Description.

You should keep these descriptive but straightforward, for our tutorial we are just going to use PiEcho and Raspberry Pi Echo.

The Profile Name field makes it easy to find the profile again if you need to set up an additional Device. The Description field is used to give an idea of what the profile itself is set up for.

Once you have filled out these two fields, you can press the Next (3.) button.

6. On this screen, we are presented with a whole bunch of information that we will need to write down somewhere safe. This page contains the information we will use for the Raspberry Pi to connect to Amazon’s Alexa service. Write down the Client ID and the Client Secret. All this information will be needed to be plugged into the scripts later.

Once you have written down the information, click on the Web Settings (2.) tab.

7. Now on this next screen, we need to click the edit button before we can continue.

8. With this screen, we need to pay attention to Allowed Origins and Allowed Return URLs. Next to both of these you will need to click “Add Another link“. This selection will allow access to two textboxes.

In the text box, next to Allowed Origins enter: https://localhost:3000
In the text box, next to Allowed Return URLs enter: https://localhost:3000/authresponse

Once you have entered that data correctly, click the Next (2.) button.

9. This last bit is the final step to set up our new device with Amazon. You could set these to whatever you want, but for our tutorial, we will set the Category to Home Automation, and Description to “Raspberry Pi Echo“.

Once you have filled out all the information, you can press the Submit (2.)button.

10. With this, all filled out and setup you can now proceed with the rest of this tutorial. Make sure that you have the Security Profile ID, Client ID and Client Secret keys that you wrote down earlier in this tutorial as we will need them shortly.

Setting up Alexa on your Raspberry Pi

1. With the developer account all set up, we can now proceed with cloning the Alexa AVS samples to our Raspberry Pi and installing it.

While you can clone and install the samples over an SSH connection, to set up and make the initial connection, you will have to utilize the Desktop GUI.

For now, either load up the terminal application or connect to your Raspberry Pi over SSH. If you’re running Raspbian light, then it’s likely GIT isn’t pre-installed, follow the first section in the GIT tutorial to learn how to do it.

Once ready, begin by cloning the Alexa AVS Samples repository by entering the following command.

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

2. Once the repository has finished cloning, we will CD (change directory) into its directory by utilizing the following command.

cd ~/alexa-avs-sample-app

3. Now that we have changed directory we can modify the automated_install.sh file, you will need to have the Device Type ID, Client ID and Client Secret handy for this step.

Run the following command to begin editing the file.

nano automated_install.sh

4. Within this file, you will need to change three values.

Replace YOUR_PRODUCT_ID_HERE with the Device Type ID.
Replace YOUR_CLIENT_ID_HERE with your Client ID.
Replace YOUR_CLIENT_SECRET_HERE with your Client Secret.

Refer to the screenshot below if you are unsure of what your file should look like when you have filled out the values.

Once you have filled out the specific values and are sure they are correct, we can then save the file by pressing Ctrl + X then Y and then pressing Enter.

5. With that all done we can now proceed with the installation of the Alexa samples. Run the following command to begin with the installation process.

. automated_install.sh

6. You will face several prompts before installation begins, the first is a general license prompt. Type Y and press Enter to continue.

7. Just type in Y and press Enter at the next step as we have already created an Amazon Developer Account.

8. Now it will ask you to confirm the credentials we entered earlier. Type in Y and press Enter to continue.

9. Now we will need to set the locale for Alexa to utilize, for our tutorial we will just be using en-US, so type in 1 and press Enter to continue.

10. On this next screen, we will need to select the interface we want to output the audio to. We will be hooking our Raspberry Pi up to a 3.5mm jack speaker. So, we will type in 1 and press Enter to continue.

11. The final screen before the installation process begins will ask if you want to enable Alexa wake word detection. Since we want this to work as an Amazon Echo, we will type in Y and press Enter to continue.

The installation will begin immediately, and this can take considerable time so now is a good time to grab a coffee.

Setting up Raspberry Pi Alexa for your Amazon Echo device

1. To complete the following steps, you will need to either be connected to your Raspberry Pi over VNC or through a physical connection. You will need access to the display to be able to utilize a Java-based client that authenticates the connection with Amazon.

To get started we first need to start the Alexa Web service, and we can do this with a few simple commands. Launch up the terminal application and type in the following couple of commands to start the service

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

If the service has successfully started up, you will see a result like below displayed in the terminal.

2. Open a new tab in the terminal application by pressing Ctrl+T. In this new tab, we will begin initializing the Java App that registers our device.

We can begin this process by running the following two commands.

cd ~/alexa-avs-sample-app/samples/javaclient
mvn exec:exec

3. These two lines of code will open a window asking you to authenticate your device. Click Yes to allow it to open a window in Chromium automatically. A second dialog will pop up asking you to click Ok, ignore this for now.

4. The web browser window that opens will initially warn you that the connection is not private as we are running a self-generated SSL certificate. We can get around this by clicking Show Advanced, and then click Proceed to localhost (unsafe).

5. You will now be prompted to login into your Amazon account within the web browser, fill out your details and press the Sign in using our secure server button.

6. Once you have logged in you will see an authentication screen for your device. Click Okay to proceed. Once completed, it will show “Device Tokens Ready“.

7. After you have seen the Device Tokens Ready page, you can now click Ok on the second Java dialog that popped up. This step will finalize the registration of your device and provide the Java applet with the authentication token to work.

8. Finally, we can start up the wake word engine. Open a new tab in the terminal application by pressing Ctrl + T. This allows it so you can say the word “Alexa” to get your Raspberry Pi Amazon Echo device to start listening. For our tutorial, we will be utilizing the KITT.AI wake word software as it is completely free.

Run the following two commands to start up the wake word engine.

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

Raspberry Pi Alexa should now be up and running, and you should have an operational Amazon Echo device. You can now go ahead and try it out by saying the word “Alexa“. Once the Raspberry Pi Amazon Echo device hears the word, it should beep indicating that it’s listening. When you hear the beep, you can then ask your question, such as “What’s the weather?“.

If your microphone is having issues, picking up your voice, then follow the steps shown below to increase the gain on the microphone.

If you are not having issues you can skip ahead to our section on getting the Amazon Web Services to start on boot.

Improving your Microphone Gain

1. If your microphone is having trouble picking up your voice, then one solution would be to increase the microphone gain. It is possible to do this by utilizing alsamixer.

To launch alsamixer type the following command into terminal:

alsamixer

2. Once the AlsaMixer app has loaded up, you can press F6 to select a different USB Device, use the arrow keys to select your microphone and press Enter.

3. With your microphone selected, use the arrow keys to increase the capture volume. Once you are happy with the capture volume, you can press ESC to exit.

4. Now to keep these new setting, we will need to tell alsa to store the new settings and make them permanent. To do this, we can run the following command:

sudo alsactl store

Getting your Amazon Echo to start on boot

1. If you want your Raspberry Pi Amazon Echo to startup on boot, there are several things we will have to do. Please note that you will still have to have your Raspberry Pi boot into the GUI and not command line due to the Java client that Amazon Echo relies on.

To get started let’s first create our startup script by running the following command:

nano /home/pi/alexa_startup.sh

2. Within this file, we want to add the following lines, make sure you copy them exactly as they are. This script will load up a new terminal tab for every service and start it automatically.

In between each terminal command, we will temporarily sleep to ensure that the services have properly started before we start on the next step.

#!/bin/bash
lxterminal -t "NPM"  --geometry=40x2 --working-directory="/home/pi/alexa-avs-sample-app/samples/companionService" --command npm start &
sleep 30
lxterminal -t "JVM"  --geometry=40x2 --working-directory="/home/pi/alexa-avs-sample-app/samples/javaclient" --command mvn exec:exec &
sleep 120
lxterminal -t "WAKE" --geometry=40x2 --working-directory="/home/pi/alexa-avs-sample-app/samples/wakeWordAgent/src" --command ./wakeWordAgent -e kitt_ai &

Once you have written the bash script correctly, save the file by pressing Ctrl + X then Y and then pressing Enter.

3. Now change the permissions on our new startup script so that we can run it from a separate script.

chmod 755 /home/pi/alexa_startup.sh

4. Finally, we will have to modify LXDE’s (Raspbian desktop) autostart script, and we will change this to run our Alexa startup script. Run the following command to edit the autostart configuration.

nano /home/pi/.config/lxsession/LXDE-pi/autostart

Find

@xscreensaver -no-splash

Replace With

#@xscreensaver -no-splash

Add Below

@/home/pi/alexa_startup.sh

Your file should end up looking something like what is displayed below, with our two additional lines.

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
#@xscreensaver -no-splash
@/home/pi/alexa_startup.sh
@point-rpi

Save the file by pressing Ctrl + X then Y and then pressing Enter.

5. With that all done, you can now restart your Raspberry Pi by running the following command. Wait about two minutes for your Raspberry Pi to fully reboot and for the three programs that control Alexa to startup.

sudo reboot

You should now have a fully operational, build your own, Raspberry Pi Amazon Echo up and running. If you found this tutorial helpful or you have run into any issues, then feel free to head over to our forums.

34 Comments

Tomer Klein on August 9th, 2017 at 3:58pm

Hi,
Great article but i'm having some problems:

1. Please note that Git is not installed by default on Raspbian, I had to install it.

2. The clone command is asking for credentials and then says the the git repo does not exist.

please advice,
Cheers!

Gus on August 9th, 2017 at 4:10pm

Hi Tomer,

1. Git should be installed by default on normal Raspbian, if you're using Raspbian lite then it's likely it will need to be installed.

2. Sorry about that, there was a mistake in the spelling, this should be fixed and should now work without any issue.

Tomer Klein on August 10th, 2017 at 5:17pm

Hi Gus,
Working great and so easy to do, thank you for this article.

I have two more questions,
When i'm asking alexa to put some music, she answers that Amazon Music is not supported for this device. Is there a work around? and I have to press the "Talk to alexa" button to ask something, it seems that the "Wakeup Word" not working.

Please Advice.

Cheers,
Tomer Klein

Ambika Joshi on October 12th, 2017 at 3:45am

Hi Tomer,

You can reinstall ". automated_install.sh" file. This resolved the issue for me.

Thanks
Ambika

Sumit Choudhary on August 13th, 2017 at 2:02am

Do I need Amazon Alexa for it?

gbCambridge on September 7th, 2017 at 1:52am

No, you do need an Amazon account though

Randall Figg on August 26th, 2017 at 10:53pm

Of all the instructions on the web, your's is the best. However, I have a problem. I am running Stretch with a Raspberry Pi 3 B. When I start the Java client, my browser will not open. Alexa Voice Service - v20160207.5 opens. It wants an access token, but since my browser will not open to Amazon, I'm stuck.
I'm thinking it is because of the new os. I bought my pi last week.
Thanks!

Manuel on September 27th, 2017 at 4:32am

Randall Figg, did you ever find a solution? I am facing the same problem (though I don't even get to the voice service box). Any help would be appreciated.

Erick on August 31st, 2017 at 9:25pm

What is the required or recommended model of Raspberry Pi used for this project?

Thanks,
Erick

gbCambridge on September 7th, 2017 at 1:51am

Hi,
Well I can tell you that I have it working (today!) on a Pi 2 Model B. It was a spare that I found useful to experiment on.
Once the system is set so that it boots straight into Alexa, in headless mode, then I shall think about comparing with a real dot for performance comparisons. As it is, the performance is acceptable. With a network/cloud product like this it is hard to say where the performance issues are.
However, once you get to an rpi 3, what you pay is so close to the price of a dot you may as well buy the dot. It is, after all, a finished product.

Kevin on September 11th, 2017 at 5:00am

Great article. This worked perfect without a hitch. Looks like I will need a better USB microphone, but aside from that... this is great. Thanks for the post!

Mohammed on September 27th, 2017 at 12:48am

How much free space do we need on the raspberry pi?

Dennis Troyer on October 20th, 2017 at 9:02am

Perfect instructions, walked through it with no issues! Used the wake word, heard the beep and asked (and received) the local weather! Until...until I rebooted. I waited for a few minutes, saw three small terminal windows open (NPM, JVM and WAKE). Minimized them to the panel and tried the Alexa wake word. Nothing. No beep or anything. Ideas?

Michael Mac Donald on October 24th, 2017 at 2:46am

Everything worked for me, until my reboot. I also see the three terminal windows but nothing works? I would have thought it would show the Alexa Java window, however it does not. I simply have the desktop with the three terminal windows. I have gone through all the last steps repeatedly, but cannot find where I went wrong?

Dutchman on October 29th, 2017 at 10:50pm

How is it possible to registrate the device at amazon by using a pi without GUI (the jave console will not start a browser there :-).

I would like to use my pi without any guy installed

Gus on October 30th, 2017 at 2:30pm

Hey,

Sadly there is no real way to handle it without the GUI at the moment. There was a few forks of the code that attempted it but upon testing them they seemed incredibly unreliable.

Cheers,
Gus

Matthew on November 19th, 2017 at 7:30am

Hi,

Really keen to have a go at this, just wondered if it could be run on Ubunti Mate instead of Raspbian ??

Cheers

Gus on December 29th, 2017 at 2:52am

Hey Matthew,

We haven't tested Alexa on Ubuntu Mate, or used Ubuntu mate at all. But it won't hurt to test it out.

Cheers

Joseph on December 13th, 2017 at 3:33am

I think part of the code in the alexa_startup.sh file is wrong
Three terminal windows open up and they’re called “NPM” “JVM and “WAKE”, but it doesn’t execute the instruction after that, it just stays as three terminal windows with nothing in them when I boot to the GUI.
I’d love to know how to fix it as each time I turn the RPI on I have to type in all the commands again which I’d rather not have to do
Thanks in advance 👍🏻

Gus on December 13th, 2017 at 12:56pm

Hey Joseph,

We just updated the startup script, please let us know if the updated version works.

Cheers

Ruben on January 6th, 2018 at 4:30am

Works like a charm, loving it!!!!!!! Much more versitaile and much easier than the Raspberry Pi Google Assistant!!!!

daniel on January 7th, 2018 at 9:44pm

Hi Ruben
You apparently managed to make it run
I can also but only via the Java GUI
Cant run ./wakeWordAgent -e kitt_ai

any hint?

Kate Lai on January 8th, 2018 at 1:02pm

Hi I was on the step that need to type .automated_install.sh but after I done that it response "command not found" I'm so confused what went wrong? Could you help me with it? Thank you so much

Gus on January 8th, 2018 at 4:17pm

Hey Kate,

Make sure you have a space between the . and automated_install.sh so it should look like . automated_install.sh

Cheers

George Brooke on January 10th, 2018 at 4:57am

Great piece of work and the best Alexa setup that I have found. I am scrapping my links to the others as they are just noise.
Based on Stretch I have now built rpi 3 and 2 without any software problems at all...they just run.

The biggest issue I have is getting a decent sound out of the jack..lots of squeals and noise on the device...still I am sure it will be solvable. On headphone, it is fine, so probably an earthing problem.

akash on February 21st, 2018 at 10:08pm

Can I makes this project with respbarry pi zero

Gus on February 25th, 2018 at 5:25pm

I am not aware of any issues that will prevent it from working on the Pi zero.

Dan D. on February 25th, 2018 at 10:44pm

Just wondering if this method is still working for everyone now that the AVS-sample-app has been put into maintenance mode. Seems it’s been replaced by the avs-device-sdk. Does that matter or do your builds continue to function properly?