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.
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.
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:
sudo nano lighton.py
Then type the following lines into the editor:
import RPi.GPIO as GPIO
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
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.
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
Verify the install worked by typing:
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:
npm install connect
npm install serve-static
npm install serialport
npm install rpi-gpio
Step 2 - Install the application server
sudo wget www.privateeyepi.com/downloads/webgpio.zip
sudo unzip webgpio.zip
Check that it works by typing the following command:
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
At the Raspberry Pi command prompt type:
Enter the following text at the end of the this file so it looks something like this:
Press CTRL-X followed by y and ENTER to exit and save
Reboot by typing:
Once rebooted type the following:
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.
I hope you have enjoyed this project. If you are in need of help feel free to email us at firstname.lastname@example.org.