Introduction: How to Control Outlets Using a Raspberry Pi

About: I enjoy DIY projects, especially those involving woodworking. I'm an avid computer programmer, computer animator, and electronics enthusiast.

In this Instructable, I will demonstrate to you how to create a web interface to control outlets in your home using a Raspberry Pi. I chose to write this project when I saw the Sensors contest, and since this project involves using a sensor to read codes off of the remote provided with the outlets you purchase, I thought this project would be the perfect fit.

I will work to keep this as high level as possible and provide code to make this all work. For all of you code monkeys out there, feel free to dig around in the code, and scrutinize me in the comments! I'm always looking to improve. There's quite a bit going on in this, so if you have questions on any step, please leave a comment so I can update the Instructable with the needed details.

This project uses radio frequency controlled outlets at 433MHz coupled with an RF transmitter connected to your raspberry pi. The outlets come with remotes that can turn on/off outlets, but that's no fun! We will instead use a web server running on the raspberry pi that allows anyone connected to the network to turn on or off certain outlets. A web front-end to this project is ideal since any web-connected device on your network can be used as your remote.

This is a project I've dreamed of completing for years, and although there is still room for additions, the project described as follows is a functional system.


Step 1: Set Up Your Raspberry Pi

Start off by installing the latest version of Raspbian on your new Raspberry Pi. If you plan to use an existing Pi (which you can totally do), all you need to is to make sure you have Python 3 on your Pi, since that is what all of the source code I provide is written in. To do this, type python3 into the command line. If you have Python 3, you should see the python terminal open. Exit the terminal by closing out or typing exit().

Once you have confirmed this, you need to install flask onto your Pi. Flask allows us to create our web server. From the Pi command line, type sudo pip3 install flask.

In the event you don't have pip installed on your Pi, you will need to type sudo apt-get install python3-pip.

I prefer to develop on my Raspberry Pi from the comfort of my desktop computer. If you too prefer this option, you need to install some sort of environment to SSH into the Pi. PuTTY will work, but I prefer to have a GUI (graphical user interface), which makes it seem like you're directly on the Pi. For the GUI option, you will want to install VNC Viewer onto the computer you plan to be working on. There are also settings you will need to adjust on your Pi. Instructions for modifying settings can be found here.

Step 2: Hooking Up Your Hardware

At this point, it is time to make all of the physical connections you will need to do in preparation for setting up the source code for this project. You will be connecting the pins of the transmitter and the receiver to the GPIO pins of your Pi. You will want to religiously reference the pinout diagram of the GPIO pins. To do so, open the terminal on your Pi and type pinout.

The following are the connections I made. You may have a different configuration if you purchased different transmitter/receiver hardware than I indicated in the introduction. If you decide to choose different pin numbers than the ones I indicate below, have no fear! When you download the source code, simply modify the pin numbers as needed in the file.

Receiver (big chip):

  • GND - Any designated ground pin
  • DATA - Pin 11
  • DER - Not connected
  • +5V - Any designated +5V pin
  • ------
  • +5V - Any designated +5V pin
  • GND - Any designated ground pin
  • GND - Not connected
  • ANT - Not connected

Note: You may connected an antenna as needed, but I found I didn't need to. This little chip was surprisingly sensitive and faithfully detects signals out to at least 50ft from where it was sitting and through at least two walls.


  • P - The 3.3V pin
  • DA - Pin 7
  • G - Any designated ground pin
  • AN - not connected

Note: Again, you may connect an antenna to the transmitter as needed, but I found I did not need to. The range is more than sufficient (50+ft).

Step 3: Adding the Code!

This is the part on which I spent most of my time when I was developing this project. I try to comment as much as possible, but I likely left gaps that may require explanation if any of you spend time trying to understand it. If this happens to you, please leave a comment!

The languages that are involved here are:

  • Python
  • CSS
  • Javascript
  • HTML
  • JQuery/Ajax

The Python is used in the flask server and in the file, which handles the code transmission and receipt. CSS is used in generating the style of the webpage. Who wants a boring website!? Javascript is used in any of the event handling (pushing a button, etc.). HTML is the core building block of the webpage. Finally, JQuery/Ajax is used to communicate between the webpage and the Python backend.

If all of these languages sound intimidating, don't worry! I only knew Python going into this project, which happened to be the language that involved the least amount of coding, go figure... Suffice it to say, take your time combing through the code if that's what you choose to do. If you don't want to, you don't have to!

The following is the directory structure I chose to employ with this webpage. The zipped folder attached to this step contains all of the source code in the following structure.

Outlet Switch
-> comm
-> webpage
----> static
-------> favicon.ico
-------> style.css
----> templates
-------> index.html
-------> schedule.html
----> data.file

You can put the Outlet Switch folder anywhere on your Pi. When you're ready to run your web server, open the terminal on your Pi, and cd (change directory) to the webpage directory. Then type python3 into the terminal. If there weren't any errors, you should be good to go!

P.S. Please be patient if you run into any bugs. I tried to squash all of them before uploading.

Step 4: Give It a Shot!

Now that you're running the code, you can get a feel for the web interface at your fingertips. The system's control is very simple:

  • You can add as many outlets as you want using the web interface.
  • Deletion is as simple as clicking edit, selecting the outlets you want to delete, and clicking delete.

The above YouTube video demonstrates how to actually go about adding a new outlet to your list. To summarize what it shows:

  • First enter the title of outlet in the Title field
  • Click or touch outside the Title field to allow the program to check that the title does not already exist. You cannot have titles by the same name
  • Once validated, the ON Code and OFF Code buttons are enabled.
  • Press and hold the ON button on your provided remote and quickly click the ON Code button on the interface. Hold the button on the remote until your binary code populates the adjacent field.
  • Repeat the prior step for the OFF Code.
  • Click OK, and now you're ready to switch this outlet!

As mentioned in the introduction, the project is not 100% complete for my own uses. The biggest component I have yet to complete and implement is the scheduling capability. I plan to allow the user to build a schedule which would allow outlets to be turned on and off at designated times automatically.

Step 5: Debugging and the File

The only known limitation of the system is that there's a chance of incompatibility amongst different vendor's outlets. The on and off code messages are 25 bits for my system, and if a system has a different message length, it is not immediately compatible with this project. If you exactly followed the instructions and you're still having issues getting this to work, the following would be the best way to debug the problem.

  • Ensure you have matplotlib installed on your Pi. You can do this by typing the following into the Pi's terminal:
    • python3
    • import matplotlib
    • If you receive an error, then you need to install matplotlib.
    • Exit the python shell using exit()
  • To install matplotlib, type sudo pip3 install matplotlib into the terminal
  • Download the attached file, and place it alongside the file on your Pi
  • Open the python terminal and change directory into the comm folder.
  • Type python3
  • When it says "**Started Recording**," press and hold your ON or OFF button on your remote until the recording ends.
  • The program will load up a matplotlib figure that looks similar to the above. Zoom in until you see the repetitive signal as demonstrated in the photos above.
  • Count the total logical 1 and logical 0 for each message, where a message is one iteration of the repetitive signal seen above. If the total is 25, the file is likely not at fault. If you encounter something other than 25, make sure to comment below, and I will retool the file to be more open-ended (or feel free to try your hand at it).

What if I don't see a repetitive signal?

If you don't see a repetitive signal, one of two things is wrong. First, check that all of your jumper wire connections are properly made. If everything looks good, you may have a bad receiver. Did you buy the one I suggested? When I bought my first receiver chip, it was very noisy. So noisy I couldn't get a clear signal. I then returned that chip and picked up the one I linked, and it couldn't have worked better.


Step 6: Enjoy!

You know have the ability to switch on/off outlets from your phone, tablet, computer, or other web-enabled device! Let me know what you think in the comments!