amzn / alexa-avs-raspberry-pi
- воскресенье, 27 марта 2016 г. в 03:15:41
1,320 stars today
This project demonstrates how to access and test the Alexa Voice Service using a Java client (running on a Raspberry Pi), and a Node.js server.
This project demonstrates how to access and test the Alexa Voice Service using a Java client (running on a Raspberry Pi), and a Node.js server. You will be using the Node.js server to get a Login with Amazon authorization code by visiting a website using your computer's (Raspberry Pi in this case) web browser.
This guide provides step-by-step instructions for obtaining the sample code, the dependencies, and the hardware you need to get the reference implementation running on your Pi.
Tick the box next to Raspbian and click on Install.
Raspbian will then run through its installation process. Note: this can take a while.
NOTE: To load the graphical user interface at any time type startx into the command line.
More info: raspberrypi.org
NOTE: You will be using the Terminal utility on the Raspberry Pi to install the utilities you need for this Alexa Voice Service walkthrough. Terminal comes preinstalled on the Raspberry Pi, and you can get to it from the Desktop. You can learn more about Terminal here.
SSH allows you to remotely gain access to the command line of a Raspberry Pi from another computer (as long as they are both on the same network). This removes the requirement to have an external monitor connected to your Raspberry Pi.
SSH is enabled by default on Raspberry Pi. If you run into problems getting SSH to work, make sure it’s enabled. This is done using the raspi-config utility.
Type the following in the Terminal:
sudo raspi-config
Then navigate to SSH, hit Enter and select Enable SSH server.
Now let's SSH into your Raspberry Pi. To do that, you need to know the IP address of your Raspberry Pi.
Type this command into the terminal:
hostname -I
> 192.168.1.10 //this is an example Raspberry Pi’s hostname, it would be different for you
If you’re on a Windows PC, follow the instructions here to SSH Using windows
Now that you know the IP address of your Raspberry Pi, you are ready to connect to it remotely using SSH. To do this, open the terminal utility on the computer you would like to connect from and type the following:
pi@<YOUR Raspberry Pi IP ADDRESS>
It will prompt you for your password. NOTE: the default password for the user pi is raspberry
Voila! You’re now remotely connected to your Raspberry Pi. Now you’ll install all the utilities while connected remotely via SSH.
VNC is a graphical desktop sharing system that will allow you to remotely control the desktop interface of your Raspberry Pi from another computer. This will come in very handy as you get rid of the external monitor connected to your Raspberry Pi.
sudo apt-get install tightvncserver
Start VNC Server
To start the VNC Server, type: tightvncserver
Run VNCServer at Startup
You want to make sure the VNC Server runs automatically after the Raspberry Pi reboots, so you don’t have to manually start it each time with the command tightvncserver through SSH. To do that, type the following in the terminal:
cd /home/pi
cd .config
Note the '.' at the start of the folder name. This makes it a hidden folder that will not show up when you type 'ls'.
mkdir autostart
cd autostart
Create a new configuration by typing the following command:
nano tightvnc.desktop
Edit the contents of the file with the following text:
[Desktop Entry]
Type=Application
Name=TightVNC
Exec=vncserver :1
StartupNotify=false
Type ctrl-X and then Y to save the changes to the file.
That's it. The next time you reboot the VNC server will restart automatically.
Connecting to Raspberry Pi via VNC
You may now disconnect the Monitor, keyboard and mouse (if you like). Now with SSH (allows remote access to the terminal) and VNC (allows you to remote control the Raspberry Pi’s desktop interface) installed, the external monitor is optional. Feel free to disconnect it from the Raspberry Pi.
Get VLC media player by typing:
sudo apt-get install vlc-nox vlc-data
NOTE: If you are running on Raspberry Pi and already have VLC installed, you will need to remove two conflicting libraries by running the following commands:
sudo apt-get remove --purge vlc-plugin-notify
sudo rm /usr/lib/vlc/plugins/codec/libsdl_image_plugin.so
Unable to fetch errors If you run into some "Unable to fetch" errors while trying to install VLC, try the following:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vlc-nox vlc-data
Source: https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=67399
Make sure VLC is installed correctly
whereis vlc
This will tell you where VLC is installed.
Most programs are stored in /usr/bin
. On my Raspberry Pi, I see:
vlc: /usr/bin/vlc /usr/lib/vlc /usr/share/vlc /usr/share/man/man1/vlc.1.gz
Set the environment variables for VLC
Type the following into the terminal:
export LD_LIBRARY_PATH=/usr/lib/vlc
export VLC_PLUGIN_PATH=/usr/lib/vlc/plugins
Check if the environment variables were set successfully
echo $LD_LIBRARY_PATH
> /usr/lib/vlc
echo $VLC_PLUGIN_PATH
> /usr/lib/vlc/plugins
Verify Node isn't already installed. It should print 'command not found'.
node —version
> command not found
Now type:
sudo apt-get update
sudo apt-get upgrade
Set up the apt-get repo source:
curl -sL https://deb.nodesource.com/setup | sudo bash -
Install Node itself:
sudo apt-get install nodejs
You need to have Java Development Kit (JDK) version 8 or higher installed on the Raspberry Pi.
Step 1: Download JDK Assuming this is a fresh Raspberry Pi and you do not already have JDK installed, you'll need to download JDK 8 from Oracle.
Step 2: Extract the contents Extract the contents of the tarball to the /opt directory:
sudo tar zxvf jdk-8u73-linux-arm32-vfp-hflt.tar.gz -C /opt
Set default java and javac to the new installed jdk8.
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_73/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_73/bin/java 1
sudo update-alternatives --config javac
sudo update-alternatives --config java
NOTE: If asked to choose an alternative, type the number corresponding to the jdk version you just installed - for example - jdk1.8.0_73
Now verify the commands with the -version option:
java -version
javac -version
Step 1: Download Maven
Download the Binary tar.gz file apache-maven-3.3.9-bin.tar.gz from https://maven.apache.org/download.cgi
Step 2: Extract the contents Extract the contents of the tarball to the /opt directory
sudo tar zxvf apache-maven-3.3.9-bin.tar.gz -C /opt
Step 3: Tell your shell where to find maven You’ll do this in the system profile settings so it is available to all users.
Create a new file /etc/profile.d/maven.sh, and type the following inside it:
export M2_HOME=/opt/apache-maven-3.3.9
export PATH=$PATH:$M2_HOME/bin
Save the file. Log out and back into the Raspberry Pi so the profile script takes effect. You can test that it is working with the following command:
mvn -version
Get a free Amazon developer account if you do not already have one.
Download the sample apps zip file. By downloading this package, you agree to the Alexa Voice Service Agreement.
Device Type Info
Security Profile
Click on the Security Profile dropdown and choose “Create a new profile”
General Tab
Client ID and Client Secret will be generated for you.
Now click on the Web Settings Tab
Device Details
Amazon Music
You are now ready to generate self-signed certificates.
Step 1: Install SSL
sudo apt-get install openssl
Verify install
whereis openssl
> openssl: /usr/bin/openssl /usr/share/man/man1/openssl.lssl.gz
Change directories to <REFERENCE_IMPLEMENTATION>/samples/javaclient.
cd <REFERENCE_IMPLEMENTATION>/samples/javaclient - //your sample apps location
Step 2: Edit the text file ssl.cnf, which is an SSL configuration file. Fill in appropriate values in place of the placeholder text that starts with YOUR_.
Note that countryName must be two characters. If it is not two characters, certificate creation will fail.
Step 3: Make the certificate generation script executable by typing:
chmod +x generate.sh
Step 4: Run the certificate generation script:
./generate.sh
Step 5: You will be prompted for some information:
Step 6: Edit the configuration file for the Node.js server
The configuration file is located at:
<REFERENCE_IMPLEMENTATION>/samples/companionService/config.js.
Make the following changes:
IMP: Do not use ~ to denote the home directory. Use the absolute path instead. So, instead of ~/documents/samples, use /home/pi/documents/samples.
Step 7: Edit the configuration file for the Java client
The configuration file is located at:
<REFERENCE_IMPLEMENTATION>/samples/javaclient/config.json.
Make the following changes:
Change directories to <REFERENCE_IMPLEMENTATION>/samples/companionService
cd <REFERENCE_IMPLEMENTATION>/samples/companionService
Install the dependencies by typing:
npm install
Open a web browser, and visit https://developer.amazon.com/lwa/sp/overview.html.
Near the top of the page, select the security profile you created earlier from the drop down menu and click Confirm.
Click Save.
Next to the Alexa Voice Service Sample App Security Profile, click Show Client ID and Client Secret. This will display your client ID and client secret. Save these values. You’ll need these.
Login to the Raspberry Pi via VNC
Step 1: Update config.js Navigate to the following file and open it in a text editor.
<REFERENCE_IMPLEMENTATION>/samples/companionService/config.js
Edit the following values in this file -
products: {"my_device": ["123456"]}
Save the file.
Step 2: Update config.json Navigate to the following file, and open it in a text editor.
<REFERENCE_IMPLEMENTATION>/samples/javaclient/config.json
Edit the following values in this file:
Save the file.
Step 3: Preparing the pom.xml file
Navigate to the following file and open it in a text editor.
<REFERENCE_IMPLEMENTATION>/samples/javaclient/pom.xml
Add the following to the pom.xml in the < dependencies > section:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
<scope>compile</scope>
</dependency>
Login to the Raspberry Pi via VNC
In your terminal window or from the command prompt, type:
cd <REFERENCE_IMPLEMENTATION>/samples/companionService
npm start
The server is now running on port 3000 and you are ready to start the client.
Open a new terminal window/tab (SHIFT+CTRL+TAB in Raspbian)
cd <REFERENCE_IMPLEMENTATION>/samples/javaclient
Upgrade your Java version
Make the script executable by typing:
chmod +x install-java8.sh
Run the installation script:
./install-java8.sh
You will get a message from Oracle Java installer that you must accept the Terms of Service for Java SE Platform, press Enter.
Press Tab, and then Enter to say “Yes” to the Terms of Service.
Build the app
Before you build the app, let’s validate to make sure the project is correct and that all necessary information is available. You do that by running:
mvn validate
Download dependencies and build the app by typing:
mvn install
When the installation is completed, you will see a “Build Success” message in the terminal.
Run the client app:
You are now ready to run the client app by typing:
mvn exec:exec
When you run the client, a window should pop up with a message that says something similar to:
Please register your device by visiting the following website on any system and following the instructions: https://localhost:3000/provision/d340f629bd685deeff28a917 Hit OK once completed.
Copy the URL from the popup window and paste it into a web browser. In this example, the URL to copy and paste is https://localhost:3000/provision/d340f629bd685deeff28a917.
NOTE: Due to the use of a self-signed certificate, you will see a warning about an insecure website. This is expected. It is safe to ignore the warnings during testing.
You will be taken to a Login with Amazon web page. Enter your Amazon credentials.
You will be taken to a Dev Authorization page, confirming that you’d like your device to access the Security Profile created earlier.
Click Okay.
You will now be redirected to a URL beginning with https://localhost:3000/authresponse followed by a query string. The body of the web page will say device tokens ready.
Return to the Java application and click the OK button. The client is now ready to accept Alexa requests.
Click the Start Listening button and wait for the audio cue before beginning to speak. It may take a second or two for the connection to be made before you hear the audio cue.
Press the Stop Listening button when you are done speaking.
Ask for Weather: Click the Start Listening button. You: What's the weather in Seattle? Click the Stop Listening button. Alexa: Current weather report for Seattle
Some other fun questions you can ask Alexa
Once you hear the audio cue after clicking “Start Listening” button, here are a few things you can try saying -
More on Music Playback The "previous", "play/pause", and "next" buttons at the bottom of the Java client UI are to demonstrate the music button events. Music button events allow you to initiate changes in the playback stream without having to speak to Alexa. For example, you can press the "play/pause" button to pause and restart a track of music.
To demonstrate the "play/pause" button, you can speak the following command: Play DC101 on iHeartRadio, then press the "play/pause" button. The music will pause in response to the button click. Press the "play/pause" button again to restart the music.
Check to see if you are seeing the response coming through on the Terminal and if you see the response cards on your Alexa app. If yes, you probably need to force audio through local 3.5mm jack, instead of the HDMI output (this can happen even if you don’t have an HDMI monitor plugged in).
To force audio through local 3.5 mm jack, pen Terminal, and type
sudo raspi-config
See Raspberry Pi Audio Configuration
hostname -I
If you run into some "Unable to fetch" errors while trying to install VLC, try the following -
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vlc-nox vlc-data