How to Control Power Outlets Wirelessly Using the Raspberry Pi
Sam Kear Hardware, Raspberry Pi
In this blog post you'll find instructions for using a Raspberry Pi to wirelessly control Etekcity power outlets using 433MHz RF.
Ever year during Christmas time I find myself dealing with the hassle of turning the Christmas tree lights on and off. This year I set out to find a solution I could use to automate this task.
While I could have just used one of those cheap light timers instead I wanted something I could control wirelessly using my smart phone. The Belkin WeMo can be controlled over the web but I think they are way too expensive at nearly $40 per outlet!
While searching for alternative options I found a blog post by Tim Leland that explained how to hack the Eteckcity remote controlled outlets using a Raspberry Pi and a cheap $5 wireless transmitter and receiver kit.
This system is significantly cheaper than using WeMo outlets plus it can also easily be scaled to control more outlets. As an added benefit of being controlled through the Pi outlets can be automated using cron jobs, scripts, or anything else you can dream up.
Parts List
Below is a listing of all of the hardware I used for this project. If you plan to use the hard wired ethernet port on the Pi then you won't need the wireless adapter.
The Pi cobbler is also optional but I find it makes wiring much simpler when using a solderless breadboard.
Other Required Items:
Step 1: Load a disk image on the SD card
If you're starting from scratch with a fresh SD card then the first thing you need to do is load a disk image onto the card. I usually load Raspbian since that's what I'm familiar with it any other image should work fine.
If you prefer not to deal with loading a disk image you can also buy a preloaded memory card.
The quickest method I've found to load a disk image using Mac OS X is to use a program called ApplePi-Baker. The program will automatically detect the SD card, unmount it and load an image file of your choice.
It can also quickly setup a card for NOOBS if that is what you prefer to use.
For Windows users Win32DiskImager is a good program to use for loading disk images on memory cards.
Step 2: Download and Build WiringPi
WiringPi is a prerequisite package required by RFSniffer and codesend.
The source files for the software can be pulled using git.
git clone git://git.drogon.net/wiringPi
Execute the build script to compile the code.
cd wiringPi ./build
To confirm that the build process was a success you can issue the following command.
gpio -v
You should see something similar to the output below. image3
Finally you can issue the command below to confirm that wiringPi can read from the GPIO pins.
gpio readall
If successful you'll see a listing for all of the gpio pins and their values like the one below.
Step 3: Install Apache and PHP
The Apache web server is used to serve the toggle.php page which will provide a web interface for controlling the wireless outlets.
Use the command below to install Apache with the php modules.
sudo apt-get install apache2 php5 libapache2-mod-php5 -y
To confirm that Apache is working put the IP address of your Pi into your web browser, you should see the default Apache test page.
Step 4 - Solder an Antenna Wire to the Transmitter
Without an antenna the transmitters range is extremely limited, mine wasn't able to reach outside the room without one. I took Tim's recommendation and used a 12″ piece of wire from the inside of a cat 5 ethernet cable.
Solder the wire to the antenna pad on the upper right corner of the transmitter PCB.
Step 5: Connect the RF Transmitter and Receiver Modules to the Pi
Using the solderless breadboard and the jumper wires connect the transmitter and receiver modules to the Pi as listed below.
The silk screen labels on the PCB for each module should match what is listed below. If it doesn't match you may have a different revision with a different pinout, in this case you may need to adjust the connections to match your specific modules.
The transmitter module is the smaller module which has 3 pins, VCC, data, and ground.
The receiver is the larger of the two modules with 4 pins, VCC, 2 data pins, and ground. Only one of the data pins on the receiver is used for this project.
Transmitter Module
Receiver Module
If you are using a solderless breadboard and a Pi breakout cable your setup will probably look something like mine below.
Step 6: Use RFSniffer to Find the Outlet Control Codes
In this step you'll use a program called RFSniffer and the 433MHz wireless receiver to read the on and off codes for each pair of buttons.
First pull down a copy of Tim's rfoutlet code from GitHub. Alternatively you can download the source for 433Utils and compile the code yourself.
git clone git://github.com/timleland/rfoutlet.git /var/www/rfoutlet
Set the appropriate ownership and permissions on the codesend executable.
sudo chown root.root /var/www/rfoutlet/codesend sudo chmod 4755 /var/www/rfoutlet/codesend
To sniff the codes run the RFSniffer program.
sudo /var/www/rfoutlet/RFSniffer
You won't see any output on the console when you run the program but if everything is connected properly you should see output after pressing some of the buttons on your remote.
Each press of a button on the remote should produce something like this:
Received 21811 Received pulse 192
What you're looking for is the longer number, not the short 3 digit pulse. In the example above 21811 would be the code you're looking for. Record the code for each of the on and off buttons on the remote.
image9 Here are a few of my notes about reading the codes:
Step 7: Update toggle.php with the Codes for Your Remote
Using your preferred text editor edit /var/www/rfoutlet/toggle.php with the codes you recorded in step 5. You will also need to update $rfPath to point to the correct path which in my case was /var/www/rfoutlet/codesend.
Testing the Web Outlet Controls
At this point everything should be done and ready to be tested. The php based web control page can be accessed by visiting the http://<your-pi-ip>/rfoutlet in your browser.
The on and off buttons on the page should function just as they would on your physical remote.
Troubleshooting
If the buttons on the web page don't work then try manually sending a code using the command line.
root@raspberrypi:/home/pi# /var/www/rfoutlet/codesend 21820 sending code[21820]
If sending a code manually works then the transmitter is functioning but there is an issue related to the web setup. Make sure that you made the correct modifications to toggle.php, specifically the $rfPath variable is pointing to the correct path.
You can also check the apache server logs to see if there is a syntax error in the toggle.php file.
tail -100 /var/log/apache2/error.log
If the manual code send isn't working then check to make sure the transmitter is wired properly. You can also connect an LED to the data pin of the transmitter to confirm that it is receiving output from the Pi, it should blink when you send a code.
Additional Resources
In addition to Tim’s blog article Ninja Blocks has a very useful article about adding 433Mhz RF to your Pi. Ninja Blocks also maintains the 433Utils code on github which may be helpful as well.