Voice Controlled Lights

It’s not moop today but me again returning as a guest contributor. So, it’s not planes or lathes today but wireless plugs and stuff.

If you read my last blog post (http://www.moop.org.uk/index.php/2013/05/27/homemade-wireless-plug/) you’ll know that I tried building a cheap Bluetooth controlled plug socket that’d fit in a standard back box. When it came to actually installing though I got cold feet. There are safety and legal issue to fitting something like that into the wall so instead I opted for a commercially available version. I choose the LightwaveRF range of plug sockets and dimmer switches. These are just like what I was designing in that they just replace the standard fittings and are switchable remotely. With the LightwaveRF Wireless Controller this also includes controlling them across the network. They are pricey compared to my design but on the other hand, look a lot nicer and far less likely to catch fire.

The LightwaveRF network protocol is a fairly simple UDP scheme that’s very easy to hook into (http://blog.networkedsolutions.co.uk/?p=149). The only strangeness is having to send a message with ID 533 first time to get the LightwaveRF box to prompt to register a new device. Coding this only took a few minutes and once that was working the only thing left was to think of something to do with it!

The most obvious thing to me was to live out Star Trek (http://www.youtube.com/watch?v=hShY6xZWVGE) by controlling things in my house by talking to them. “Computer, lights on” for example. I didn’t want to leave a computer running all the time but I did have a Raspberry Pi lying around which I’ve been wanting to find a use for.

So, I started by looking for voice recognition technology to use. Doing a quick Google, bought up a lot of suggestions to use Google’s voice API (they would say that! ;) ). As much as I like Google, I don’t really want everything I ever say getting sent to their servers, so that was out. Next best choice seemed to be CMU Sphinx. pocketsphinx is designed for embedded systems so seemed a good fit for the Pi.

On first impressions I was very unimpressed with CMU Sphinx. The accuracy of recognition seemed absolutely terrible. However it does have facilities to work with a subset of words for doing digit recognition for example. With a text file of commands I wanted it to recognise the lmtool (http://www.speech.cs.cmu.edu/tools/lmtool-new.html) creates all the files needed. Using these files the quality of recognition improved considerably. However there were still problems distinguishing “on” and “off”. Also the word “Computer” was frequently misunderstood. So accepting this as a limitation of voice control, “Computer, lights on” became “Jeeves, lights up”. Finally I adapted the voice model (http://cmusphinx.sourceforge.net/wiki/tutorialadapt) which improved the voice recognition even further.

Up until this point I’d been doing everything on my laptop but moving this to the Pi was fairly straight forward. I now needed some sort of microphone though. I tried Singstar mics at first. These worked straight off but didn’t really do a very good job unless speaking directly into them. So next I tried another of my discarded game accessories, a PS3 Eye camera. This worked much better when picking up sound from across the room.

For sound output I used a pair of cheap PC speakers which I could connect to the 3.5mm audio jack on the Pi. I had spent some time trying to sort out a problem where the first second or so of audio when using HDMI output was silent. I tried updating the firmware and googled around for the answer. In the end I worked around this by appending a second of silence to the start of my audio output with sox. However when using the speakers the problem just went away anyway much to my relief.

As a finishing touch I made a quick LED display to indicate when the Pi was ready to accept the next command. While doing this, I decided to also wire up an IR LED so I could control the television too. Not having IR LEDs in my parts draw I just cracked open an old remote and took the LED out of there. I connect this to GPIO 7 as an arbitrary clock can be generated on that pin. I used this to provide the 38KHz modulation needed for my TV. I then toggled that pin between clock and a zero output in software to encode the commands to control the TV. This did not work at first. I could see on my laptop’s webcam that the IR LED was flashing away but the TV did not respond. Cracking out the oscilloscope I found that the pulses when the output was a clock were all around 200us too long. I assume this is due to a delay in toggling the state. I decided to take the easy way out and just adjusted these timings. After these adjustments I was successfully able to control my TV using just an IR LED and a resister.

So enough talk, here it is in action…

Nb. The source is downloadable here Lightwave Source. I warn you now though it’s some pretty ugly code. A lot of this coding was done crouched on the floor working directly on the Pi. Getting back off the floor was my top priority, not following any coding standards. Still thought I should include it just in case anyone wanted some reference. The main guts, speech.c is just a modified version of the pocketsphinx continuous speech sample. irTrans turns the TV on and off and lightwave.c deals with the network side of things. speaker.c just keeps festival open so I don’t have to pay the start up time when doing voice synthesis. Hope it’s helpful to someone.

36 thoughts on “Voice Controlled Lights

  1. Adam Baxter

    Can you please let me in on the secret of working IR transmission on the Pi?

    I’ve got both a receiver and transmitter on my Pi’s GPIO 17 and 18, receive works every time but no matter how hard I try I can’t get transmit to work.

    voltagex at voltagex dot org.

  2. Ellis Benus

    Absolutely blown away by this! You’re traffic is probably going to explode thanks to lifehacker featuring you. :-)

    Would you consider making one of these for someone else?

    I don’t have the electronics know how to tackle this, but I could purchase a version from you and purchase the light switches.

    Thanks! Let me know!

  3. Pingback: Add Voice Control To Your Home With LightwaveRF And A Raspberry Pi | Lifehacker Australia

  4. Pingback: Deporte24horasControl de voz en tu casa gracias a Raspberry Pi » Deporte24horas

  5. Pingback: Control de voz en tu casa gracias a Raspberry Pi | Civeloo News

  6. Pingback: Control de voz en tu casa gracias a Raspberry Pi | Descubridores de Tecnologia, Motor y Actualidad

  7. Pingback: Control de voz en tu casa gracias a Raspberry Pi | Ediary Blog

  8. Pingback: Control de voz en tu casa gracias a Raspberry Pi | New code Blog

  9. Pingback: Control de voz en tu casa gracias a Raspberry Pi | esPCializados

  10. Pingback: Add Voice Control to Your Home with LightwaveRF and a Raspberry Pi « Music RSS

  11. Pingback: ใช้เสียงสั่งงานระบบบ้านอัตโนมัติ | Raspberry Pi Thailand

  12. Pingback: Frambuesa Pi Colombia » Controle su hogar usando comandos de voz

  13. Marshall

    I was wondering if you could build this for someone like me with little technical knowledge to be able to turn on a ps3 and tv each with their own command. No lights or time needed. I have my own ps3 eye and speakers. What do you think?

  14. Pingback: Control de voz en tu casa gracias a Raspberry Pi | ITech Soluciones Informáticas

  15. Andrew

    Hi, congratulations for the project :)
    I want to achieve something like this, but without LightWaveRF, because i’m in Italy. There is a scheme of input/output for interface with a relay system? Thank you :)

  16. matej

    hi, are you using pulseaudio or alsa? it runs smoothly when i use alsa and logitech webcam.. unfortunately only at close range. so i tried ps3 eye camera but i cannot get it running with alsa. with pulseaudio its too slow:(

  17. viraj

    I liked your project very much.
    I am new to raspberry pi and i am not getting how to install every thing of your project idea.
    Can you please elaborate a little more of the installation process or can you make a short video of it.
    Or you can upload the raspbian image with all your project installed on it.
    Please help me.
    Thank you.

  18. Joe

    I am new to the raspberry pi and was wonder if you would tell me what gpio pins you use for the led indicaters so that I could just controll them and how I would get a line of LEDs to flash
    Also could you cut the unnessary lines out of the code the light switch control and tv as only want it to control a line of LEDs or a led cube

  19. James


    How did you get such a good speed increase whilst using pocketsphinx? There seems to be at least a second or two pause between the “READY” stage, and the “LISTENING” stage.


  20. craig

    Could you list all the equipment needed please and If i purchase the same equpiment could i just run your source and just alter one or two variables (naming of switches etc) and it will work?

    Any info would be great as i am a novice with rasberry pi and this sort of tech.

  21. Oliver

    Hello mate,

    Could you roughly explain how to install the source code that you link to in your post? just a few steps on how to get the basics up and running on my pi would be great!

    Thanks :)

    1. Kent

      First off- AWESOME Charlie!! I have a similar thing going at home now- mostly from your example…
      for the others out there looking for step by step:
      has a great tutorial just ignore the racket and arduino stuff…
      Just a side note… make sure you have a fresh RPi install and have already set up the audio so that your mic works with ALSA and that you can arecord and aplay- DO THIS FIRST Before installing pocketsphinx.

  22. Stephan

    Hey James,

    you just need to start talking, system is ready as soon as it reads “ready”. The “listening” message comes up when the tool recognizes input.

  23. Pingback: voice recognition with CMUS | uncircuitous

  24. Pingback: Den Raspberry Pi mittels Sprache steuern Teil 1 | Raspberry Pi Dokumentation

  25. Pingback: Voice control for LightwaveRF devices | SECURITY EXPO

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>