Building An Amazon Echo on the Raspberry Pi Model B Revision 2

I am fascinated with the success of the Amazon Echo. A company founded on selling books has worked very hard to become a hardware powerhouse and I think they achieved that goal with the Amazon Echo. I bought an Echo to play with home automation but when Amazon posted instructions on how to build your own Echo I knew that I would need to build one.

I have multiple Raspberry Pi units laying around doing various useful and non-useful things. I didn’t want to steal one from my RetroPie project, or my Tor Onion Router so I looked towards the parts drawer and found a great candidate, a Raspberry Pi Model B Revision 2.

I tried the Amazon method of building a test app using Java on the Raspberry Pi but the java app and GUI requirements were too heavy for the poor little board. I didn’t want to write a program either since I am an efficient engineer…which is another way to say lazy.

Enter the AlexaPi. Sam Machin built a nice portable Python based Alexa application for the CHIP. Even better, he ported the code to a Raspberry Pi 2. A little configuration on the Raspberry Pi RevB and I had a working Echo.

Begin at the Beginning

Here is a quick tutorial on how to turn your Raspberry Pi RevB (and probably any Raspberry Pi) into an Echo.

Setup of Raspberry Pi

First we need to look at is a quick overview of the hardware needed for this project. Here are the items you will need:

Once the dd is complete, you can remove the SD card from your PC or Mac and put it into the Raspberry Pi. Plug in your Raspberry Pi and if all went well, then you should see a standard linux boot sequence.

Login and First Setup

Once the Raspberry Pi boots, you can login with the username ‘pi’ and the password ‘raspberry’.

We will need to complete one step to begin working on this Raspberry from your desktop instead of using a keyboard connected to the Raspberry Pi. We will need to fire up the Raspberry Pi configuration utility and enable SSH.

sudo raspi-config

Using your arrow keys and the enter key, select Advanced Options and scroll down to select SSH.

Once you enable SSH, press the Tab key to navigate to the bottom and select <Finish> and press enter.

One last thing before we return to your desktop. Run the command ‘hostname -I’ to get the IP address of the Raspberry Pi.

hostname -I

More Setup via SSH

You can remove the keyboard and HDMI monitor connections to your Raspberry Pi. From here forward we will SSH into the device.

ssh to pi@<raspberry pi IP adress>

Once you SSH into the Raspberry PI we can run the configuration application again and do a little bit of clean up.

sudo raspi-config

First let’s expand the filesystem to occupy the whole SD card.

Next up, change the password for the ‘pi’ user. Or don’t, your call.

You can also overclock from this utility. I usually set a ‘medium’ overclock.

Then let’s set the hostname for the Raspberry Pi under the ‘Advanced -> hostname’ menu.

Finally, let’s force the audio out the 3.5mm jack using the ‘Advanced -> Audio’ menu.

For the last step of setting up the Raspberry Pi, before we begin customizing the install for the AlexaPi application, we should update and upgrade.

Run these two commands

sudo apt-get update

sudo apt-get upgrade

Now we are ready to install do all of the pre-requisites for the AlexaPi application.

Get Your Amazon Credentials In Order

This next section is a literal cut and paste from this document in which Amazon explains how to setup their test application. The relevant section for us is section 3, which I have pasted below. We will create a Amazon Developer Account and Security Credentials for our AlexaPi build. I have removed the non-relevant sections from the Amazon cut and paste to make things easier.

Image: Save the following test image to your computer, then upload it:

Category: Other

Description: Alexa Voice Service sample app test

What is your expected timeline for commercialization?: Longer than 4 months / TBD

How many devices are you planning to commercialize?: 0

Click Next

Amazon Music

Enable Amazon Music?: No (You may optionally select Yes and fill in the required fields if you want to experiment with Amazon Music. However, Amazon Music is not required for basic use of the Alexa Voice Service.)

Click the Submit button

AlexaPi Pre-Requisites

We will need to install the AlexaPi application as root. So let’s set a root password.

sudo passwd root

We will install ‘git’ to clone the AlexaPi application.

sudo apt-get install git

I went ahead and installed mpg123 since I know AlexaPi uses it to encode and decode sound.

sudo apt-get install mpg123

Then change user to root.

su

Change to root’s home directory.

cd /root

Installing AlexaPi

Now we can clone the AlexaPi application.

git clone https://github.com/sammachin/AlexaPi.git

Once the clone is complete, enter into the AlexaPi directory and run the setup script.

cd /root/AlexaPi

./setup.sh

Answer yes (press ‘y’) to install the dependencies. There will be many since we installed the lite version of Raspbian.

The script will pause and ask you for your ProductID. Go to your Amazon Developer account page and select Apps & Services and Security Profiles to get your answers to these questions.

Click on the Security Profile you created.

The script will ask you several questions that you can copy and paste from your developer account.

Once you finish with the questions, we need to get the Auth token from Amazon for your new AlexaPi application. To do this connect to the Raspberry Pi IP address on port 5000.

https://<raspberry pi IP Addr>:5000

The web page that comes up will ask you to log in to your Amazon Developer account. If you see a success message with a long key then you are done.Reboot your Raspberry Pi

reboot

Test It Out

Make sure your speakers are on and microphone is connected. Once you can re-connect to your Raspberry PI via SSH you can test it out.

Push and hold your button and say into the microphone “tell me a joke”. The Green LED should light up when you push the button and the red button should light while it is processing your command. You should hear a response, probably quietly, via the speakers.

To fix the low volume, run this command on the Raspberry Pi in your SSH session.

alsamixer

Push the up arrow to increase the volume output of the speakers.

The volume should go up to 100. Press F6 to change the sound card to your microphone and you can tune the mic volume (gain) there.

Playing with Alexa

So you do not need to say “Alexa,” before every command. Just pushing the button acts as the trigger to say your command. There is tons of things you can do from here. You can download the Alexa App from Amazon and configure your new Echo in settings. Be sure to add the location to your settings so the weather and other geo located services are correct. You can also try some Easter Eggs to demo your new Echo.

Post navigation

Awesome post Richard, glad somebody has tested it out on the V1 Pi, I suspected it would work but didn’t get around to trying as there’s so much else I need to implement/fix!

Would you mind if I used your post for the basis of a setup guide? Especially the section on the amazon credentials, One of the things near the top of my list is to improve the setup experience.

By the way I’ve just released v1.1 as a beta which has some fairly major changes, particularly using vlc instead of mpg123 so we can now playback some of the music apps, I’d be interested to see how that runs on the original pi.

For starters, I much prefer your wiring layout, just makes more sense, and allowed me to connect my switch up directly bypassing the breadboard.

However I can’t seem to get the refresh_token generated during the auth stage.
I have to open http (not https) for :5000 during the initial setup stage (ssl doesn’t connect, not even with a self signed cert) afterwards I get an error page from amazon after clicking Okay allow on the popup window and get from amazon:

We’re sorry!
An error occurred when we tried to process your request. Rest assured, we’re already working on the problem and expect to resolve it shortly.

I’ve been getting this all week so I believe it’s something in the cherrypy capture session of the refresh_token that’s broken.