How to Control a Lamp With Voice Commands and a Raspberry Pi

Voice control is not just for smart phones and B-movies anymore. Steve Hickson has created a system to bring automation and the intelligence of Wolfram Alpha to your beck and call.

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.

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 Pipowerswitchtailpi600
    Pi With Jumpers Attached
  • 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 IIpowerswitchtail600
    PowerSwitch Tail II With Jumpers Attached

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.

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.

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
  • create the following script as a file named lightswitch
  • Set the script to be executable with the following command
  • Plug the PowerSwitch Tail II into mains power
  • Plug the lamp into the PowerSwitch Tail II
  • Test the script and your configuration by running the script to turn the light on then off

    If everything is all hooked up correctly you should see your lamp turn on and off.

    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.

    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.

    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.

    • 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.


    点击查看原文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值