Home‎ > ‎

On/Off Project - Switch a light on/off using your smart phone

Project Description

In this project I will show you how to switch a circuit on and off using an internet browser, RaspberryPi minicomputer and a relay switch. 

Warning: Use extreme caution working with high voltages. This project will work for switching both DC and AC current. Using it to switch low voltage DC current is safe, however if you need to hack into a high voltage appliance please exercise extreme caution and preferably consult a professional to make the high voltage connections.

How it works

You will create lightweight web server and java based application server (called Node) to interface with the GPIO ports on the Raspberry Pi, as per Figure 1. The Node application server will process the on/off requests coming from the web browser and switch the desired GPIO pin on and off. A relay circuit that is driven by the GPIO pin state (0 or 1) will switch an external electronic circuit.

Figure 1 - How it works

What you need
Raspberry Pi, SD card with Raspbian OS, network connectivity to the internet
Preferably run the Raspberry Pi headless and use Putty to log into the Rpi and FileZilla to copy files to the Rpi. This way you can put the Raspberry Pi close to the appliance you want to switch. Alternatively run two long wires from the appliance to the Raspberry Pi.
You do not need any programming experience, we will provide all the code
You do not need a PrivateEyePI account, as this project runs everything off the Pi and is not dependent on our PrivateEyePi server
Basic soldering skills to build the relay circuit
We recommend you use this relay circuit, however you can build your own relay and source your own parts.

Build instructions

1. There are two separate circuits depicted in figure 2. The first circuit is the power and on/off signal to the relay switch (shown by PWR, IN and GND bottom right of figure 2). This circuit provides power to the relay coil and a signal (IN) that switches the coil on/off. The second circuit is the circuit that powers the lamp. By making a break in the GND (-VE) and attaching the two ends to the relay you allow the current to flow through the relay when it is open and a break in the circuit when it is closed. 

Warning: I would highly recommend first building a low voltage LED circuit to switch on and off. If you do hack into a high voltage appliance then exercise extreme caution. If you do not have high voltage experience and qualification then consult an electrician to do this part of the project for you.

If you have built your own relay switch make sure that the power rating of the relay input (IN) allows for 3V and the power of the coil is 3-5V and importantly does not exceed the maximum 1 Amp current rating.

Figure 2 - Circuit diagram for a relay switch controlled by a Raspberry Pi*

* The diagram shows the relay wired to the 3V Raspberry Pi Pin. Some relays require 5V (which is pin 2). 

2. Lastly test the relay by building a simple Python program. If you have never written a Python program that uses the GPIO ports then follow these instructions on how to install the GPIO library for Python.

Navigate to the Raspberry Pi command prompt and enter the following commands:


cd /home

sudo nano lighton.py

Then type the following lines into the editor:


import RPi.GPIO as GPIO
GPIO.setup(12, GPIO.OUT)

Press CTRL-x y ENTER to save and exit

Run the program by typing:


sudo python lighton.py

The light should go on. If you want to switch the light off then change the following line in the program:





Now that your relay switch is working you can move on to the software section of the project where you will install the necessary software required to control the GPIO pins from a web application. We've written a simple web application for you (as show in figure 1) that you can download an modify if you want. We will work out of the /home directory but you can choose a different directly if you want.

Tip: It's easier to install and run this software using the root user. It is possible to do it with the "pi" user but you will run into permission problems which makes this install complex. If you haven't used the root user before you can easily create it using the following command:

sudo passwd root

It will prompt you for a password. Reboot and login using the root user.

Step 1 - Install node.js

We will use some JavaScript to control the GPIO pins. JavaScript was originally written to run in a browser so we need to install an application that allows you to run it from the command line of the Rpi. The application we will use is called Node which is an application platform that is especially good at performing server side functions like controlling the GPIO pins and running a web server. 

Navigate to the command prompt of your Rpi and type the following commands:

Tip: Its easier to copy and paste these commands into a putty session window than to type them in. You can paste into a putty session window by clicking the right button on the mouse.


cd /home
sudo wget http://nodejs.org/dist/v0.10.12/node-v0.10.12-linux-arm-pi.tar.gz  
sudo tar xvfz node-v0.10.12-linux-arm-pi.tar.gz
sudo mv node-v0.10.12-linux-arm-pi /opt/node/
echo 'export PATH="$PATH:/opt/node/bin"' >> ~/.bashrc
source ~/.bashrc

Verify the install worked by typing:


node -v

It should respond with the Node version number.

You will need a couple other Node modules so you will use the Node Package Manager npm to do that. 

The first module you will install is called Connect. Connect allows Node to run like simple web server. The web server will process the on/off requests you will send from your smart phone or browser. Type the following command to install Connect:


cd /home

npm install connect

npm install serve-static

npm install serialport

In order to control the GPIO pins from Javascript you need to install the following module:


npm install rpi-gpio

Step 2 - Install the application server

The application server will process the requests from the smartphone or browser and switch the GPIO pin on/off. You installed the Node web server application framework in step 1. In this step you will download the JavaScript application server. Type the following command to download the application server code:


cd /home

sudo wget www.privateeyepi.com/downloads/webgpio.zip

sudo unzip webgpio.zip

Check that it works by typing the following command:


node webgpio.js

The application should start and print the following message to the window:

Server running at http://youripaddress:8000/

That's it! Now you can move to a web browser on another PC or smart phone and navigate to the IP Address of your Raspberry Pi. We have set the server to run at port 8000 so you will type the following address into the browser:


Where youripaddress is replaced with the IP Address of your Raspberry Pi. 

The screen should display a web page with an on and off button. 

Automating a node.js script on Raspberry Pi

Now that you have your node javascript application up and running you may want to make sure it is always running on your Pi. The next steps will show you how to create a service from a node.js application and run it automatically run it in the background of your Raspberry Pi. 

At the Raspberry Pi command prompt type:

apt-get install systemd
sudo nano /boot/cmdline.txt

Enter the following text at the end of the this file so it looks something like this:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait init=/bin/systemd

Press CTRL-X followed by y and ENTER to exit and save

Reboot by typing:
sudo reboot 

Once rebooted type the following:

cd /home
cp webgpio.service /etc/systemd/system
systemctl start webgpio.service
systemctl daemon-reload

Note that we have configured this service to use the root user. If you are using a different user then edit webgpio.service and change the root user to your user.  Every time you edit the webgpio.service file you must execute the above lines to copy it over and reload it. 

Now it should be running as a background service. Check it is working by going to your browser and reloading the URL:


If it is not working then you can check the status of the service by typing:
systemctl status webgpio.service

When it is working it should return something like below. If its not working then you will see the error message displayed that will give you a clue as to what is wrong. 

root@raspberrypi:/home# systemctl status webgpio.service
          Loaded: loaded (/etc/systemd/system/webgpio.service; enabled)
          Active: active (running) since Wed, 24 Jun 2015 22:43:14 -0400; 27min ago
        Main PID: 2509 (node)
          CGroup: name=systemd:/system/webgpio.service
                  └ 2509 /opt/node/bin/node /home/webgpio.js

Jun 24 22:43:16 raspberrypi node-webgpio[2509]: Server running at http://19...

I hope you have enjoyed this project. If you are in need of help feel free to email us at support@privateeyepi.com. 

Please support us by donating or purchasing your project parts from our store.

See also: