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!
- Raspberry Pi
- A lamp (max 220V)
- Multiple Adapter (powered) for convenience and not to cut the cable of the lamp
- Electrical derivation box It is very important to insulate the relay
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
CONNECT THE CABLES INTO THE ELECTRICAL DERIVATION BOX AND TO THE RELAY
CONNECT THE RELAY WITH YOUR RASPBERRY PI ACCORDING TO THIS SCHEME
CONNECT THE LAMP WITH THE MULTIPLE ADAPTER
CONNECT THE MULTIPLE ADAPTER TO THE ELECTRIC CURRENT
Now power on your Raspberry Pi, update and upgrade it
Install these two packages
Create a new directory
Enter the directory
and download these scripts
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
To power off the lamp run
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
Edit sudoers to get the permission to run scripts from web
And add to bottom of the file this string
Restart apache service
Now remove the default index
And insert this one
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:
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!
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:
Edit the motion config file
Edit this file
Now you can start the daemon
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!
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
You can download the script from here.