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 https://github.com/alexa/alexa-avs-sample-app.git

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

Once these are added, run ". automated_install.sh" 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 automated_install.sh
$> . automated_install.sh

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...

32 comments:

  1. is it possible to configure alexa to speak french?

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

    ReplyDelete
  3. 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

    ReplyDelete
  4. is it possible to installed on laptop which running linux

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

    ReplyDelete
  6. 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!

    ReplyDelete
  7. 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?

    ReplyDelete
  8. 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?

    ReplyDelete
  9. 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!

    https://www.xianic.net/post/installing-maven-on-the-raspberry-pi/

    ReplyDelete
  10. 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.

    ReplyDelete
  11. 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 com.amazon.alexa.avs.App".

    Did I fail to download some files properly?

    Your help would be greatly appreciated! Thank you!

    ReplyDelete
  12. 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: https://github.com/alexa/alexa-avs-sample-app/issues/746

    ReplyDelete
  13. Thanks Ben! I will check it out! :)

    ReplyDelete
  14. 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?

    ReplyDelete
  15. 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.

    ReplyDelete
  16. 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)?

    ReplyDelete
  17. 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?

    ReplyDelete
  18. Command 2 authentication error:

    Had the same problem, found the solution here: https://github.com/alexa/alexa-avs-sample-app/issues/5

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

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

    ReplyDelete
  19. Hello Ben,

    Thanks for the lovely tutorial (y)

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

    ReplyDelete
  20. Thanks Shawn! Checkout this comment on my smart mirror post, I ended up using these instructions myself: http://www.cyber-omelette.com/2016/12/smart-mirror.html?showComment=1496034739032#c4457578659077750673

    Hope that works for you.

    ReplyDelete
  21. Hello Ben

    Hello your tutorial is great
    Can you please upload the 5 files of the autorun function. Thx a lot

    ReplyDelete
  22. how to add video play skill to alexa?

    ReplyDelete
  23. Thank you so much for your posting. I want to know is it possible to launch the alexa app on start not typing command line whenever reboot? As you seemed to setup mirror for your GF I think you might know how.

    ReplyDelete
  24. if it says "alexa" itself, it stops the previous command and wakes up. This is the dumbest bug ive ever seen

    ReplyDelete
  25. hey! having a problem with command 2, says command not found anytime I type it in

    ReplyDelete
  26. Hell0 .in command 2 i am getting the app opened.But its asking for acess token.And in the terminal i get that "there was problem connecting companion service"?Can u help me out!!!

    ReplyDelete
  27. Hi Ben,

    After numereous attempts to get my microphone working I still get the following error; An error happened in the mainLoop of SensoryWakeWord snsrRun(): task: Input channel audio-pcm: ALSA error: No such file or directory.

    I know this has to do with the ~/.soundrc file. I tried the various settings supplied by blog post on different sites.
    Could you please send the settings in your ~/.soundrc file.

    ReplyDelete
  28. Hi there
    This was a great guide, really useful, just wondering how to make it so the alexa always comes on at boot opposed to having to open these three terminal commands?

    ReplyDelete
  29. Please advise. Followed all step above when executed the step2 has this error. [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (d efault-cli) on project sample-java-client: Command execution failed. Process exi ted with an error: 139 (Exit value: 139) -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit ch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please rea d the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE xception

    ReplyDelete
  30. Hi Ben, when i try to clone the github link it requires my github username and password, after inserting it this error pops up

    remote: Repository not found.
    fatal: repository 'https://github.com/alexa/alexa-avs-sample-app.git/' not found
    Do you know how i can fix this issue?

    ReplyDelete
  31. How to control Magic mirror modules on and off using Alexa?

    ReplyDelete
  32. Please share how to integrate MMM-MirrorMirrorOnTheWall

    ReplyDelete