November 30, 2016

DIY Alexa Raspberry Pi Voice Assistant

Amazon did something really cool. They made their Alexa voice assistant code open source and cross platform. That means instead of buying their Amazon Echo to get a home assistant, you can simply install it on any system you want.

It's actually a fairly straightforward project, especially if you've setup a Raspberry Pi before (or eight #fromthevault)! Basically you're just installing an application, and setting up a microphone and speaker. Nothing fancy.

There are already some great guides for getting started too for both Raspberry Pi and Alexa. I'm just going to direct you to these, give a rough overview, and add some additional tips.

Here it is in action:

Shopping List

For equipment, you'll need:

1. A Raspberry Pi 3 with SD card and USB micro power source:

2. A monitor with HDMI, and a keyboard and mouse for the install. These can be borrowed from another machine, as you don't need them once it's running.

3. A speaker and microphone combo. Any speaker with a 3.5mm Aux input will work. Microphone must be USB connected. I am using:

Raspberry Pi Raspbian Installation

The Raspberry Pi is the brain of Alexa, and needs an operating system and internet connection to function. I strongly recommend Raspbian Jessie with Pixel (download and install guide can be found here). This is what most Raspberry Pi projects have been tested on, so things tend to go more smoothly.

Once the Pi starts up, run the following in terminal to get the software up to date:

$> sudo apt-get update
$> sudo apt-get upgrade

Install Alexa

To install Alexa, head over to the excellent guide by Amazon.

Essentially it boils down to getting a developer account here, and following the steps here to setup the credentials you need for a home application. Note your Device ID, Client ID, and Client Secret from your developer account.

Then head to your Pi and run the following in a terminal:

$> git clone

Now the repository is downloaded. Navigate to the new alexa-avs-sample-app directory and edit to include your personal Device ID, Client Secret, and Client ID.

Once these are added, run "." to begin installation. The install script configures the audio devices on your Pi too, so make sure your speaker and mic are plugged in at this stage.

These commands look like:

cd alexa-avs-sample-app
$> nano
$> .

This process takes about 30 minutes, and will have a few prompts along the way.

Running Alexa

When you finish the installation, you'll see instructions of how to proceed. The Amazon install guide also covers running these three steps.

Command 1:

The first command starts the companion service which opens port 3000 for communication with Amazon.

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

Command 2:

The second starts the Alexa app, which prompts you to open a browser and get authenticated.

If this fails in Chromium try going to settings -> advanced -> Privacy -> content settings -> allow key generation.

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

Command 3:

The last command enables the use of Wake Words. If this fails, there is probably something wrong with your audio setup. I had to re-run the install after using a new mic. You can use kitt_ai or sensory, I use kitt_ai as it doesn't require a license renewal and works great.

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

Once all three are running, you should be able to say "Alexa" (wait for the acknowledging beep), and speak with your personal assistant to your hearts content.

If you are running "Headless" (with no monitor), checkout this page on how to setup a VNC server  and run a graphical session remotely.

Using  Alexa

Now that you've got a personal assistant, what should you do?

My favourite voice commands so far are:
  • Tell me a joke.
  • Do you believe in ghosts?
  • Do you like Jeff Bezos?
  • Sing me a song.
  • I am your father. 
To customize the experience, go to the Amazon Alexa page. The app also doesn't allow non USA addresses. This means I have to say "what's the weather in Toronto" instead of "What's the weather". It does have my address on record under traffic though, and seems to know about local businesses.

There's also the Mr. Robot treatment where you use Alexa as your therapist. Suit yourself...


emna barred said...

is it possible to configure alexa to speak french?

Unknown said...

I don't think so. Currently Alexa only speaks English (US & UK) and German.

Ben said...

Dude, I love your website and this tutorial. I've installed this Alexa Service on my RPi and it works. But I'm wondering how you make it "Always On", or "Automatic" I mean, this sample asks you to click on "Listen" each time you use alexa but on your video you do not seem to require this, how do you do that? Thanks in advance for your help -Ben

Anonymous said...

is it possible to installed on laptop which running linux

Abdelrahman said...

do I have to have a microphone usb since my speaker has a mic?

Ben Eagan said...

Yes it's possible on a laptop.

You may not need microphone usb if your speaker has it built it... Just something you'll have to test!

Unknown said...

Hi Ben, thank you for the guides, they've been extremely helpful. I'm having a small problem that I was hoping you could help me with. When I have the three terminal windows open to run Alexa and I open a fourth terminal window to run MagicMirror, I experience so much lag that MagicMirror will not fully load. Any suggestions to get them both to run at the same time?

Anonymous said...

Hi Ben, really appreciate your guide in setting up Alexa for the Pi. However, I encounter an issue whenever I run the command "$> cd javaclient && mvn exec:exec
" in Terminal 2. My error looks like this: "bash: mvn: command not found". Any way to go about this problem?

Ben Eagan said...

Hi, glad you're finding it helpful. It's been a while since I wrote this, so some steps have probably changed a bit - it looks like you're missing maven though. I think something like this guide might do the trick, please let me know if it works!

Anonymous said...

Thanks Ben for the quick response! I managed to get Maven installed and working successfully.

Then I had another issue upon executing the command. The error contains this message: "[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project sample-java-client: Command execution failed."

Not quite sure what this means but I do not see a webpage loading up as per what is in the instruction.

Anonymous said...

To follow up on the error I received, this is also found: "--- exec-maven-plugin:1.2.1:exec (default-cli) @ sample-java-client ---
Error: Could not find or load main class".

Did I fail to download some files properly?

Your help would be greatly appreciated! Thank you!

Ben Eagan said...

I am away from my Pi, so won't be able to take a look until tonight. This thread looks like it might be similar though, probably related to which directory you are running from:

Anonymous said...

Thanks Ben! I will check it out! :)

Anonymous said...

I've managed to get Alexa to work! YAY! :)
Just a last question, can I exit all the 3 terminal commands and run my Magic Mirror module with Alexa constantly listening out for my keywords?

Anonymous said...

Hello Ben. Firstly, wonderful guide you've created, helped me a lot.
I've been facing problem with Command 2, it asked to open a link to authenticate but when I opened it, it showed error. I'm currently using chromium and it doesn't have any option to allow key generation.
Please help.

Ben Eagan said...

Hey Anonymous, happy it's helping you... Let's see if we can get past this next hurdle! Did you look all over the page for an option like "advanced" or "more options"? I remember I had to click a settings button on the page, and specifically give permission for the page to open. Hopefully chromium hasn't changed. If you don't find it, would you sharing a screenshot of what the error looks like (cellphone picture is fine)?

Anonymous said...

My speaker is working however the lapel mic is not working through the usb audio card. Is there a way to fix this so that the mic works as well?

DavKo said...

Command 2 authentication error:

Had the same problem, found the solution here:

So the solution is the Product name AND Product ID on have to be "my_device". I created a new product with these names, reconfigured and ran and it worked.

Regarding "key generation" option under Chromium, new versions of Chrome/Chromium don't have this option anymore.

Shawn said...

Hello Ben,

Thanks for the lovely tutorial (y)

I have a question, How to auto start Alexa on RPI bootup?

Ben Eagan said...

Thanks Shawn! Checkout this comment on my smart mirror post, I ended up using these instructions myself:

Hope that works for you.

Post a Comment