RobotThoughts

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.

20160429_183116

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:

I linked to a starter kit for Raspberry Pi above that has most of those listed parts.

Wiring It Up

The reference diagram that comes with the AlexaPi code is below.

Circuit

Here is mine, a little more hastily done. I skipped the resistors and shared a single ground for both LEDs instead of a single ground for the LEDs and the switch.

20160429_183034

The first 5 GPIO pins are used by my power source. Pins 18 (Red) and GND (Brown) beside it are for the push button. Pins 24 (Green/Blue) and 25 (Orange) share the GND (Yellow) for the LEDs.

20160429_183106

Here you can see the jumper wires connected into the mini-breadboard. The longer LED positive legs go into pin 24 (green/blue) for the red LED and pin 25 (orange) for the green led.

20160429_183058

Don’t forget the button, black wire connected to ground and yellow wire connected to pin 18.

20160429_183051

In my parts bucket Raspberry Pi, I found a 32GB microSD card. That much storage would be overkill for this project, so I searched for something smaller.

20160429_183457

A 4GB microSD card will fit the bill, but as you will see takes a little extra work to get rolling.

20160429_183942

Load The OS

So first, to get an OS onto my Raspberry Pi, I copied NOOBS onto the 4GB card, plugged it into Raspberry Pi and booted up.

20160429_184037

I was able to select the full version of Raspbian and pressed ‘i’ to start the install.

20160429_184045

I was …this… close. It seems I need a few more MB. I guess it is time to install the lite version of Raspbian…probably what I should have started with. Lazy bit me in this case.

20160429_184100

Loading an OS the Old Fashioned Way

So let’s do it the old fashioned way. Download a copy of Raspbian Jessie Lite.

Pick your platform and follow the install instructions.

Now I was doing most of this work on my Mac. So I simply formatted the SD card. Popped open a terminal. And…

Did a quick

df -mh

to see where the SD card was mounted. It was mounted on disk8.

Changed directories to my Download folder here I had the Raspbian Jessie lite image file.

cd ~/Download

And fired up the dd command.

sudo dd bs=1m if=2016-03-18-raspbian-jessie-lite.img of=/dev/rdisk8

Finally, I waited while the dd command ran.

Pro Life Tip: you can press ‘ctrl-t’ in the terminal window to see how your command is progressing.

Full Mac instructions

Screen Shot 2016-04-29 at 6.50.46 PM

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.

20160429_185229

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.

Screen Shot 2016-04-29 at 7.01.28 PM

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.

Screen Shot 2016-04-29 at 7.01.11 PM

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.

Screen Shot 2016-04-29 at 7.01.45 PM

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.

3.1 Register for a free Amazon Developer Account

Get a free Amazon developer account if you do not already have one.

3.4 Register your product and create a security profile.

  1. Login to Amazon Developer Portal – developer.amazon.com
  2. Click on Apps & Services tab -> Alexa -> Alexa Voice Service -> Get Started
  3. In the Register a Product Type menu, select Device.
  4. Fill in and save the following values:

Device Type Info

  1. Device Type ID: my_device
  2. Display Name: My Device
  3. Click Next

Security Profile

  1. Click on the Security Profile dropdown and choose “Create a new profile
  2. General Tab
    • Security Profile Name: Alexa Voice Service Sample App Security Profile
    • Security Profile Description: Alexa Voice Service Sample App Security Profile Description
    • Click Next

Client ID and Client Secret will be generated for you.

  1. Now click on the Web Settings Tab
    • Make sure the security profile you just created is selected in the drop-down menu, then click the “Edit” button.

    • Allowed Origins: Click “Add Another” and then enter https://localhost:3000 in the text field that appears.
    • Allowed Return URLs: Click “Add Another” and then enter https://localhost:3000/authresponse in the text field that appears.
    • Click Next

Gonna break in here. On this step instead of https://localhost:3000 use http://localhost:5000 for “Allowed Origins” and http://localhost:5000/code for “Allowed Return”. Also, click on “Add Another” for both “Allowed Origins” and “Allowed Return” URLs. Add http://<your raspberry pi IP address>:5000 to “Allowed Origins” and http://<your raspberry pi IP address>:5000/code to “Allowed Return URLs”.

Device Details

  1. Image: Save the following test image to your computer, then upload it:
  2. Category: Other
  3. Description: Alexa Voice Service sample app test
  4. What is your expected timeline for commercialization?: Longer than 4 months / TBD
  5. How many devices are you planning to commercialize?: 0
  6. Click Next

Amazon Music

  1. 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.)
  2. 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.

Screen Shot 2016-04-29 at 7.34.16 PM

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

Screen Shot 2016-04-29 at 7.33.53 PM

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.Screen Shot 2016-04-29 at 7.53.44 PMReboot 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.

Screen Shot 2016-04-29 at 7.55.57 PM

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.

Screen Shot 2016-04-29 at 7.56.26 PM

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.

Next Post

6 Comments

  1. Sam Machin May 2, 2016

    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.

    • Rich Thompson May 2, 2016 — Post Author

      Sure, please use it. I’ll pull the 1.1 code and test sometime later this week. I’d be curious to see how it works as well.

  2. DanielS May 16, 2016

    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.

    I see this (screenshot):
    https://db.tt/t2gmmRIB

    Any thought’s? Otherwise I’ve been able to follow your guide perfectly and am so close to getting my raspberry pi B working as an echo as well!

    • Rich Thompson May 16, 2016 — Post Author

      Check your Allow Origins and Alllowed Returns URLs. Make sure you entered the raspberry PI IP address and also go ahead and add in your actual raspberry Pi hostname as well. Then re-auth.

  3. F Graham Marshall September 20, 2016

    I was hoping I could get Alexa to switch those 2 leds – and then extend that to other IOT items on my LAN

    Seems not?

    Have you any pointers to IOT control once this setup works?

  4. F Graham Marshall September 21, 2016

    I also get
    “We’re sorry!
    An error occurred when we tried ….etc” all week

    I did your “Check your Allow Origins and Allowed Returns URLs”
    They are as instructed – I am puzzled that Allowed Returns URLs are not open web addresses – they are on my local LAN
    ================================================
    BTW
    I have just found this!
    https://www.hackster.io/taifur/voice-controlled-smart-home-36e37e?ref=part&ref_id=17772&offset=5

    I am now wondering what happens if the Pi reboots under cron control. Can it get the code by itself? – or perhaps you just have to leave it live and hope for no crashes.

    (side note – none of my Pis runs for weeks on end – my routers and Arduino seem rock solid)

Leave a Reply

© 2017 RobotThoughts

Theme by Anders Norén