Make an Internet Controlled Lamp with a Raspberry Pi and Flask
Here I will be showing you how to turn on and off a lamp from anywhere in the world. However, you can control any device that works by toggling its power source, such as a fountain, TV, Christmas tree lights, projector, etc.
- Raspberry Pi
- Internet Connection (Ethernet or WiFi)
- Device Under Control (Lamp in my case)
- Remote Controlled Outlets (I used these.*)
- 6 2N2222A Transisitors (Here’s 100 of them*)
- Jumper wires and breadboard
- Soldering Iron
* affiliate links
Our goal it to hack into the remote that controls the outlet, allowing us to toggle the buttons with the Raspberry Pi’s GPIO pins. Check out my page on hardware control for more information about GPIO pins. We will then set up a simple webserver with Flask to allow remote control of the outlets from anywhere with a web connection.
Hacking into the Remote
To open up the remote I first had to remove the battery cover and undo one screw. Once this was done I was able to pry the two covers apart using a flat head screwdriver. With the plastic gone, you can see the simple through-hole circuit board. See below for a picture of the front and back. (Ignore the wires for now, I forgot to take a picture before I soldered them on.)
As you can see in the first picture, there are two rows of buttons – an on and off button for each of the three outlets. Our goal is to control those buttons with the Raspberry Pi. We can accomplish this with a circuit element known as a transistor. Basically, a transistor is just a switch that can be controlled with a computer, which is exactly what we need.
The specific transistor I will be using here is the infamous 2N222A. All 2N222A transistors have three pins. Two of the pins are inserted in series into your circuit, and the third pin controls if the transistor conducts current (closed switch) or does not conduct current (open switch). To close the switch and allow current to flow, this third control pin needs to be raised high. To open the circuit and stop current, the control pin should be set low.
The collector is where current should flow in and the emitter is where current should flow out. The base pin is the control pin I mentioned above, which is used to control the state of the transistor (conducting or not conducting). The arrow drawn on the symbol indicates the direction of current flow. You can read more about the specifics of the 2N222A in the datasheet (PDF).
To identify which pin is which, hold the transistor with the flat side facing you. The left pin is the emitter, the middle pin is the base, and the right pin is the collector.
Our goal is to use the transistor to bridge the same gap as the buttons on the remote, so that applying a voltage to the base pin on the transistor will close the circuit just as pressing the button would. In this way we are able to simulate a button press without knowing anything at all about the circuit the buttons are a part of.
Time to Solder Things
The first step is to solder wires to each side of the buttons. Here I am only going to connect up two of the buttons (on and off for outlet 1) but you can follow the pattern and connect up all six. In the picture below I’ve labeled the three places you will need to solder cabled to.
Solder wires to the positions indicated above so it is easier to connect this remote to a breadboard. Once this is done connect the wires across a transistor using your breadboard. See below for a circuit diagram
While not captured in this photo, you will also need to solder a wire to the ground terminal and connect that to the ground of the Raspberry Pi.
For more detail: Make an Internet Controlled Lamp with a Raspberry Pi and Flask