Hello! In this instructable, I'm going to show you how I built my Wi-Fi Light switch using a Raspberry Pi and some basic components. Please note that this is my first instructable and I'm not really good at explaining things, so I will try to answer your questions in the comment section if you don't understand anything.

Step 1: Parts

To build a project, you obviously will need some parts. Here's a list of what I used.

  • Raspberry Pi
  • Cat-5e ethernet cable (you can use a WiFi dongle instead)
  • Jumper wires
  • Strand wires
  • DC motor with gearbox
  • 2-channel relay module
  • Power supply for Raspberry Pi
  • Power supply for DC motor
  • Small metal plate (the size of your light switch)

You will also need some additional tools, like a soldering iron and a glue gun. We will need the DC motor to be powerful enough to flick the light switch on and off, so it needs a gearbox. I didn't connect the relay directly to the lights, because it's too dangerous. Instead, I'm going to use it to power the DC motor instead.

Step 2: Preparation

Prepare your glue gun. We're going to use it to stick the metal plate to the shaft of the DC motor.

Align the metal plate and the DC motor just like in the diagram, and then glue them together. Make sure the connection is strong.

Step 3: Wiring

Next, connect the motor to the relay as shown in the diagram above. We use the 2-channel relay as an H-bridge to turn the DC motor using the power supply. We need 2 relays so that it can rotate the motor in both directions.

Step 4: Mounting

Once the metal plate is glued to the shaft, use the glue gun to stick the motor to the wall, text to the light switch. You can see how I stuck mine to the wall in the video at the beginning, and in the diagram above.

Step 5: Configuring

Now comes the hardest part, configuring and programming your Pi. This requires a lot of configuration, and putting everything in one instructable is illogical, especially when there's already instructions on how to do those. So I will just explain what I did, and if you don't understand anything I'll give links to the complete tutorials and documentation. You will also need a monitor, a keyboard and a mouse to configure the Raspberry Pi.

First, you need to install the Raspbian OS to your Raspberry Pi. (Guide)

Once you Raspberry Pi boots up, you need to connect it to the network, either by Ethernet cables or using a WiFi dongle. Simply plug in the Ethernet cable (RJ-45) to the Raspberry Pi and to the router. Or, if you use a WiFi dongle, you can find the option to connect to a wireless network at the top right corner of the screen.

Step 6: Webserver

Next, we need to install apache webserver on the Raspberry Pi to send it commands, like turn off the lights.

Open a console window (shown in the diagram) and type in the following command:

sudo apt-get install apache2 -y

And then press Enter. Once the installation process is done, you can test the webserver by going to the web browser (in your Raspberry Pi) and navigating to http://localhost/

You should be able to see the default page as shown above. If you don't, please read the official install guide.

Step 7: Programming Pt. 1

In this project, we will be using pin 2 and 3 to control the motor First, let's create a python file for pin control.

Navigate to the apache directory, which is at /var/www/html/ by typing the following command to the console/terminal window:

cd /var/www/html

and create a new file there called "lights.py"

sudo nano lights.py

and then type in the following script:

<p>#!/usr/bin/env python<br>#lightsOn.py
import RPi.GPIO as GPIO #Import Raspberry Pi's GPIO Functions
import sys #Import sys for getting shell arguments
import time #Import time for delays</p><p>#Checks if the user wants to turn the lights on or off
#And then sets the pins accordingly
if(int(sys.argv[1]) == 1):
	pin = 3
	pin2 = 2
	pin = 2
	pin2 = 3</p><p>#Setup the pins
GPIO.setup(2, GPIO.OUT)
GPIO.setup(3, GPIO.OUT)</p><p>#Turns the motor backwards slightly, then forwards.
#This is done so that the motor will get enough momentum to flick the switch.
GPIO.output(pin2, True)</p><p>GPIO.output(pin2, False)
GPIO.output(pin2, True)</p><p>GPIO.output(pin, False)
GPIO.output(pin, True)</p>

Note that comments (the ones starting with a #) is not required, and you don't need to type them, except for the topmost one.

Then, press Ctrl+O and then Enter to save the file, and Ctrl+X to exit out of the editor.

Step 8: Programming Pt. 2

Next, you need to set up a PHP file to receive HTTP requests and run the python script. This script will connect the web interface to the python script, which controls the pins. Type in:

sudo nano lights.php

to the console and press Enter. Type in the following code to the editor:

<p><?php</p><p><?php<br-->$fname = escapeshellcmd('lights.py');</p><p>$pin = escapeshellarg(isset($_REQUEST['on'])?'1':'0');</p><p>$output = shell_exec('sudo python /var/www/html/lights.py ' . $pin);</p><p>?></p>

Then press Ctrl+O and then Enter to save. Ctrl+X to exit.

Step 9: Programming Pt. 3

Next, we need to create the user interface. This is where the buttons will be. First, we need to delete the default index.html file. Type in:

sudo rm index.html

to the console. Then, create a new file called index.php (or index.html)

sudo nano index.php

We need to make the interface. The easiest way is to use links, but I will show you how to make it using buttons and some javascript. Type in this code (http://pastebin.com/3Nzkv4CU). (I had to link it to Pastebin because the Instructables editor won't let me paste HTML code).

Then do the same as the previous steps to save and exit out of the editor.

Step 10: Testing

Now it's time to test the project. Navigate to http://localhost/ in your Raspberry Pi browser. Two buttons should appear, try clicking on one of them to see if the motor moves. If it is reversed, you can either flip the motor wires or edit the code at this part:




and just reverse the on and off. (Change on to off, and off to on).

Step 11: Android App Pt. 1

This part is easier, because there's a tool called MIT App Inventor to help you make Android apps easily.

First, create a new app and place 2 buttons, on and off.

Then, click the "Blocks" button at the top-right corner as shown in the image above. This is the part where we program the Android app.

Step 12: Android App Pt. 2

Before programming this, we need to know the IP address of the Raspberry Pi. Here's a simple guide on how to get it.

Now drag the blocks from the left side to the code area as shown in the diagram above. In this case, my Raspberry Pi's IP address is, but yours may be different.

When you're done, click on the Build option at the top and click on App (save .apk to my computer)

Now, plug in your phone to your computer, and use your favorite file manager to copy the apk file to your phone.

To install it, make sure you have the "Unknown Sources" option checked in the settings. To enable it, go to Settings > Security > Unknown Sources.

Using a file manager in your phone, navigate to the folder where you placed your apk file and open it. Select "package installer" when prompted. Just click on "Install" and open the app.

You should be able to control the motor and flick the light switch using your phone.

Step 13: Done!

By now, you should have a working light switch. If it doesn't work, feel free to ask me on the comments and I will try to help you. I won't be able to answer all of your questions, but I will try to answer as much as possible. I hope this instructable is useful. Also pardon me if my explanations are hard to understand; trying to explain complex technical things in a simple way is not easy.

<p>Dear Sir</p><p>I am trying on the same project but instead f relays, I am using the motor driver L293D. So,the .py code you have given does not work. Also, I do not understand what does pin 2 and pin 3 stand for? Please tell me the changes I'll have to make to make in the code to make the motor work? </p>
I have replied to your PM.
<p>Thanks for sharing your very first project!</p>
<p>You're welcome! It takes patience to document complex projects like this, and I'm happy to see people appreciate my work! :D</p>

About This Instructable




More by HizkiFW:Wi-Fi Light Switch W/ Raspberry Pi 
Add instructable to: