“Clap on! Clap off! The Clapper!”
This project will walk you through the creation of a DIY “Clapper”. It uses the Adafruit MAX4466 Microphone Amp to detect when the claps occur, then sends an RF signal to RF-controlled power plugs.
The script could probably be modified to use a standard USB microphone, which would simplify the wiring significantly. I may revisit that later.
Parts:
- Raspberry Pi Zero (with header pins)
Any Raspberry Pi [2, 3, Zero, Zero W] should work, just make sure it has GPIO header pins unless you’re comfortable soldering directly to your Pi. The Pi Zero and Zero W do not come with header pins standard (but the 2 and 3 do).
See this for info about adding headers to the Pi Zero. - Adafruit MAX4466 Electret Microphone Amplifier
- Adafruit MCP3008 SPI ADC – Analog-to-digital converter
- Etekcity Wireless Outlet Switch – RF-controlled power plugs
- RF Transmitter / Receiver Kit – Transmitter is used to control the power sockets. Receiver is used to learn the signal from the remote control.
- Breadboard, jumper wires, etc…
Wiring Diagram:
Python Code:
Putting it all together
- First, wire everything up the way I’ve illustrated in the wiring diagram above
- Get Raspbian Jessie (I used the Lite edition, but I presume the full version is fine) and load it on an SD Card
- Once in Raspbian, I recommend doing the following commands to get everything updated (a good practice in general on most new installations of Raspbian):
- sudo apt-get update
- sudo apt-get dist-upgrade --yes
- sudo raspi-config – Configure your localization settings here. You will also need to enable SPI in Interfacing Options (older versions had it under Advanced).
- sudo reboot – when it boots back up, log back in
- Install Git, the Python dev libs, and pip (Python package manager):
sudo apt-get install git python-dev python-pip - Install spidev (SPI Python libs): sudo pip install spidev
- Install WiringPi:
- Clone the wiringPi repository into a directory (like “src” subdir under your home, example: mkdir ~/src; cd ~/src ):
git clone git://git.drogon.net/wiringPi - cd wiringPi
- git pull origin
- ./build
- Instructions and details were stolen from Gordon Henderson’s blog.
- Clone the wiringPi repository into a directory (like “src” subdir under your home, example: mkdir ~/src; cd ~/src ):
- Install Tim Leland’s web-manager code (which includes a modified version of 433Utils for using the RF hardware). Note, Tim’s code is a part of a web-based project that he maintains. I’m not going into details on setting up the web server, but if you would like to have the option of controlling the plugs from a web app, check out Tim’s excellent blog post
- sudo git clone https://github.com/timleland/rfoutlet.git /var/www/rfoutlet
- Figure out what RF codes are used by the power outlet remote control and receiver:
- Run: sudo /var/www/rfoutlet/RFSniffer
- Press each button on the remote control and take note of the 6-digit code as well as the pulse that shows on the screen – for both ON and OFF.
- Note, you may need to hold the remote control RIGHT next to the receiver on your breadboard – nearly touching, it doesn’t have much range.
- Create the python script that will control the clapper and ties everything together.There are several ways you can accomplish this:
- Download it straight to the Pi with the command:
wget -O ~/clapper.py https://gist.githubusercontent.com/aplocher/e83fdc72e705c099936cc187cee74768/raw
(NOTE #1 – that’s an uppercase -O, NOTE #2 – if you’re typing this out by hand, there’s a tinyurl you can use instead: http://tinyurl.com/bc-clapper) - Or, edit a new file and paste the contents into that file:
- nano ~/clapper.py
- Copy the script (above) and paste it into your terminal window
- Press CTRL+o to SAVE, and hit ENTER
- Press CTRL+x to EXIT
- Download it straight to the Pi with the command:
- Configure the Python script if necessary. Lines 20 – 40 can be tweaked to adjust the RF codes, the path to the codesend bin, and sensitivity of the clap detection.
- nano ~/clapper.py
- Change any necessary config variables between lines 20 and 40
- When done, press CTRL+o to SAVE, and hit ENTER
- Press CTRL+x to EXIT
- Test it by running:
- python ~/clapper.py -h – should show help info
- python ~/clapper.py --on – should activate the power (and exit)
- python ~/clapper.py --off – should deactivate the power (and exit)
- python ~/clapper.py – should listen for claps to toggle power on and off (will remain running)