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:
- Raspberry Pi or Raspberry Pi Starter Kit
- USB Mic
- Speakers with a 3.5″ jack
- Wired Ethernet connectivity or a Wi-Fi dongle
- Momentary switch
- Jumper Wires
- HDMI cable
- monitor with HDMI connection
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.
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.
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.
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.
Don’t forget the button, black wire connected to ground and yellow wire connected to pin 18.
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.
A 4GB microSD card will fit the bill, but as you will see takes a little extra work to get rolling.
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.
I was able to select the full version of Raspbian and pressed ‘i’ to start the install.
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.
Loading an OS the Old Fashioned Way
So let’s do it the old fashioned way. Download a copy of Raspbian Jessie Lite.
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
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.
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.
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.
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.
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.
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.
3.1 Register for a free Amazon Developer Account
Get a free Amazon developer account if you do not already have one.
- Login to Amazon Developer Portal – developer.amazon.com
- Click on Apps & Services tab -> Alexa -> Alexa Voice Service -> Get Started
- In the Register a Product Type menu, select Device.
- Fill in and save the following values:
Device Type Info
- Device Type ID: my_device
- Display Name: My Device
- Click Next
- Click on the Security Profile dropdown and choose “Create a new profile”
- 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.
- 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”.
- 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
- 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
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.
Change to root’s home directory.
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.
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
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.
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.