Are you passionate about home automation? If yes, this article is for you! We are going to explain how to use a relay to switch on and off a lamp (max 220V) via web through a Multiple Adapter (powered) and your Pi. We will guide you step by step, so take your Raspberry Pi and start reading!

NECESSARY

EXPLANATION

In this article we are going to explain how to use a relay to switch on and off a lamp (max 220V) through a Multiple Adapter (powered). We have used a multiple adapter for convenience and not to cut the cable of the lamp, but note well that you can't the same connect more than 220 V or you are going to "fry" the relay module. We will guide you step by step!

CUT THE CABLES OF THE MULTIPLE ADAPTER

image

CONNECT THE CABLES INTO THE ELECTRICAL DERIVATION BOX AND TO THE RELAY

Note well: Perform operations without connecting the strip to the electric current.

image

CONNECT THE RELAY WITH YOUR RASPBERRY PI ACCORDING TO THIS SCHEME

image

CONNECT THE LAMP WITH THE MULTIPLE ADAPTER

Note well: The lamp must be maximum 220 V or you are going to "fry" the relay module. Close the electrical derivation box and connect the lamp into the multiple adapter.

image

CONNECT THE MULTIPLE ADAPTER TO THE ELECTRIC CURRENT

Now you have to connect the multiple adapter to the electric current, but (just to be sure) keep the switch of the multiple adapter off

image

CODE

Now power on your Raspberry Pi, update and upgrade it

$sudo apt-get update && sudo apt-get upgrade

Install these two packages

$sudo apt-get install python-dev python-rpi.gpio

Create a new directory

$mkdir relay

Enter the directory

$cd relay

and download these scripts

$wget http://www.cmprogrammers.com/script/on.py
$wget http://www.cmprogrammers.com/script/off.py

The first script will turn on our lamp, the second one will turn it off. Once you have done, you can turn on the switch of the multiple adapter.

MAKE IT WORK

Now you can run the scripts, to power on the lamp run

$sudo python on.py

To power off the lamp run

$sudo python off.py

WEB INTERFACE

We have also developed a web interface. This interface have 3 functions:

  • Turn on and off the lamp from web
  • Watch real time streaming througt motion
  • Coming soon...

IMPORT THE WEB INTERFACE

First to all, you have to install these packages

$sudo apt-get install apache2 php5 libapache2-mod-php5

Edit sudoers to get the permission to run scripts from web

$sudo nano /etc/sudoers

And add to bottom of the file this string

www-data ALL=(root) NOPASSWD:ALL

Restart apache service

$sudo service apache2 restart

Now remove the default index

$sudo rm /var/www/index.html

And insert this one

index.php

Just copy the source of this page. Note well: php code is included. Once you have done, open a browser and go to the ip of your Raspberry, for example 192.168.1.25. You will see this page:

image

TURN ON AND OFF THE LAMP FROM WEB

If you press on and off buttons, they will work and turn on and off the lamp! Note well: The scripts (on.py and off.py) must be in the same directory (/home/pi/relay).

MOTION STREAMING

To watch a real time streaming on the web page we need a webcam. Connect the webcam to your Pi, according to these models. Then install motion:

$sudo apt-get install motion

Edit the motion config file

$sudo nano /etc/motion/motion.conf

and change

DAEMON = OFF (change to ON) Webcam_localhost = ON (Change to OFF)

Edit this file

$sudo nano /etc/default/motion

and change

start_motion_daemon = no (change to yes)

Now you can start the daemon

$sudo service motion start

and check if it works, go to the ip of your Pi (port 8081), for example: 192.168.1.25:8081. Then go to the index page and enable the streaming from the dedicated button, you will see the stream working!

PIR SENSOR

If you have a pir sensors you can connect it on your Pi and switch on and off the light through the sensor. We have already written a tutorial on pir sensor, have a look here: Raspberry Pi Alarm

We can easily implement a new script

image

You can download the script from here.

Have fun!