Voice control on your Pi

By Russell Barnes. Posted

Use Amazon’s Alexa service on a Raspberry Pi and open up a world of voice-control projects

In the UK, all the Star Trek shows have just been put back on Netflix, reminding us of the desire to ask the computer for Earl Grey tea or Klingon coffee (we can’t start the day without a raktajino, you know). So it’s exciting to see Amazon’s Alexa is quite readily available on the Pi now. Let’s get it working, then, and make some projects.

You'll need

  • Alexa AVS
  • A constant internet connection
  • External speakers
  • A USB microphone

We need to start by installing VLC. Not just normal VLC, though – we need to install this one slightly differently. Open up the terminal and enter:

sudo apt-get install vlc-nox vlc-data

This might take a while; once its done, we then need to set the environment variables so we can access VLC from Alexa properly later. Do this with:

export LD_LIBRARY_PATH=/usr/lib/vlc
export VLC_PLUGIN_PATH=/usr/lib/vlc/plugins

Next, it’s time to download the Alexa files we need:

git clone https://github.com/amzn/alexa-avs-raspberry-pi

Now we need to install our dependencies: Node, JDK, and Maven. In the terminal, enter:

curl -sL https://deb.nodesource.com/setup | sudo bash -

And let it work. It will end by prompting you to install Node.js. Do that with:

sudo apt-get install nodejs

Next, use cd to move to /alexa-avs-raspberry-pi/samples/companionService and install npm with:

npm install

Once that’s finished, we need to then install a specific version of the Java Development Kit (JDK). Use cd to move to the alexa-avs-raspberry-pi/samples/javaclient folder and run:

./install-java8.sh

You will get a message from Oracle Java installer that you must accept the terms of service for the Java SE platform, which you need to now do.

Once that’s complete, download Apache Maven from magpi.cc/2bDPluf. Move to the Downloads folder and extract the contents with:

sudo tar xvf apache-maven-3.3.9-bin.tar.gz  -C /opt

You then need to create a file with some system settings for Maven. Start by creating the file like so:

sudo touch /etc/profile.d/maven.sh
sudo nano /etc/profile.d/maven.sh

Add the following to the file you just opened:

export M2_HOME=/opt/apache-maven-3.3.9
export PATH=$PATH:$M2_HOME/bin

Save and exit the file. Reboot your Raspberry Pi before continuing.

Certification

We now need to generate self-signed certificates:

sudo apt-get install openssl

Once installed, move back to /alexa-avs-raspberry-pi/samples/javaclient and run the script:

./generate.sh

It will ask you to enter some information. Enter the following details exactly as shown:

  • Product ID: my_device
  • Serial Number: 123456

Just press ENTER when it prompts you for a password and then let it run and generate a key.

Now we can get our details for the Alexa Voice Service; this does require an Amazon account, though. Go to developer.amazon.com and log in – we then had to ‘complete’ our registration before continuing, so be prepared to do so as well.

Once you’re at the dashboard, click on the Apps & Services tab, then Alexa. On Alexa Voice Service, hit Get Started. From the drop-down menu ‘Register a Product Type’, select Device.

On the first page, fill in Device Type ID as my_device and Display Name with My Device. Click Next to go to the security profile. Click on the Security Profile drop-down and choose ‘Create a new profile’.

Enter the following:

  • Security Profile Name: Alexa Voice Service Sample App Security Profile
  • Security Profile Description: Alexa Voice Service Sample App Security Profile Description

Click Next and your Client ID and Client Secret will be generated for you. Go to the Web Settings tab and make sure the security profile you just created is selected in the drop-down menu, then click the Edit button.

On Allowed Origins, click Add Another and then enter https://localhost:3000 in the text field that appears. For Allowed Return URLs, enter https://localhost:3000/authresponse and then click on Next.

We’re now on Device Details; first set the Category as Other. Use a description of ‘Alexa Voice Service sample app test’ and then choose ‘Longer than 4 months / TBD’ for the expected timeline question. Finally, enter 0 for for the number of devices you plan to commercialise and hit Next once more. On the next tab, click for Amazon Music and hit Submit. You’re done!

Final configurations

In a browser, go to the developer overview page. At the top of the page, select the security profile we created and click Confirm. You’ll need to enter a dummy web address for the consent privacy notice URL.

Click Save. Click on Show Client ID and Client Secret and make a note of them.

Back on the Pi, move the alexa code folder to the Desktop and rename it alexa-avs-raspberry-pi-master. In the terminal, open the following file:

nano /home/pi/Desktop/alexa-avs-raspberry-pi-master/samples/companionService/config.js

Post your Client ID and Client Secret in the fields clientID and clientSecret respectively, and then save and close the file.

Now we can get the Alexa service running. Start in the terminal with:

cd /home/pi/Desktop/alexa-avs-raspberry-pi-master/samples/companionService
npm start

Open a new terminal window. Move to alexa-avs-raspberry-pi-master/samples/javaclient and use:

mvn install

It will say ‘build success’ when done. Run it with:

mvn exec:exec

Talk to me

A window will pop up asking you to register the device. Copy the URL into a browser and log into Amazon, and click Okay on the next page to confirm everything. You’ll be redirected to a page saying ‘device tokens ready’.

Click OK on the original pop-up and you can start asking Alexa stuff. Hit the Start Listening button, wait for the audio cue, then ask it something like ‘what is two plus two’. Hit Stop Listening and it should reply ‘four’. And that’s it, you’re ready to play with Alexa!

Check out Akash Chandran's version of this tutorial which ours was based on!

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.