Taillieu.Info

More Than a Hobby..

Add voice controls to your Raspberry Pi using Jasper

Jasper is an open-source voice-control platform that runs on a variety of systems, including the Raspberry Pi. You can use it to easily create a voice-controlled application that can add things to your Google Calendar, play Spotify playlists, or even accept commands to control your entire home using a platform like OpenHab.

Today I will show you how to install and configure Jasper on your Raspberry Pi so that you can send it voice commands and have it do cool things.

Jasper vs. Alexa
Recently, Amazon has open-sourced Alexa, the voice-control platform used in its Amazon Echo. Because Alexa is newer, it has fewer prebuilt modules and integrations than Jasper does; in addition, Jasper utilizes a number of different Speech To Text (STT) and Text To Speech (TTS) engines and can run in offline mode, making it fundamentally different. However, the Jasper project is not as active as Alexa.

Is Jasper better than Alexa?
No, it's just different. However, because Jasper doesn't require you to apply for a developer key like Alexa does, we'll start here for Raspberry Pi voice-controlled applications.

I have plans to add a different guide for Alexa soon as well, so stay tuned!

1

If you came from the Raspberry Pi MagicMirror installation guide, then your Pi is already ready to install Jasper and you can skip to step 5. If not, download the latest Raspbian Jessie disk imageand burn it onto your SD card.

2

Connect your Pi, USB microphone, stereo speakers, HDMI cable (optional), network cable (or WiFi dongle), and AC adapter. You don't necessarily need to have a monitor available since we'll be connecting remotely to the Pi to install and run Jasper, but it might be nice to have to view the output in its native environment.

3

Open Terminal (OS X) or Command Prompt (Windows) and find your Raspberry Pi's IP:

ping raspberrypi

Then, connect to your Pi using the default Raspberry Pi username/password:

ssh pi@your-pis-ip
password: raspberry

For security reasons, it's advisable to change the default Raspberry Pi username and password.

4

Now that the image has been burned to your Pi's SD card, we need to tell your Pi to use all available space by expanding the filesystem. Run:

sudo raspi-config

Highlight the first option and press enter. Then, press ESC to get back to the shell. Finally, reboot:

sudo reboot
5

Reconnect to your Pi and make sure everything is up to date:

sudo apt-get update
sudo apt-get upgrade --yes
6

There are three primary options to install Jasper on the Pi when using Raspbian Jessie: manually, using a disk image, or with an install script.

Option 1: Manual installation
Pros: Doesn't require moving a large image file around.
Cons: Can get complicated and difficult to troubleshoot; the issue is that Raspbian Jessie is too new of an operating system version and Jasper for Raspbian Jessie is neither well-documented nor well-supported.

Option 2: Use a disk (SD card) image
Pros: In theory, you install it and it works.
Cons: The file is huge (I'm talking 30GBish huge); also, perhaps more importantly, no Raspbian Jessie image exists per the Jasper support forum.

Option 3: Use an install script
Pros: Works great, and automates much of the leg work.
Cons: Can't think of any.

Richard Nelson/unixabg wrote a great install script that he shared on the Jasper support forum. We forked this script and made some minor changes to improve support for Raspbian Jessie. This is the script we'll use to install.

Get the Jasper for Jessie install script: 

cd ~/
wget https://raw.githubusercontent.com/Howchoo/raspi-helpers/master/scripts/jasper-installer.sh

Run the install script:

sudo chmod +x jasper-installer.sh
sudo ./jasper-installer.sh

You will now see the following:

######################################################
Welcome to the jasper-installer.sh script.
######################################################

######################################################
The installer script can install Jasper with or
without local Speech To Text (STT) support. By default
the jasper-installer.sh script will assume you want
network based STT. If you want local STT please answer
the question accordingly.

Select the desired STT support of (NETWORK or LOCAL)
(default: NETWORK) 

Edit: We'll want to choose LOCAL because (it will break if you don't)?

Specify whether you want local (installed directly on the pi) or network (internet access required for use) STT support. Since my Pi will always be connected to the Internet (and it's simpler to use a remote service), I'm going to specify network:

NETWORK

It will take a bit to download and install all the necessary dependencies.

7

If you see an error similar to the following:

Could not open requirements file: [Errno 2] No such file or directory: '/root/jasper/client/requirements.txt'

Run:

sudo chmod +x ~/jasper/client/requirements.txt
sudo pip install --upgrade -r ~/jasper/client/requirements.txt

This will read Jasper's required packages from requirements.txt and install/upgrade them accordingly.

8

Now we'll set up our Jasper profile. This information will help us set up various integrations, localization of results, etc.

cd ~/jasper/client
python populate.py

Enter whatever information you'd like (First name, Last name, email address, phone number, time zone, etc.)

This profile will be used for certain add-ons that will provide things like weather, email notifications, text message alerts, etc.

Then, specify whether you'd rather receive notifications by text or email.

When prompted which STT (Speech to Text) engine you'd like to use, I recommend using PocketSphinx. Type the following and press enter:

sphinx

Editing your profile later
The aforementioned script basically just writes to Jasper's YAML profile file. To edit it later, run:

nano ~/.jasper/profile.yml
9

You can run Jasper manually by typing:

python /usr/local/lib/jasper/jasper.py

Note: The location into which you've installed Jasper may vary by your version of Raspbian. If you get a "not found" message try:

python /home/pi/jasper/jasper.py

You can also set Jasper to run at startup:

crontab -e

And insert the following line:

@reboot python /usr/local/lib/jasper/jasper.py;
# or, depending on your installation location:
# @reboot python /home/pi/jasper/jasper.py

Save and exit. Finally, Reboot your Pi:

sudo reboot

Troubleshooting note
I ran into the following error when trying to run Jasper for the first time:

RuntimeError: hmm_dir '/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k' does not exist! Please make sure that you have set the correct hmm_dir in your profile.

To fix this, you'll need to open up stt.py located at /usr/local/lib/jasper/client/stt.py.

vim /usr/local/lib/jasper/client/stt.py

Find the following line:

    def __init__(self, vocabulary, hmm_dir="/usr/local/share/" +
                 "pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"):

and change it to:

    def __init__(self, vocabulary, hmm_dir="/usr/share/" +
                 "pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"):

You just need to remove the word "local".

10

Jasper is an "always listening" system, similar to Siri or Google Now. For example:

You: "Jasper"
Jasper: high beep
You: "What's the weather like tomorrow?"
Jasper: low beep
Jasper: (speaks the forecast)

Jasper comes preinstalled with a number of modules (commands) that you can use. You can also add custom modules, or create your own.

Included modules:

Time: "What's the time?"
Weather: "How's the weather?" Or, "what's the weather like tomorrow?"
News: "What's in the news?"
Gmail: "Do I have any email?"
Hacker News: "What's on Hacker News?"
Facebook Notifications: "Facebook notifications?"
Birthday: "Who has a birthday today?"
Jokes: "Tell me a knock-knock joke."
Life: "What is the meaning of life?"
11

Many additional modules can be installed to enhance your time with Jasper.

Google Calendar
Retrieve and add events to your Google Calendar. Documentation

Spotify
Speak commands to Jasper and he'll play songs and playlists from Spotify. Documentation

OpenHab
Control OpenHab using voice commands. OpenHab is an awesome open-source home automation system (that also runs on the Raspberry Pi!) Look for guides on OpenHab in the future; I'll be posting some. Documentation

WolframAlpha
WolframAlpha is an amazing computational search engine. Want to know how tall the Eiffel Tower is? Or when Paris was founded? Just ask Jasper, and he'll ask WolframAlpha. Documentation

Movies
Ask Jasper about a movie, and he'll return information about the movie: length, IMDB rating, genre, etc. Documentation

Wikipedia
Ask Jasper about a topic, and he'll give you the Wikipedia summary of the article. Documentation

Other modules
This is a fraction of the custom modules that have been written. For others not on this list, search around Github a bit.

Creating your own custom modules
Want a module that will reply with the answer to an inside joke? (Or do something more useful than that?) You're only a few lines of code away.

12

Problems? Questions? Post in the comments section below and I'll do my best to help you out! Or the [Jasper Support Forum] is a great place to start.

If you're looking for a good Jasper project to start off with, I made a voice-activated magic mirror guide that's really straightforward.

Have fun!