Description: This is Amazon Alexa which is built by using Raspberry Pi and Bluetooth Speaker. This is a step by step tutorial to build Amazon Voice Service(AVS) SDK on Raspberry Pi. Please refer to Amazon's official documentation for detail in depth.
Note: Raspberry Pi Zero W, Raspberry pi model 3B+, and Raspberry Pi 4B can be used in this project as they support Bluetooth. If you are using Model 2B or 2A, 2B+, 1B, 1B+, 1A then you have to use USB Bluetooth for which Steps may be a little different and are out of the scope of this Tutorial.
Bluetooth SpeakerAny Bluetooth speaker withA2DP-SINK
orAVRCP
Profile can be used in this tutorial.
If the Speaker has inbuilt Microphone then no need to use USB Microphone.Required Hardware
1. Raspberry Pi: You can use Raspberry pi zero w, 3, or 4. However please note that Raspberry pi zero w works slow or sometimes not capable to build the app. The best option here is raspberry pi 3 or raspberry pi 4. Raspberry 4B+ with 8GB ram works the best.
2. Heat sink plus Cooling Fan: attach heatsink plus cooling fan if you are using Raspberry pi 3 or 4. If you have raspberry pi zero make sure to use a good quality heatsink.
Reason: If you don't use a cooling fan and the heatsink, your raspberry pi might overheat and overload. Due to this, it may get stuck while building the app by the "make" command.
3. Bluetooth Speaker: This tutorial is regarding building the app with a Bluetooth speaker. so make sure to have a Bluetooth speaker supporting the required profiles.
4. Vnc viewer: By VNC viewer installed on your laptop you can connect your raspberry pi to your laptop. you need to turn on VNC in raspberry pi for this to work. Alternatively, you can also use an HDMI monitor, keyboard, and mouse to operate your pi. But using it through vnc will be easier.
5. Micro sd card: Make sure you have a micro-sd card with a minimum of 8GB space.
6. USB Microphone(optional): if your Bluetooth speaker doesn't have an inbuilt microphone you will have to use a USB Microphone.
Alexa Voice Service (AVS) - SDK ArchitectureThe first step to get started is by knowing what exactly amazon voice service actually is. One of the best resources to learn this is amazon's official website documentation. Make sure you have completely understood what amazon voice service actually is.Below is the link to learn all about Alexa Voice Service and SDK architecture
>> https://developer.amazon.com/en-US/docs/alexa/avs-device-sdk/overview.htmlInstalling Bluetooth Dependencies
To enable BlueZ and PulseAudio, the SDK uses the following libraries, modules, and dependencies. You can choose to build with different dependencies, however, they haven't been explicitly tested against the SDK.
1.SBC Library: Minimum Version should be 1.3
pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get upgrade
pi@raspberrypi:~ $ sudo apt-get install libsbc-dev
2.BlueZ 5.37: Minimum Version should be 5.37.
pi@raspberrypi:~ $ sudo apt-get install bluez
3.PulseAudio and PulseAudio Bluetooth
pi@raspberrypi:~ $ sudo apt-get install pulseaudio
pi@raspberrypi:~ $ sudo apt-get install pulseaudio module-bluetooth
To Know about more dependencies visit this official page of amazon :
>>https://developer.amazon.com/en-US/docs/alexa/avs-device- sdk/dependencies.html
Make an Amazon Developer Account1.To access Amazon Voice Service, you need an Amazon Developer account. You create one by visiting the Amazon Developer Services and Technologies website and clicking on “Sign In” and then on “Create your account”. You can also sign in via your regular Amazon account, if available.
2.Once you have provided the necessary information and agreed to the terms of use and guidelines, you will have access to the Developer Dashboard, which allows you to view and manage your various Amazon projects. Under the heading “Amazon Alexa” you will find “Alexa Voice Service”, which starts the activation of your Raspberry for the language assistant:
3.In the next menu, click on the “GET STARTED” button and Amazon will ask you some questions about the planned project.
- Under “Product name” and “Product ID, ” you can enter “Alexa” or “RaspberryPiAlexa”, for example.
- As "Product type" select the option "Device with Alexa built-in". Click no for the question about the use of a companion app.
- For the“Product category”, choose “Wireless speakers”. In the product description, you don't have to use big words, an entry like "Raspberry Pi Alexa Speaker" is sufficient.
- Since you want to command your Raspberry Pi like the classic Alexa speaker; exclusively by voice command, check "Hands-free" (voice command from close range) and "Far-field" (voice command from a distance) as planned types of interaction.
- The concluding questions about commercial use or use of the Raspberry for Alexa for Business and Alexa Mobile Accessory protocol should be answered with a “no”, as should the question of whether your project is specifically tailored for children.
4.The next step is to create an LWA (Login With Amazon) security profile for the Raspberry Pi so that it can verify itself with Amazon. Since you do not yet have one, click on “CREATE NEW PROFILE” and then enter a suitable name and a short description of the profile:
- Click CREATE NEW PROFILE.
- Under Security Profile Name, enter a relevant name. For example, AVS Tutorials Project.
- Under Security Profile Description, enter a relevant description. For example, AVS Tutorials
- Click NEXT. Your Security Profile ID generates.
- Select the Other devices and platforms tab.
- Under Client ID name, enter a relevant name. For example, Prototype.
- Click Generate ID.
- Click Download. This downloads your config.json file that contains your AVS credentials. Save the file to your
/home/pi
directory. If you are creating this profile from a different PC, you must transfer it to your Raspberry Pi. - Read the Amazon terms and conditions. If you agree, check the I agree to the AVS agreement and the AVS Program Requirements box.
- Click FINISH. Congratulations! You now have access to AVS.
- A pop-up appears. Click OK. You can now view your device on your AVS dashboard.
Make the necessary directories and folders for the app installation.
pi@raspberrypi:~ $ cd /home/pi/
pi@raspberrypi:~ $ mkdir sdk-folder
pi@raspberrypi:~ $ cd sdk-folder
pi@raspberrypi:~ $ mkdir sdk-build sdk-source third-party sdk-install db
Update and upgrade the packages on your Raspberry Pi.
pi@raspberrypi:~ $cd /home/pi/
pi@raspberrypi:~ $sudo apt-get update
pi@raspberrypi:~ $sudo apt-get upgrade
pi@raspberrypi:~ $sudo apt-get dist-upgrade
All the steps required for building the app with bluetooth speaker are the same compared to building the app with normal speaker and can be found here.The only different step is the building the sdk with cmake command. To build with bluetooth, you will need to add additional line (-DBLUETOOTH_BLUEZ_PULSEAUDIO_OVERRIDE_ENDPOINTS=ON -DBLUETOOTH_BLUEZ=ON
) in the cmake command.
Follow all the steps before the sub heading "Building the SDK" in the amazon's official documentation. After that follow this step:
Build the SDK with Bluetooth
pi@raspberrypi:~ $ cd /home/pi/sdk-folder/sdk-build
pi@raspberrypi:~ $ cmake /home/pi/sdk-folder/sdk-source/avs-device-sdk \
-DSENSORY_KEY_WORD_DETECTOR=ON \
-DSENSORY_KEY_WORD_DETECTOR_LIB_PATH=/home/pi/sdk-folder/third-party/alexa-rpi/lib/libsnsr.a \
-DSENSORY_KEY_WORD_DETECTOR_INCLUDE_DIR=/home/pi/sdk-folder/third-party/alexa-rpi/include \
-DGSTREAMER_MEDIA_PLAYER=ON \
-DPORTAUDIO=ON \
-DPORTAUDIO_LIB_PATH=/home/pi/sdk-folder/third-party/portaudio/lib/.libs/libportaudio.a \
-DPORTAUDIO_INCLUDE_DIR=/home/pi/sdk-folder/third-party/portaudio/include\
-DBLUETOOTH_BLUEZ_PULSEAUDIO_OVERRIDE_ENDPOINTS=ON -DBLUETOOTH_BLUEZ=ON
pi@raspberrypi:~ $ make SampleApp
Note: Themake SampleApp
command builds the SDK sample app. To build the entire SDK – including unit and integration tests – run themake
command instead.
After this, follow all the steps from amazon's official documentation. you will see the sub heading as " To set up AlexaClientSDKConfig.json by using genConfig.sh". Follow the exact same steps from there.TroubleShooting
1. Most Common: There is no audio output (from bluetooth speaker) when using the sample app. This problem can occur if the "Gstreamer" is not configured correctly.
- Test Gstreamer with the following command
gst-launch-1.0 audiotestsrc ! alsasink
OR
gst-launch-1.0 audiotestsrc ! alsasink device=bluez_sink<XX_XX_XX_XX_XX>.a2dp
If GStreamer is working, you hear a constant tone sound. If you dont hear the sound follow these steps:
- Step 1: disconnect your Bluetooth speaker and remove the device from Bluetooth devices of Raspberry pi.
- Step 2: Reboot your Pi
sudo reboot
- Step 3: Open Two Command Terminals In one terminal type this step by step
//Terminal 1//
bluetoothctl
power on
agent on
default-agent
scan on
After some seconds, you will see the headset name and MAC address (xx:xx:xx:xx:xx:xx)While scanning, we will kill Bluealsa, and start PulseAudio. so in terminal 2 type
//Terminal 2//
sudo killall bluealsa
pulseaudio --start
Go
Back to terminal 1 and type
//Terminal 1//
pair xx:xx:xx:xx:xx:xx
trust xx:xx:xx:xx:xx:xx
connect xx:xx:xx:xx:xx:xx
At this step, you should have your device successfully connected to Raspberry Pi.
>> Now in the same terminal type
//Terminal 1//
pacmd list-cards
pacmd set-card-profile bluez_card.xx_xx_xx_xx_xx_xx a2dp_sink
pacmd set-default-sink bluez_sink.xx_xx_xx_xx_xx_xx.a2dp_sink
Note: After the command "pacmd list-cards" you should your bluez card from the list. If you don't see your card then you haven't followed the steps correctly. Try it again and make sure you have installed all bluetooth libraries and dependencies correctly.
>> Now Test your GStreamer
gst-launch-1.0 audiotestsrc ! alsasink
//or you can try//
gst-launch-1.0 audiotestsrc ! alsasink device=bluez_sink<XX_XX_XX_XX_XX>.a2dp
Now your Gstreamer should work. if it still doesn't work visit this link:https://developer.amazon.com/en-US/docs/alexa/avs-device-sdk/troubleshooting.html
2. Common: "App gets stuck while making or building the SDK by using the "make" command". This problem occurs if your raspberry pi gets overloaded or overheated while building the app. Make sure have placed good quality heatsink plus have attached the raspberry pi cooling fan to it which prevents overloading and overheating.(This can also happen if you are using Raspberry pi zero w which has a single core)
How to start the app
1.Keep your Bluetooth speaker in connecting Mode(In this mode the speaker's light will keep blinking)
Note:This step is very important. If your speaker is already connected to Bluetooth then while launching the app the speaker might get disconnected. I don't know why this happens and I myself am trying to
figure this out.
- Also, keep in mind every time you boot your pi you have to perform these steps:
pi@raspberrypi:~ $ sudo bluetoothctl
bluetooth : connect <SPEAKER ADDRESS>
2.Launch the app while the speaker is in connecting mode
Note: If your speaker has an inbuilt Microphone then you don't have to connect
the
USB Microphone to it.
3.Speaker will connect and you can start talking to Alexa.
Note: If you have any doubts DM me or comment in the comment's section below.
Working Video: Don't forget to check this cool working video:)
Comments