Overview
Amazon Alexa, also known simply as Alexa, is a virtual assistant AI technology developed by Amazon that is primarily available on mobile and smart home devices.
The ReSpeaker 4-Mics Pi HAT is a quad-microphone array expansion board for Raspberry Pi. This means you can build a more powerful and flexible voice product that integrates Amazon Alexa Voice Service, Google Assistant, and can also be used as voice recognition, voice recording, and many more,
In this series of posts, there will be two tutorials on setting up Amazon Alexa on Raspberry Pi. This post is the first in the series and basically, we'll learn how to integrate Amazon Alexa to the Raspberry Pi with ReSpeaker 4-Mics Pi HAT.
Hardware
- Raspberry Pi 3B+ (Compatible on Raspberry Pi Zero and Zero W, Raspberry Pi B+, Raspberry Pi 2 B, Raspberry Pi 3 B, Raspberry Pi 3 B+, Raspberry Pi 3 A+)
Software
- RPI terminal
- Internet Browser (Example Chrome)
Application Discussion
Amazon Alexa, also known simply as Alexa, is a virtual assistant AI technology developed by Amazon, first used in the Amazon Echo smart speakers developed by Amazon Lab126. With the integration of the Raspberry Pi and ReSpeaker device, we can make a portable Alexa device that enables voice-activated searches, playing music, and so on.
A ReSpeaker 4-Mics Pi HAT is a quad-microphone expansion board for Raspberry Pi. This board is developed based on AC108, a highly integrated quad-channel ADC with I2S/TDM output transition for high definition voice capture. Besides, it provides a super cool LED ring, which contains 12 APA102 programmable LEDs.
ReSpeaker 4-Mics Pi Hat Expansion Board:
Other than its audio feature, here are the other components included on the board.
- MIC: 4 analog microphones
- LED: 12 APA102 programable RGB LEDs, connected to SPI interface
- Raspberry Pi 40-Pin Headers: support Raspberry Pi Zero, Raspberry Pi 1 B+, Raspberry Pi 2 B, Raspberry Pi 3 B, and Raspberry Pi 3 B+
- AC108: highly integrated quad-channel ADC with I2S/TDM output transition
- I2C: Grove I2C port, connected to I2C-1
- GPIO12: Grove digital port, connected to GPIO12 & GPIO13
Setup the Hardware
Mount the ReSpeaker 4-Mics Pi HAT to Raspberry pi like this.
Setup up the Software
Alexa Setup
- Go to the Alexa Voice Service Developer Console, or go to the URL indicated below.
https://developer.amazon.com/alexa/console/avs/home
- Log-in if you have already an Amazon Account, if not then register a new account and log-in. The screen below will appear. Next, click the "MANAGE YOUR PRODUCTS" button.
- The "Product" tab will be shown, click the "ADD NEW PRODUCT" button to add a new product.
- Fill up the product information form, you can copy the example data as shown below. When your done filling-up the information click the "Next" button.
- Next, is to create LWA security profile. Click the highlighted "CREATE NEW PROFILE" text then fill up the information needed. You may copy the information below, or you can have your own info. If you decide to have your own info, on the "Security Profile Name" don't use "ALEXA" as a single word it will cause an error when creating a security profile. After filling up the information click the button "Next".
- Click the "Other devices and platforms" tab and fill in the "Client ID name". If you decided to have your own info from the last instruction, and if you used spaces, fill it up and change the space with "_", like "rpi_security". If not, copy the info in the image below. Then click the "GENERATE ID" button.
- Then click "DOWNLOAD" button, it will download a config.json file, we will use later. Check the "I agree to the Amazon Developer Service Agreement", then click the "Finish" button.
- You're done creating a product and a LWA security profile, now we proceed to enable the security profile by going to "Login with Amazon" homepage by clicking the link below.
- Then select on the drop-down box of your LWA Security profile, and click "Confirm".
- Add a "Consent Privacy Notice URL", if you are using this for personal use, you can set this to a fake URL to proceed. You can copy the fake URL below. Then click "Save"
- Now you are done, we will proceed to install Alexa to Raspberry Pi.
Alexa integration to Raspberry Pi
- Open your RPI terminal, and input the following commands.
$ sudo apt-get update $ sudo apt-get upgrade
- Next, is to download all the installation and configuration script from the Alexa-device-sdk Github. These scripts will setup and configure Alexa on your Raspberry Pi. Download all these scripts by running these 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
- Next, if you downloaded the config.json file earlier on your raspberry pi move it to the "/home/pi" directory and skip the next command. If not, create a new file by inputting this command.
$ sudo nano ~/config.json
- Copy the contents of config.json file you downloaded earlier and paste it into this file. The contents should be like this as shown below.
{ "deviceInfo": { "clientId": "amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "productId": "AlexaRPI" } }
- Now run the "setup.sh" script through below command
$ bash setup.sh config.json
- This message as shown below will be prompted after you run the command. To proceed, you will need to input "AGREE" and press ENTER to continue. If you do not agree input "QUIT" and press ENTER but the installation process will stop.
############################################################################# ############################################################################# 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". ############################################################################# #############################################################################
- Note: In case you encounter some issues while installing, the next steps are your guide. Also, it takes a long time to install due to the many packages to be installed.
- Note: If you encountered this error "fatal error curl/curl.h: No such file or directory", you need to install the libcurl package first. Input these commands below to install the package. After that re-run the setup.sh command
$ sudo apt-get install -y libcurl4-openssl-dev
- Note: If your device freezes and the install process stops, you need to increase your "Swap File Size". In order to do that input these commands.
$ sudo dphys-swapfile swapoff $ sudo nano /etc/dphys-swapfile
- Look for the "CONF_SWAPSIZE" value and change the value to 1024 as shown below. After that re-run the setup.sh command
CONF_SWAPSIZE=1024
- Note: If your device still freezes and the install process stops after increasing swap file size, Raspberry Pi might have overheated. In most cases, you can fix this issue by unplugging the Raspberry Pi power cord and waiting a few minutes until it cools down. After it's cooled off, boot the Raspberry Pi back up. When you get back to your desktop, re-run the setup.sh command to finish your install. To prevent this from happening you can buy any compatible fan. You can also use another safe method of cooling the device, such as pointing a room fan at it
- After finishing the setup we will proceed to install the ReSpeaker audio.
Install ReSpeaker 4-Mics Pi HAT driver
- Install the seeed voice card driver for ReSpeaker 4-Mics Pi HAT by inputting these commands on the RPI terminal.
$ sudo apt-get update $ sudo apt-get upgrade $ git clone https://github.com/respeaker/seeed-voicecard.git $ cd seeed-voicecard $ sudo ./install.sh $ reboot
Or you can use this driver instead, this is a copy of the Seeed voice card driver that is available in our Github Library. Use these commands.
$ sudo apt-get update $ sudo apt-get upgrade $ git clone https://github.com/createlabz/seeed-voicecard $ cd seeed-voicecard $ sudo ./install.sh $ reboot
- Check if ReSpeaker 4-Mics Pi HAT is recognized by using this command
$ arecord -l
- Check the list of capture hardware devices. You should see the ReSpeaker 4-Mics Pi HAT recognized like the below response (Take note of the card # and the device # as displayed below):
**** List of CAPTURE Hardware Devices ****
card 1: seeed4micvoicec [seeed-4mic-voicecard], device 0: bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0 [bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
- Check if your audio output is recognized by using this command below. For this tutorial I'm using an audio jack.
$ aplay -l
- Check the list of audio output devices. Look for the audio output device you want to use and take note of the card number and the device number as exampled below:
**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
- Test Speaker Audio Output by using this command.
$ speaker-test -t wav
- Note: For the RPI OS December 2020 onwards that is using PulseAudio. Encountered some issues on Alexa using PulseAudio, the problems are not detecting the ReSpeaker device and no audio. Resolved some of the problems but still, having audio issues. So reverted back to Alsa Utils to solve the issues. In order to revert you need to input these commands.
$ sudo apt purge pulseaudio
$ sudo apt autoremove
$ sudo apt-get install alsa-utils
$ sudo reboot
- Then create .asoundsrc file to set default audio input and ouput device. In order to create the file run this command.
$ sudo nano /home/pi/.asoundrc
- Then edit and copy the contents below, change the card number and device number for each device. For the microphone which is "pcm.mic" and for the speaker which is "pcm.speaker". Then paste it to the .asoundsrc file.
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>"
}
}
- Call alsamixer to change the volume of the speaker.
$ alsamixer
Alexa Authorization and Start Sample
- Now we can start the sample Alexa app by running this command.
$ bash startsample.sh
- The first time you start the sample Alexa app, you will be shown this message. This message is to Authorize your Raspberry Pi device to connect to Alexa. Go to Amazon Code and follow the prompts to approve the connection. You may have to scroll up to see the message.
################################## # NOT YET AUTHORIZED # ################################## ############################################################################# # To authorize, browse to: 'https://amazon.com/us/code' and enter the code: {XXXX} # #############################################################################
- After approval, this message will be displayed. You may have to scroll up to see the message.
########################### # Authorized! # ###########################
- Then you're done setting up Alexa. Now you may ask Alexa by saying "Alexa" then your query message. For example, say "Alexa" then say "What is amazon". Next guide is to setup your Alexa location for the weather query.
- Also there's commands to configure your Alexa, just follow the commands below.
+----------------------------------------------------------------------------+ | Options: | | Wake word: | | Simply say Alexa and begin your query. | | Tap to talk: | | Press 't' and Enter followed by your query (no need for the 'Alexa').| | Hold to talk: | | Press 'h' followed by Enter to simulate holding a button. | | Then say your query (no need for the 'Alexa'). | | Press 'h' followed by Enter to simulate releasing a button. | | Stop an interaction: | | Press 'going interaction. | | Privacy mode (microphone off): | | Press 'm' and Enter to turn on and off the microphone. | | Playback Controls: | | Press '1' for a 'PLAY' button press. | | Press '2' for a 'PAUSE' button press. | | Press '3' for a 'NEXT' button press. | | Press '4' for a 'PREVIOUS' button press. | | Settings: | | Press 'c' followed by Enter at any time ts' and Enter to stop an ono see the settings screen. | | Speaker Control: | | Press 'p' followed by Enter at any time to adjust speaker settings. | | Firmware Version: | | Press 'f' followed by Enter at any time to report a different | | firmware version. | | Info: | | Press 'i' followed by Enter at any time to see the help screen. | | Reset device: | | Press 'k' followed by Enter at any time to reset your device. This | | will erase any data stored in the device and you will have to | | re-register your device. | | This option will also exit the application. | | Reauthorize device: | | Press 'z' followed by Enter at any time to re-authorize your device. | | This will erase any data stored in the device and initiate | | re-authorization. | | | | Quit: | | Press 'q' followed by Enter at any time to quit the application. | +----------------------------------------------------------------------------+
Alexa Setup Location for Weather Query
- For weather queries, we need to set up your location first. In order to do this go to this Alexa Settings webpage. Click your device, as the example indicated below.
- Click "Edit" on the "Device location" field.
- Fill up the information needed as per example below. Then click the button "Save".
- After saving you may change other information. Then you can query the weather by saying this command: Say "Alexa" then "Weather today".
Libraries Used
https://github.com/alexa/avs-device-sdk
https://github.com/respeaker/seeed-voicecard.git
Demo Video
Conclusion
Alexa is helpful in day to day use, by checking the weather, time, or any inquiry, play music, and so on. Integrated to Raspberry with the ReSpeaker 4-Mics Pi you can have a portable device or a low-cost Amazon Alexa device installed at your home. This can also be a smart home device to control your lights, check your power rating, check room temperature, and many more.
Check other tutorials in this series: Integrating Amazon Alexa to Raspberry Pi
References
https://pimylifeup.com/raspberry-pi-alexa/
https://wpitchoune.net/tricks/raspberry_pi3_increase_swap_size.html
https://zoomadmin.com/HowToInstall/UbuntuPackage/libcurl4-openssl-dev
https://developer.amazon.com/en-US/docs/alexa/alexa-voice-service/register-a-product.html