How to Build Your Own Amazon Alexa with a Raspberry Pi
Howdy, it’s been a minute since my last post on Alexa Skill Development. I left us hanging, as I mentioned in one of my previous SQL posts, work craziness, sickness, and general life stuff happening imposed a brief, involuntary hiatus on writing. Enough self talk to make future self feel better. Let’s build our own […]
Howdy, it’s been a minute since my last post on Alexa Skill Development. I left us hanging, as I mentioned in one of my previous SQL posts, work craziness, sickness, and general life stuff happening imposed a brief, involuntary hiatus on writing.
Enough self talk to make future self feel better. Let’s build our own Amazon Alexa with a Raspberry Pi! What a step up from our previous articles. 🙂
But first! A couple of notes: This is NOT necessary for you to do in learning Alexa Skill Development. You can use a regular Echo instead of this expedition into Computer Homebrew 102. Also, I developed my first Alexa skill without an Echo or comparable device at all. One more thing, this is a long post that is both deep and wide ranging. Don’t say I didn’t warn you.
1. The Materials List
To get started, you’ll need a few things:
- Raspberry Pi, version 2 or later
- Power source
- Micro SD card (Recommended at least 8 gb in storage size)
- Speaker with a line-in 3.5mm cable (or headphones of similar line-in spec)
- USB microphone
- Keyboard and Mouse
- HDMI cable to connect to a monitor or TV
- Obviously, you need a monitor or TV. 🙂
A few notes on the Raspberry Pi: I recommend you get a pre-assembled kit, for time and convenience, if nothing else. I previously purchased from CanaKit on Amazon, and have been pleased with it. If, for some reason, you are building your own or need to reformat, the two articles below from RaspberryPi.org are simple, fast instructions to get you up to speed.
Formatting a MicroSD card 32 GB or less:
Downloading New Out of Box Software (NOOBS) for Raspberry Pi, in preparation of your first boot.
Okay, we’re going to proceed assuming that you’ve taken care of the above, and have successfully booted your Pi, connected all requisite hardware, connected to the internet, and so on. Below is where I’m at.
2. Installing Other Utilities
Another note here, it is possible to SSH (read: a bit like Microsoft’s RDP remote desktop application, but command line style) your way into the Raspberry Pi. I will not be doing that. This may sound a bit silly, but I don’t have a multi-screen dock for my laptop (capped at 2), and I’m happy for the Pi and other monitor to be a standalone computer. (Perks of a wide desk.) Also, the Wi-Fi USB card I’m using is very slow at the moment. Also, an edit after I’ve written this post offline: Linux-esque command line work is a royal pain in the you-know-what. Remarkable computing has advanced to where it is today. More on Raspberry Pi intricacies later.
Enough of my discourse, let’s continue setting up our Raspberry Pi / DIY Alexa to-be. Task time.
Navigate to your Pi’s command line module.
- We’re going to update the GET. Type:
- sudo apt-get update (Pro tip: don’t misspell sudo as I’ve laughingly done on the first try.)
- In the new line created by default, type: clear and hit enter. This will clear your command line.
3. Registering Your Raspberry Pi as an Alexa Voice Service Device Type
Alright, head to the Amazon Developer Portal and log in with your credentials.. (If you’ve followed along with previous posts, you have a login! If not, see this article on setting up an AWS account.)
Once you’ve logged in, click on the Alexa navigation item, then click “Get Started”. You should land on a page that has a “Register Product Type” drop-down. Select Device and continue.
You’ll be taken to a new screen / process to create a new device type. Choose and type your device type ID and display name. For ease of process here, I’ve used the same value for device type ID and display name.
Once you’ve selected your ID and name of choice, click next. Now we need to create a new security profile. Click the dropdown menu that reads, “Select Security Profile” and click, “Create New Profile”. If you’ve filled out all the fields properly, you should have the option to submit this device / product. Hooray! Below is what I see after I submit the new device.
Create a Security Profile Name and Description of your choosing and click “Next”.
You’ll now see a new screen with your created security profile. Below, you should see some tabs such as, “General”, “Web Settings”, “Android/Kindle Settings”, and so forth. Click on “Web Settings” and then click “Edit”.
You should now be able to edit the “Allowed Origins” and “Allowed Return URLs” fields. Enter the following values into each of the fields, respectively:
Once you’ve entered the above values, click “Next”.
Now we need to add some additional detail about our device type.
- Upload an image (I used this one, they’re picky about sizing)
- Choose a category (I selected “Other”)
- Provide a short description to your pleasing
- Select “No” for plans on making product available to general public
- Select “No” for product directed to children
Click “Next” to proceed. The next screen asks about applying for access to Amazon Music. I’m selecting “No” for the purposes of this exercise.
4. Enable Login With Amazon for your new Device / Product
Sweet! After you click, “Submit”, you should see the below. Now head over to the “Apps & Services” section, and select the “Login with Amazon” item, once the Apps & Services screen has loaded.
From the drop-down menu of existing security profiles, select the profile we just created. You’ll need to enter a consent privacy notice URL and a consent logo image. Because we’re doing anything public facing, you can enter any URL you’d like here. For the image, I used the same image from the device creation process. Click “Save” and let’s keep moving.
Upon a successful save, you should see a table with a column labeled “Oauth2 Credentials”. Click, “Show Client ID and Client Secret”, copy and save the information somewhere safe. Next stop, Raspberry Pi.
5. Install the Application on Your Raspberry Pi
Flip back over to your Raspberry Pi and type the following two bullet point texts as unique lines into command line.
- cd Desktop
- git clone https://github.com/alexa/alexa-avs-sample-app.git
It should take a minute or two to process, and you should see the below.
Next, type the below point texts as unique lines into command line:
- cd alexa-avs-sample-app/
- nano automated_install.sh
After returning the two above lines, you should see something like the below. Remember what we entered in the Developer Console a few steps ago? You’ll need this information here. (Highlighted boxes.) Update from 2/25/2017: I actually had quite a bit of trouble with this step. Linux command line / text emulator is not my friend yet. The trouble I had here was making and saving my changes to the below. However, the program is surprisingly helpful! It will prompt you to verify credentials before install proceeds.
After you’ve made the appropriate changes, command line language of CTRL + X should help you save the changes. Close out the command line window and re-open. Type the following bullet texts:
- cd Desktop
- cd alexa-avs-sample-app
- . automated_install.sh
When prompted for the AVS + Raspberry Pi License and Agreement, obviously answer and/or “y” to proceed, and answer yes and appropriate and desired throughout the setup process. E.g., you’ll be asked about language preference, audio input settings, enabling wake word detection, and so forth. (It takes anywhere from 30 min to 1 hour.)
Once the install runs, it’s time to start the Alexa service!
6. Running the Alexa Service / Finishing Touches
To test and run the Alexa service, we need 3 command line terminals open. The command paths for these are at the end of the install. (As I type this, the latest install on my Pi is approaching 45 minutes, so I’ll have to give you the bare bones for now based on past tests and some generous documentation.
The first terminal will set Alexa up to be listening on port 3000 (remember the local host URLs from earlier?) The second window deals with setting up a Java client and logging into Amazon with the Security profile we set up. Logging in and confirming will enable you to initiate the connection to Alexa, paving the way for the third terminal, which enables the wake word detection and actual running of the Alexa service. Woohoo! I’ll update with examples later.
Update — 2/25/2017:
So, the whole process took a looooong time. At about 3 am of the night I wrote / finished this post, the Pi finally finished unpacking and installing everything. Below is our success confirmation! I’ll be testing this evening…
Goodness, what a ride, if you stuck it through the whole article. You could do this, or just get a darn Echo. After all this, I recommend you buy an Echo. 🙂
Other miscellaneous updates from 2/25/2107: By the time I got to the wrap up, it was 3:30 in the morning, yikes! I wasn’t feeling very descriptive, nor was I yesterday either. This was an incredibly long process that took several days from start to finish. Was it worth it? Yes. I’m still going to test the capabilities tonight and hopefully have some follow up’s in the coming days. Will I be primarily using an Echo moving forward? Yes.