This Raspberry Pi Alexa tutorial will show you how to build and set up a fully functional personal assistant that uses your Raspberry Pi and the Alexa Voice service to respond to your voice commands.
The only negative to making your Alexa powered device is that it cannot access music services such as Pandora or Spotify.
This Raspberry Pi Alexa however, does come with all the other bells and whistles of Amazon’s very own Echo device as it utilizes the Alexa Voice Service. By default, it will have always listening, so you can talk to your Alexa by simply saying its name.
That means you have access to Alexa, Amazon’s own virtual assistant, something that is much like Microsoft’s Cortana, Apple’s Siri and Google Now.
Utilizing the Alexa Voice Service gives you the ability to do numerous different things such as unit conversions and checking sports scores just by using your voice.
All you need to build your own Alexa powered personal assistant is a Raspberry Pi with a USB Microphone and speakers.
Equipment List
Below are all the bits and pieces that I used for this build your own Raspberry Pi Alexa tutorial, you will need a microphone and speakers handy to be able to set this up.
Recommended
- Raspberry Pi ( Amazon )
- Micro SD Card ( Amazon )
- Power Supply ( Amazon )
- Network Connection ( Amazon )
- USB Microphone ( Amazon )
- Speakers ( Amazon )
Optional
- Raspberry Pi Case ( Amazon )
Registering an Alexa Voice Service Product
1. Before we get started with setting up the Raspberry Pi, we will first have to create our own Amazon Developer Account.
We can do this by going to Amazon developer web page and following the prompts. We won’t go through the exact steps to set up your account as it is self-explanatory.
Once you have created your account and logged in, we can then proceed with setting up everything we need for our Raspberry Pi Amazon Echo.
Upon logging in you will be greeted with the following screen, hover over the “Alexa” (1.) tab and select “Alexa Voice Service” (2.).
2. On this next screen, click Products. This screen is the area that will allow us to set up access for our Raspberry Pi to connect to the Alexa voice service.
3. Now click “Create Product” to proceed on with the process of setting up a “product” for your Pi.
4. On this screen, there are a few things that you will have to set before you can proceed.
(1.) Product name is displayed to users in the “Alexa App“, set this to something simple but identifiable.
(2.) Product ID is used for the authorization process. The ID must only contain letters, numbers ,and underscores. For our example, we set this to “pimylifeup_alexa_tutorial“.
(3.) Make sure that you set the “Product Type” to “Device with Alexa built-in“.
(4.) Unless you intend on creating your own companion app, set this to “No“.
(5.) Now for “Product Category“, we set this to “Other (Please Specify)” with the specified category being “Raspberry Pi“.
(6.) Finally, you can write a short description of the device. This setting is purely for the AVS service.
5. Continuing from the previous step, there are several more options that we still have to set on this page before being able to proceed.
Most of these options are for you to indicate how you intend on using the Alexa Voice Service to Amazon.
Below is a quick run through of each of these remaining options.
(1.) Here you can set the way that you intend on people interacting with your Raspberry Pi Alexa device.
In our guide, we are setting this to both “Touch-initiated” and “Hands-free” so that you can trigger it by either pressing a button or speaking to the device.
(2.) Unless you intend on distributing your Raspberry Pi Alexa commercially set this option to “No“.
(3.) For this option select “No” unless you intend on utilizing the “Alexa for Business” service.
(4.) Select “No” for this option, unless you happen to have the device also associated with AWS IoT Core accounts.
(5.) Now for the final option, make sure that you select “No” to this option as it is not intended purely for people under the age of 13.
6. Once you have filled in all the required options, you can proceed on with the setup process by clicking the “Next” button that is displayed at the bottom of the page.
7. You will now be prompted to set up a Login with Amazon (LWA) Security Profile.
As we don’t already have a profile, we will have to create one by clicking the “CREATE NEW PROFILE” link as shown below.
8. Now in the text boxes enter a name for this security profile and a short description (1.). We kept these as simple descriptors for what we intend on utilizing it for.
For our tutorial, we set the “Profile Name” to “Pi My Life Up Raspberry Pi Alexa” and set the “Profile Description” to “Profile for Pi My Life Up’s Raspberry Pi Alexa“.
The “Security Profile Name” field makes it easy to identify the security profile and what it is being utilized for. The “Security Profile Description” field is meant to explain what the profile is for.
Once you have set both fields, click the “NEXT” button (2.) to proceed.
9. With the “Login with Amazon” security profile created we can proceed to generating the required ID information so that we can make a connection to the Alexa Voice Service from our Raspberry Pi.
First, switch to the “Other devices and platforms” tab (1.) within the “Platform Information” section.
This section will allow us to generate the relevant data that we require to make a connection to the Alexa Voice Service.
Now we need to set a “Client ID name” (2.) before we can generate the actual ID and authorization data.
The client ID is used for identifying and authorizing your Raspberry Pi to connect to Alexa. This value is not shown to the end user and is purely for authorization.
For our guide, we set the client ID to “pi_my_life_up_raspberrypi_alexa” as it represents both the device and what we intend on utilizing it for.
Once you have set a client ID click the “GENERATE ID” button (3.).
10. With the Client ID generated go ahead and click the “DOWNLOAD” (1.) button.
Make sure that you save the file somewhere that’s easily accessible as we will need to grab the contents of this file later on in the tutorial.
Now click the checkbox next to the “I agree” (2.) to the terms and conditions text.
Once you have downloaded the Client ID files go ahead and click the “FINISH” (3.) button.
11. You have now successfully created your product allowing you to access the AWS API.
12. Before we can set up Alexa on our Raspberry Pi, we must enable the security profile that we just created.
To do this, you will need to go to the “Login With Amazon” homepage.
13. On this screen, you need to select the LWA Security profile that we created in previous steps (1.)
Once selected, click the “Confirm” (2.) button.
14. Here you are required to set the “Consent Privacy Notice URL” (1.). If you are only using this for personal use, you can set this to a fake URL to proceed.
For our tutorial, we just set the consent URL to “http://example.com” as we do not plan on this to be a publicly accessible profile.
Once you have set a Consent Privacy Notice URL, click the “Save” (2.) button.
Setting up Alexa on the Raspberry Pi
1. Before we proceed to install Alexa to the Raspberry Pi, let’s first ensure that we are running the latest available software.
You can achieve this by running the commands below on your Raspberry Pi.
sudo apt update
sudo apt upgrade
2. Before we can start the setup process, we will need to install some packages for the Alexa installer to work correctly.
For some reason, these packages aren’t installed during the standard setup process, and you will run into errors without them.
Install these additional packages by running the command below.
sudo apt install libcurl4-openssl-dev gstreamer1.0-alsa gstreamer1.0-tools gstreamer1.0-plugins-ugly
3. Now let’s download all the installation and configuration scripts that we require from the alexa-device-sdk GitHub.
These scripts will setup and configure Alexa on your Raspberry Pi and handle most of the grunt work of setting up the Alexa software.
Download all three scripts by running the following three wget commands.
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/genConfig.sh
wget https://raw.githubusercontent.com/alexa/avs-device-sdk/master/tools/Install/pi.sh
4. With all the scripts now available to us we will now want to move the “config.json
” file that we downloaded earlier to the right location.
If you downloaded the file on your Raspberry Pi, then you can move it to the “/home/pi
” location and skip to step 5.
Otherwise, you can create a new file by running the following nano command.
nano ~/config.json
5. Now copy and paste the contents of the “config.json
” file you downloaded earlier to this file.
The contents should end up being 6 lines long as we have shown in our example below.
{
"deviceInfo": {
"clientId": "amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"productId": "pimylifeup_alexa_tutorial"
}
}
Once you have entered the correct data, go ahead and save the file by pressing CTRL + X then Y and finally ENTER.
6. Now we can go ahead and run the “setup.sh
” script by running the command below.
This script will install all the necessary information and prepare Alexa on your Raspberry Pi.
bash setup.sh config.json
7. Before the setup process can start, you will be asked about whether you agree to various terms and agreements as the Alexa Device SDK relies on multiple third-party repositories.
We have included an example of what this message should look like below.
To proceed with the installation process, you will need to type in “AGREE
” and press ENTER to continue.
If for some reason you do not agree with the terms you can enter “QUIT
” but the software will not be installed and setup.
#############################################################################
#############################################################################
AVS Device SDK Raspberry pi Script - Terms and Agreements
The AVS Device SDK is dependent on several third-party libraries, environments,
and/or other software packages that are installed using this script from
third-party sources ("External Dependencies").
These are terms and conditions associated with the External Dependencies
(available at https://github.com/alexa/avs-device-sdk/wiki/Dependencies) that
you need to agree to abide by if you choose to install the External Dependencies.
If you do not agree with every term and condition associated with the External
Dependencies, enter "QUIT" in the command line when prompted by the installer.
Else enter "AGREE".
#############################################################################
#############################################################################
Please note that the setup process can take some time as there are a fair few packages that will be installed and several that will need to be automatically compiled from scratch.
During the installation process, it will require your input. So keep an eye on its progress from time to time. For example, at one point, you will need to press the ENTER key and then answer “YES
” to some additional terms.
Changing the Default Language for Alexa
It is also possible to adjust the default language that your Raspberry Pi Alexa device will use.
If you are fine with the device operating in English, then you are free to skip to the next section.
1. To change the language, we will need to modify the “AlexaClientSDKConfig.json
” file that was generated during setup.
We can begin modifying this file by running the following command.
nano /home/pi/build/Integration/AlexaClientSDKConfig.json
2. Within this file, you need to search for the following section.
You can use the CTRL + W key combination to search the file.
"deviceSettings":
3. Within this section, you should see an option called "defaultLocal"
.
"defaultLocale":"en-US",
To change the default language, we need to change "en-US"
to the locale of the language you want to use, below is a list of languages supported by the Alexa SDK.
Language | Locale |
---|---|
English (US Dialect) | en-US |
English (UK Dialect) | en-GB |
English (Canadian Dialect) | en-CA |
English (Australian Dialect) | en-AU |
English (Indian Dialect) | en-IN |
French (Canadian Dialect) | fr-CA |
French (French Dialect) | en-FR |
Spanish (Spanish Dialect) | en-ES |
Spanish (Mexican Dialect) | en-MX |
Spanish (US Dialect) | en-US |
Japanese (Japanese Dialect) | ja-JP |
Italian (Italian Dialect) | it-IT |
Hindi (Indian Dialect) | hi-IN |
Portugues e(Brazilian Dialect) | pt-BR |
4. As an example, if we wanted to switch the language to Spanish, we would modify the option so that it looks like the following.
"defaultLocale":"es-ES",
5. With the default language changed, go ahead and save the file by pressing CTRL + X, then Y, followed by ENTER.
Setting up your Audio for your Raspberry Pi Alexa Device
1. Before we go ahead and run the Alexa software, we must first ensure that our audio is configured correctly.
We must first retrieve the card and device numbers for both our microphone and our audio output.
1a. Run the following command to list all available recording devices. From this command write down the card number and the device number for your microphone.
arecord -l
1b. Now run the following command to list all playback devices. Record both the card number and the device number for the device you want to output through.
Note that the Raspberry Pi’s 3.5mm-jack is typically labeled as Analog
or bcm2835 ALSA
, with the HDMI output being identified as bcm2835 IEC958/HDMI
.
aplay -l
2. Now that we have all the device and card numbers that we require, we now need to go ahead and create our “.asoundrc
” file for the pi user by running the command below.
This “.asoundrc
” file is used for the ALSA sound driver to know what devices to utilize.
nano /home/pi/.asoundrc
3. In this file, you will need to enter the following lines to configure your audio.
Replace both with their respective values that you retrieved during step 1 of this section.
pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw:<card number>,<device number>"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:<card number>,<device number>"
}
}
4. Once you are done, you can save the file by pressing CTRL + X then Y and finally ENTER.
Authorizing and Running Alexa on your Raspberry Pi
1. Now with Alexa setup on our Raspberry Pi, we can now proceed to start up the sample Alexa app to get our device authorized.
You need to enter the following command on your Raspberry Pi to start the Alexa sample app.
bash startsample.sh
2. The first time you start up the sample Alexa app, you will be greeted with the message shown below.
{xxxx}
is a unique code that has been generated to authorize your Raspberry Pi to access the Alexa voice service.
To authorize your Raspberry Pi, go to Amazon Code and follow the prompts to approve your Raspberry Pi’s connection to Alexa.
The Alexa sample app will automatically talk with the Amazon web servers to detect when you have authorized the device.
##################################
# NOT YET AUTHORIZED #
##################################
################################################################################################
# To authorize, browse to: 'https://amazon.com/us/code' and enter the code: {XXXX} #
################################################################################################
3. Once you see the text below, then you will know that your Raspberry Pi has been successfully authorized to talk with the Alexa Voice Service.
You may have to scroll up through the command line history as the Alexa software will produce a fair amount of output once it has been marked as authorized.
###########################
# Authorized! #
###########################
4. There are various ways you can interact with the Alexa sample software. The easiest way of interacting with it is to say “Alexa” followed by your prompt to the Alexa Voice Service.
Alternatively, you can also enter “t
” into the command line to skip the process of having to say “Alexa” out loud.
If you want you can use “h
” which will simulate holding a button down. Using “h
” will make the Alexa sample software continually listen until you enter “h
” into the command line again.
If everything is working as it should, you will get a response streamed from the Alexa Voice Service to your Raspberry Pi. This response will be played back through the audio device you configured earlier.
Getting your Raspberry Pi Alexa Device to Start on Boot
1. To get the Alexa software to startup at boot on our Raspberry Pi ,we will have to first make some modifications to the “SampleApp
” code.
By default the code is designed to wait on input from stdin, but when stdin isn’t available it will throw errors so we will have to modify the input loop.
Begin modifying the “UserInputManager.cpp
” script by running the following command.
sudo nano /home/pi/avs-device-sdk/SampleApp/src/UserInputManager.cpp
2. Within this file use nano’s CTRL + W functionality to find the following section of code.
We found it easiest to find it by searching for “m_interactionManager->begin();
“
Below that section of code and within the while loop make sure you add the code displayed below. This additional code will sleep the input thread and stop any of the input handling to happen.
Find the following block of code
SampleAppReturnCode UserInputManager::run() {
bool userTriggeredLogout = false;
m_interactionManager->begin();
while (true) {
Add Below
std::this_thread::sleep_for(std::chrono::hours(1));
continue;
3. After the changes the code should look like what we have displayed below.
SampleAppReturnCode UserInputManager::run() {
bool userTriggeredLogout = false;
m_interactionManager->begin();
while (true) {
std::this_thread::sleep_for(std::chrono::hours(1));
continue;
4. Now save the file by pressing CTRL + X then Y and finally ENTER.
5. As we have now modified one of the scripts we will now need to recompile it.
Don’t worry this will be a fairly quick process as we just need to rerun the setup script from earlier as shown in the following two commands.
cd /home/pi
sudo bash setup.sh config.json
6. Now that we have compiled the Alexa SampleApp we can now proceed on to creating a service file so that we can automatically start our Alexa service at startup.
Let’s create this service file by running the following command.
sudo nano /lib/systemd/system/alexa.service
7. Within this service file, you will need to enter the lines shown below.
These lines of text will define how the operating system should handle our service.
It tells the operating system what user will execute the bash script. It also sets that we require the network to be active before initializing Alexa.
[Unit]
Description=Raspberry Pi Alexa
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
ExecStart=/bin/bash /home/pi/startsample.sh
Restart=on-abort
User=pi
Group=pi
[Install]
WantedBy=multi-user.target
Once everything has been entered you can save the file by pressing CTRL + X then Y and finally ENTER.
8. With our Alexa service created we need to enable it before it can load on startup.
To enable the new service will use the systemctl utility. Type in the following command to enable it.
sudo systemctl enable alexa.service
9. With the Alexa service now enabled, let’s proceed to start it. Starting the service now will allow us to ensure that everything is working as it should be.
Start the service by running the command below on your Raspberry Pi.
sudo systemctl start alexa.service
10. With the service started you should now be able to use the “Alexa” hotword to interact with your Raspberry Pi Alexa device.
If you get no response, you can check the status of the service by running the following command.
sudo systemctl status alexa.service
Improving your Microphone Gain
1. If your microphone is having trouble picking up your voice, then one solution would be to increase the microphone gain. It is possible to do this by utilizing alsamixer.
To launch alsamixer type the following command into terminal:
alsamixer
2. Once the AlsaMixer app has loaded up, you can press F6 to select a different USB Device, use the ARROW KEYS to select your microphone and press ENTER.
3. With your microphone selected, use the ARROW KEYS to increase the capture volume. Once you are happy with the capture volume, you can press ESC to exit.
4. Now to keep these new setting, we will need to tell alsa to store the new settings and make them permanent. To do this, we can run the following command.
sudo alsactl store
You should now have a fully operational, build your own, Raspberry Pi Amazon Alexa up and running. If you found this tutorial helpful or you have run into any issues, then feel free to leave a comment below.
According to Amazon’s own developer page, as of Sept 2021, Sensory Wake is no longer available, and you must install your own wake word engine. For reference, others have have had success with Kitt-AI or porcupine. Time for another update to this tutorial as instructing others to modify UserInputManager.cpp as described renders Alexa useless in the SampleApp.
Hi Collin,
Thank you for letting me know about this issue, I’ll look into this and update the guide.
Cheers,
Emmet
Hi Emmet,
I’m getting the following error when running the setup script bash setup.sh config.json
It seems the repo is no longer available.
Thanks
==============> CLONING AND BUILDING SENSORY ==============
Cloning into ‘alexa-rpi’…
fatal: remote error:
Repository not found.
Hi John,
It appears that the Sensory script repository that the Alexa team was relying on for hotword detection was removed.
The team are currently working on a solution to this, however, it is most likely going to lead to the removal of hotword detection from the looks of it.
You can follow this issue on the AVS Device SDK repository at https://github.com/alexa/avs-device-sdk/issues/1980
I’ll look into including a workaround for this guide in the meantime.
Cheers,
Emmet
Could you advise on making this work with other audio outputs, such as a Waveshare WM8960 Audio HAT? The Pi’s built-in 3.5mm jack is not high quality at all. A HAT with a built-in microphone is a much better option.
Hi Lawrence,
You can control what devices this is using by modifying the “
.asoundrc
” file.You will need to work out the “
card number
” and the “device number
” that the Raspberry Pi is assigning to the HAT.Cheers,
Emmet
Hi! I’m having issues when applying the “bash start.sample.sh” command. When I enter it i get that response that there is “no such file or directory” and that is it. I’m not sure if this means I made a mistake earlier in the code, or if there is another command I can run to fix this. Let me know!
Hi CMac,
Please make sure that you used the command “
bash startsample.sh
” and not “bash start.sample.sh
“.Typically that issue is caused when an error occurred during the setup process. Please try scrolling back through the command line and see if any errors occurred.
The “
startsample.sh
” script is created near the end of the setup process.Cheers,
Emmet
I know this is old, but I hope you can help, I am getting an error: bash: startsample.sh: No such file or directory What should I do?
Hi Akil,
This problem seems to be caused by a compilation error during the Alexa setup due to curl headers apparently missing. The setup is meant to compile curl and handle this itself but something doesn’t seem to be working.
I’ve made some changes to the tutorial so that we install the “
libcurl4-openssl-dev
” package before running the install script.If you follow the tutorial again from the start of the “
Setting up Alexa on the Raspberry Pi
” section everything should now work properly.Cheers,
Emmet
Hi I am trying to make an alexa on my raspberry pi and when i type bash startsample.sh it says no such file or directory. i am pretty sure that i have followed all the steps correctly.
Hi Daniel,
There was an issue with the official install script not installing a required package.
I have adjusted the tutorial so it should now compile Alexa on your Raspberry Pi correctly.
Cheers,
Emmet
These instructions worked flawlessly on my dietpi installation, as long as I accommodated the home directory and user account differences.
Is there a way to change the default wake word?
Thanks,
Rodney
I have it working too. I had to expand swap file to compile on my Pi 3B+.
Thanks for your work. I’m currently using a webcam for its microphone and the builtin audio for output.
MANY thanks. This tipps saved my whole working progress 🙂
It stopped so many times and I did not know why.
Now it worked with no problem.
thanks, got it working in spanish now!!
cheers mate!
Hello, I know its an old entry. I just followed your step by step guide and works like a charm!! thanks for that. I can ask alexa to turn on my home lights and launch any routine on my account. Im just wondering if I can switch this beauty to spanish language by a chance…
Thanks so much for your work!
Hi Rubens,
I went ahead and researched up on this and found out there was a file you could modify to change the language.
If you follow the new “Changing the Default Language for Alexa” section, you should be able to change the language to Spanish.
I haven’t been able to test this properly since I only know English so let me know if you run into any issues with it.
Cheers,
Emmet