Introduction: Using a Sound Sensor With a Raspberry Pi to Control Your Philips Hue Lights
Use case: once my kitchen door is closed I'm not able to hear my doorbell - annoying when you throw a party with many guests!
Luckily, one guy at the partycame up with the excellent idea to "hook up some kind of microphone and ring a sound if the doorell rings".
The idea seemed nice, so we brainstormed the possiblities over a couple of beers with my tech-savy friends.
It hat to be a bit more flashy than just a doorbell sound, so instead of using my speakers we decided to use my Philips Hue lights instead to create a cool light effect to announce new guests!
All of the lights were set up in the kitchen already, so we went for a blink the lights if the doorbell rings approach.
Step 1: Hardware Requirements
- a Raspberry Pi 2 to run the software / scripts (every Raspberry generation should be feasible to implement this)
- a cheap sound sensor for a couple of bucks from ebay/amazon/etc - e.g. here or here
- some female-to-femalejumper wires to connect the sound sensor with the Pi
- Philipps Hue lights, you can go for every set up you want - I went for
- Philips Friends of hue - LivingColors Bloom
- Philips Hue Go
- Philips hue - LED
- one of the above needs to be the starter kit / you will need a bridge to control the lights after all
Step 2: Software Requirements
Step 3: Initial Setup
This will outline the main steps of the set up, as you can see it's pretty simple and should be straight-forward to understand.
- Connect the sound sensor to the Raspberry Pi via female-to-female jumpers, using 3 jumpers for:
- power on your Raspberry Pi
- If your sound sensor has indicator LEDs, make sure it triggers when creating noise to test the basic sound sensor capabilities
- install Raspbian image (if not done already)
- setup up and connect via ssh
- install necessary software (python-dev) via
apt-get install python-dev
- and install the python libraries requests, qhue and RPI.GPIO via
pip install requests
git clone https://github.com/quentinsf/qhue/
pip install RPI.GPIO
Step 4: Set Up the Script
Let's open a new script named sensor.py and put in the below code.
import RPi.GPIO as GPIO from qhue import Bridge
GPIO.setmode(GPIO.BOARD) # use board pin numbers # define pin #7 as input pin pin = 7 GPIO.setup(pin, GPIO.IN)
b = Bridge("192.168.1.30", 'e254339152304b714add57d14a8fdbb') groups = b.groups # as groups are handy, I will contorll all
while 1: if GPIO.input(pin) == GPIO.LOW: i = 3 # number of iterations for l in range(1,i+1): # this is one of the temporary effects, see official docs # at http://www.developers.meethue.com/documentation/core-concepts b.groups.action(alert="select") #group 0 = all lights time.sleep(1) time.sleep(10)
Step 5: Fire It Up!
Just run the script via
sudo python sensor.py
And trigger the sound input - if all went well, your lights should blink 3 times..
Congratulations, you just created a listener script to listen to your doorbell / any sound you wish!
Step 6: Set Up Autostart for Your Listener Script
We will be utilizing the Linux rc.local functionality and create a new shell script that will run the python part we just created in the previous step:
#!/bin/sh<br># sensor.sh sudo python /home/pi/qhue/sensor.py
Now make this script executable by performing:
chmod +x sensor.sh
Open up the /etc/rc.local file
and enter the following line before exit 0 to run the script at startup
Save the file and reboot your Raspberry Pi via
sudo shutdown -r now
Step 7: Summary / To-Do
This instructable outlines the very basics, I will add a few pictures and more details once I got the time to fine-tune this.
It is worth noticing that the distance to your sound source shouldn't be too far away, neither should you place the Pi in a spot where you would expect regular noise.
Hope you enjoyed the guide, any suggestions? Feel free to comment ;)
We have a be nice policy.
Please be positive and constructive.