Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.
In this tutorial I'll show you how to install Voice Command, configure it, and teach it to turn a lamp on and off. The control of mains power is a tricky and dangerous subject for hobbyists so you'll be using a prebuilt solution for controlling the voltage to the lamp.
Gather the Components and Tools
- Raspberry Pi - Model A or B with power supply and SD card
- PowerSwitch Tail II
- Female to Male Jumper Wire
- Raspberry Pi compatible microphone. Either a USB Webcam with mic or a USB sound card with a microphone will do. This list can help youidentify one in your budget
- Speakers for sound output from your Raspberry Pi
- Household Lamp
- Small, flat blade screw driver
Hook Up the Components
Before you plug anything into power, hook up the external hardware to the Pi. The setup is fairly basic.
- Connect the microphone or webcam to the Pi USB port
- Connect the external speakers to the Pi. You can use the audio out jack or use sound out through the HDMI connection
- Connect a black female to male jumper to physical pin 6, a ground pin. Use this GPIO pinout diagram for help
- Connect a red jumper to physical pin 16 of your Pi
- Connect the male end of the red jumper to pin 1 on the PowerSwitch Tail II. Use the flat blade screw driver to loosen the screw and insert the pin into the hole in the side of the PowerSwitch. Then tighten the screw until the jumper pin is firmly attached
- Connect the male end of the black jumper to pin #2 on the PowerSwitch Tail II
Setup the Raspberry Pi
There are several guides to purchasing and installing an OS on your Raspberry Pi. Install the latest Raspbian operating system.
You will be working from the command line and doing a bit of copying and pasting so ensure that sshd is enabled. Also ensure that you can ssh into your Pi from your favorite terminal program. Once your Raspberry Pi is configured and booted move on to the next step.
Tip: If you need to know more about flashing an SD Card, for your Raspberry Pi, just refer to our tutorials: How to Flash an SD Card for Raspberry Pi and How to Install NOOBS on a Raspberry Pi With a Mac.
Install Wiring Pi
The script that controls the lamp relies on the Wiring Pi project to switch the GPIO pins high and low. Run the following commands to download and install Wiring Pi.
sudo apt-get install git-core git clone git://git.drogon.net/wiringPi cd wiringPi ./build
Create the lightswitch Script and Test
Now that the commands to turn pins high and low are installed with the Wiring Pi kit, the next step is to create a script called lightswitch. lightswitch will be called from the Voice Command framework. It will receive a command line argument on or off depending on your instructions. The script evaluates the argument and sets the pin high or low appropriately.
- Run the following commands to create a scripts directory
cd ~ mkdir scripts cd scripts
#!/bin/bash if [ $# > 1 ] then /usr/local/bin/gpio mode 4 out if [[ "$1" = "on" ]] then /usr/local/bin/gpio write 4 on fi if [[ "$1" = "off" ]] then /usr/local/bin/gpio write 4 off fi fi
chmod u+x lightswitch
./lightswitch on ./lightswitch off
If everything is all hooked up correctly you should see your lamp turn on and off.
Install Voice Command
Voice Command is a program that will run on the Raspberry Pi and listen for a keyword that means you are addressing it. Imagine your favorite Star Trek character addressing the ships computer by simply saying "Computer". It's like that and just as cool.
After hearing its keyword or name, the Voice Command will prompt for an instruction by default saying "Yes sir". It will then attempt to process whatever you say depending on how its configured.
In this step you'll install the Voice Command software. Voice Command installs as part of a pretty neat suite of tools. However, only the dependencies and voicecommand components are needed for this tutorial. When the setup script runs, it will ask if you wish to install several packages only say yes to dependencies and voicecommand.
Execute the commands below.
git clone git://github.com/StevenHickson/PiAUISuite.git cd PiAUISuite/Install/ ./InstallAUISuite.sh
After Voice Command installs it will prompt you to allow it to setup. Choose yes to allow the install script to auto setup. When the setup is complete it will prompt you to press Enter to edit the config file. Press Enter to edit the file and see the next section for configuration.
Configure and Run Voice Command
After pressing Enter you will be offered a file editor to modify the Voice Command setup. Add the following line to the config file, save, and exit.
This line means that when you say light on or light off Voice Command will execute the script /home/pi/scripts/lightswitch with the argument on or off. This will have the same result as when you ran the script manually.
Use the command line below to launch Voice Command. The -c means to run continuously, -k pi sets the name prompt you will say to get the Raspberry Pi's attention. The -v causes the program to verify the prompt before going into voice recognition mode.
The -i causes voicecommand to only process explicit commands listed in the config file. Without this flag voicecommand searches for answers to words that are not defined as commands on Wolfram Alpha and reads the results out loud.
Finally, the -b0 argument forces voicecommand to not use filler text before its response.
voicecommand -c -k pi -v -b0 -i
- Execute the above command
- Say clearly pi and wait for your pi to respond with "Yes Sir"
- Say clearly light on. The lamp should turn on
- Say clearly light off. The lamp should turn off
Congratulations! You've successfully voice controlled your lamp with your Raspberry Pi.
This tutorial combined a number of technologies to perform the job of turning on and off a lamp. I showed you how to install the PowerSwitch Tail II to safely switch mains power. I also showed you how to install Wiring Pi and create a script to handle the task of controlling the low level GPIO pins.
Finally, I showed you how to install the Voice Command package with its dependencies and configured it to respond to my instructions. Using Voice Command and custom scripts, you can automate a wide range of tasks through the Raspberry Pi. Voice Command can also do so much more without the -i flag.
Take some time to experiment with the voicecommand program and see what other things you can automate.