Nani: Nani
Setting Up Alexa

First we're going to get all of the components configured correctly, and then connect them together to bring Alexa to life. This includes creating a new project in an Amazon Developer account, installing Alexa onto the Pi, and preparing the microphone and speaker.

Create a Device

In order to create an Alexa device and use Amazon’s Alexa Voice Service (AVS) you must create an account under https://developer.amazon.com/login.html. Your Amazon Developer account will be linked to the Alexa device and allow it to use Amazon’s services. Once you have created your account, go to the AVS Console and click on Create Product at the top right.

Fill out the information as follows:

You can set Product Name and Product ID to whatever you’d like, but you must remember what you put under those fields!

Click on Next. We now have to set up a Login With Amazon (LWA) Security Profile. Creating this profile and linking it to your Alexa device project will allow the device to use your Amazon credentials to access Amazon’s services. Click on Create New Profile.

Then fill out the information as follows:

Again, you can choose to fill out these fields however you want, but don’t forget what you typed in!

Click on Other devices and platforms, enter whatever you like under Client ID name (such as the same name as the Product ID from a previous step), and then click on Generate ID. This will generate your device’s Client ID, which is a necessary component to set up your Alexa device. Save the generated ID as soon as you see it. You will need it shortly.

Next up, read and agree to the AVS terms. Click Finish. Now you need to enable this profile that you just created. Go to https://developer.amazon.com/lwa/sp/overview.html and select the profile from the dropdown menu and click on Confirm.

Next, enter a fake URL for the Consent Privacy Notice URL since we will not be needing this functionality. Something like https://www.example.com suffices.

Click on Save and you are ready to go! You can now click on Show Client ID and Client Secret to obtain the necessary IDs (again, save these!)

Installation Scripts

We will now download the actual files onto the Pi in order to begin the device setup. In your Pi, create a directory to hold all the setup files:

$ mkdir alexa_pi
$ cd alexa_pi
Next, download the setup files (setup.sh, config.txt, pi.sh) into this directory:
$ wget https://raw.githubusercontent.com/alexa/avs-device-sdk/master/tools/Install/setup.sh
$ wget https://raw.githubusercontent.com/alexa/avs-device-sdk/master/tools/Install/config.txt
$ wget https://raw.githubusercontent.com/alexa/avs-device-sdk/master/tools/Install/pi.sh
Using the Product ID and Client ID that you created in the previous steps, edit config.txt to include this information. Serial number does not need to be filled in.
$ vim config.txt

For reference, the Product ID in the previous steps was alexa_pi and the Client ID was the one generated when creating the security profile.

Now you’re ready to begin the setup! Run the setup script with:

$ sudo bash setup.sh config.txt
Read and agree to the terms, and wait for it to complete successfully!

Microphone Setup

Now you must set up the USB Microphone to work with your Pi. To see if the microphone is connected and displayed properly, run:

$ arecord -l
You should see something along the lines of

This output states that your USB device is attached to card 1 as a device with id 0. Take note of this if your output is different, and proceed through the next steps using the correct numbers.

Edit the audio configuration file:

$ sudo vim /etc/asound.conf
And enter the following information at the end of the file.
pcm.!default {
  type asym
  capture.pcm "mic"
  playback.pcm "speaker"
}
pcm.mic {
  type plug
  slave {
	pcm "hw:1,0"
  }
}
pcm.speaker {
  type hw
  card 0
}
ctl.!default {
 type hw card 0
}
Create a new file called .asoundrc in your home directory and enter the exact same information you entered in asound.conf
$ vim ~/.asoundrc
This will set your USB mic (card1, device0 -> hw:1,0) as the audio capture device.

Speaker Setup

Your speaker might work right out of the box! Let's test it out and see.

First off, install gstreamer, a command line audio utility.

$ sudo apt-get install gstreamer1.0
Run this command:
$ gst-launch-1.0 -m audiotestsrc ! autoaudiosink.
If the system is working, you will hear a test tone. You can now skip the rest of this section. If the system is not working, you will hear no test tone. If it is not working, try this command:
$ gst-launch-1.0 -m audiotestsrc ! alsasink.
If you hear a test tone now, you have to change the source code for the project and recompile. Modify the code on ./avs-device-sdk/MediaPlayer/src/MediaPlayer.cpp on line 599 from:
m_pipeline.audioSink = gst_element_factory_make("autoaudiosink", "audio_sink");
to
m_pipeline.audioSink = gst_element_factory_make("alsasink", "audio_sink");
And rerun the setup with
sudo bash setup.sh config.txt

We’re almost there! Time to finally connect all the pieces together. Run:

sudo bash startsample.sh
The script will ask you to visit a URL and enter a code. Follow those instructions and authenticate using your Amazon Developer credentials.

The script will continue to execute as it waits for you to input the code. The url and code appear at the beginning of the script’s output, so make sure to catch it before the script continues to execute!

Click on Allow and wait a few minutes for your changes to take effect on Amazon’s servers. Once everything is set up, you will observe something along the lines of:

You are now ready to go! Try saying something like

Alexa, tell me a joke

Now that Alexa is working, next we'll learn how to make custom skills for Alexa that pair her with our IoT system!