Setting up a Raspberry-Pi, Home-Assistant, Homebridge, Siri and Alexa. Part 4: Installing Alexa-Pi, Amazon’s Alexa Voice Service

Welcome to Part 4 of my multipart blog of setting up a Raspberry-Pi to be the center of your homeautomation world.

To recap what I’ve done so far:

  • Set the Pi in headless mode, ie it’s never going to be connected to a monitor
  • Installed HomeAssistant so I can control lights in my apartment, set automation like lights on at sunset if i’m home.
  • Installed Pi-Hole for network wide advert blocking.
  • Setup Homebridge, to link HomeAssistant to Apple Home on my IOS devices, so Siri can control my lights, show me temperatures in the apartment etc.

Here are the previous posts:

And now for making my Pi into a cheap Amazon Echo. Now know from the outset, that a RaspberryPi with Alexa cant do all the things an Echo can, it can’t control local devices locally, so it won’t naturally find my HomeAssistant install, I have to have a workaround with IFTTT which I’ll get to later on. It also doesn’t allow controlling of media, so you can’t link it to your Spotify or Amazon Music account. You also can’t set the location outside of Seattle, which is a pain, but not a dealbreaker.

So why do I want Alexa if it can’t do the above? I want Alexa to operate in my apartment for two main reasons. Firstly I want to be able to control the lights that I installed for my HA system, I want to tell Alexa to turn them off when I go to bed or on in the morning if the automation hasn’t worked for any reason. I also want to use it for normal everyday needs, converting measures, weather, news etc that Alexa does so well.

This post is a bit of a ramble, initially I’m going to install Alexa to prove it works, then I have to setup SSL to allow my HA install talk to the outside world, finally setup IFTTT to work with Alexa and my HA install.

Installing Alexa

Extra things you need:

I actually power my Pi from a powered USB hub, which is then plugged back into my Pi so I can use the hub itself from the Pi.

So go ahead, plug the USB microphone in, and the speakers into the 3.5mm jack and the power socket, in my case the USB hub. Go ahead and create an Amazon Developer Account and log into it, make up anything for your company name.

Setting up Alexa on your Amazon Developer account

  • Click on the Alexa tab at the top.
  • Click on Alexa Voice Service button
  • Click Register product button, top right then Device drop down

  • Device Type ID: my_device
  • Display Name: Anything you want, I went with Pi

Click Next.

  • Click the Select Security Profile tab, drop down and select Create new security profile.
  • Choose a Security Profile Name
  • Choose a Security Profile Description

I put Raspberry in both.

Click Next.

Make a note, or take a screenshot of Product ID, Client ID and Client Secret so it’s on hand for later.

Click the next tab Web Settings, and the edit button, then fill out:

  • Click Add Another by “Allowed Origins” and type: https://localhost:3000
  • Click Add Another by “Allowed Return URLs” and type: https://localhost:3000/authresponse

Click Next.

  • Choose a category, I went with Home Automation
  • Enter a description, I just used Pi
  • Click No for the couple of questions.

Click Submit bottom right.

So that’s your developer account setup, time to jump into the Pi.

Setting up your Pi

Unfortunately Amazon’s Alexa Voice Service requires a desktop, and our Pi doesn’t have one, but never fear, this is what VNC (virtual network computer) is for. Your version of Rasbian comes with a VNC server installed, you just need to run it, when we initially set the Pi in headless mode you could enable it from raspi-config if you wanted to, but for security and memory usage reasons we chose not to. You can start a VNC server on your Pi at any time and connect to it very easily.

SSH into your Pi, and type: vncserver to initialise a desktop, it’ll give some output saying it’s started.

Download a VNC app, if on a mac, just search for VNC Viewer, download and install, on a PC RealVNC does a good one, and there are options for your IOS/Android device too. You don’t need to make an account.

Open the viewer, at the top enter your Pi’s IP address and a :1 (this is the desktop number, if you type vncserver again in ssh, it’ll open a second desktop and you can use :2, and so on), in my example it’s 192.168.0.24:1 and use username pi and enter your ssh password to get in:

So if you’re new to this, it can be a bit of a headfuck. You’re running a computer without a desktop, but you’ve just accessed it’s desktop? Like wtf! Technology is great! This is a virtual desktop, but for all intents and purposes is a real desktop, just like you get when booting into Windows. If you close the VNC viewer window, you can reconnect to it, and it’ll be where you left it, with the same windows/apps open. If forever reason you want to close this virtual desktop you can kill it with vncserver -kill :1 in your ssh terminal. Be warned, if you close the virtual desktop, then you have to complete the steps below each time you want Alexa to run, so just close the VNC viewer and leave the virtual desktop running.

Open a terminal window by clicking the terminal icon on the top bar and type:

  • cd desktop
  • it clone https://github.com/alexa/alexa-avs-sample-app.git
  • nano automated_install.sh

This will launch the install script which you need to edit as according to your details earlier, here’s a screenshot of mine:

Make sure your ProductID is the same as earlier in my_device, I made the mistake of just using the three pieces of info from the security tab. Enter the ProductID, ClientID and ClientSecret after the = sign and save.

  • ctrl-x and y to save
  • cd ~/Desktop/alexa-avs-sample-app
  • . automated_install.sh
  • Press Y a few times during the install as prompted

This will take a good half an hour to install.

Next is run the web service.

  • cd ~/Desktop/alexa-avs-sample-app/samples
  • cd companionService && npm start

This will start the web service, leave this shell open.

Run the Sample App
Open a new shell tab, click File – New Tab.

  • cd ~/Desktop/alexa-avs-sample-app/samples
  • cd javaclient && mvn exec:exec
  • A java applet will open asking to open a browser window, click ok

  • Log into your Amazon Developer account on the browser window, if successful it’ll look like this:

  • Close the browser window, wait a few seconds and a token should appear in the Java window, click Ok on the applet

Start the Wake word Engine
Open another shell tab, click File – New Tab.

  • cd ~/Desktop/alexa-avs-sample-app/samples
  • cd wakeWordAgent/src && ./wakeWordAgent -e kitt_ai

That’s it, Alexa is now running. You can close the VNC Viewer window, it’ll leave the virtual desktop running, you can log back into it anytime.

If you ever close the shell windows, you will have to do the three steps above every time, start the Web Service, start the Sample App and confirm your account, Start the Wakeword engine.

Check the volume level in the top right window of your virtual desktop is up and say something like: “Alexa, what time is it” and it should respond. If it doesn’t you may have the mic set too low, you can edit this to make it louder:

Either in a new shell tab or back in SSH type alsamixer so this pops up:

  • Press F6, and choose the USB microphone

  • Press Tab to get to the capture option and the up arrow to turn up the mic gain
  • Press ESC to exit the mixer
  • sudo alsactl store to save the settings

That should have updated the mic.

At the end of all this you should have a virtual desktop that looks like this:

Once again, if you close any of the terminal windows, the java applet, or kill the virtual desktop via ssh then you’ll have to start the companion service, voice service and the wakeword agent all over again. Just close the VNC window, it’ll stay running in the background. Mine has been up for twenty odd days so far.

Now try talking to Alexa. My USB microphone is at about a foot from floor level, I actually removed the casing and cut out the grill area so there is more direct access to the little mic and I can talk to it from anywhere in my living/dining/kitchen room area in my two bed apartment.

You can now access http://alexa.amazon.co.uk or .com depending on your location and setup Alexa, you can add skills, for news, jokes and stuff. So try it out! Unfortunately you cannot change the location from Seattle, unless you’re in the USA. So if you say “Alexa, what’s the weather today?” She’ll give you the weather forecast for Seattle. I have to use “Alexa, what’s the weather in Sydney today?” You can change the timezone, so at least if you ask her to set alarms and what time is it, she works fine. “Alexa, what’s my flash briefing?” Is usually the first words out of my mouth in the morning as I boil the kettle.

The great info on this thread I’ve gleamed from the great article over at https://www.lifehacker.com.au/2016/10/how-to-build-your-own-amazon-echo-with-a-raspberry-pi/.

The next post will be interfacing Alexa with your HomeAssistant installation!

 

Edit 21/5/2017

A new version of the java client is available since i wrote this, meaning you don’t have to reopen a browser window to authenticate the tokens each time you restart the pi. I simply renamed the alexa desktop folder with an _old in front, and ran the install again from scratch.

Typically by doing this i couldn’t get Alexa to run the wakeword engine, and kept getting errors.

As I was using a USB microphone and 3.5mm jack, I edited my asoundrc file to this affect:

sudo nano ~/.asoundrc
pcm.!default { type asym playback.pcm { type plug slave.pcm "hw:0" } capture.pcm { type plug slave.pcm "hw:1" } }

I then restarted the pi sudo reboot, I restarted the companion service, restarted the javaclient, the wakeword engine and she works!

1 thought on “Setting up a Raspberry-Pi, Home-Assistant, Homebridge, Siri and Alexa. Part 4: Installing Alexa-Pi, Amazon’s Alexa Voice Service

Leave a Reply

Your email address will not be published.