loading

Hey guys, first of all I'd like to introduce myself to everyone. My name is Jack, commonly known as Jackk or JackkTutorials over on YouTube where I do technology tutorials but mainly cover Hacking & Security and other useful things that you wouldn't normally know about.

For this competition I decided I would make a completely universal Home Automation control panel using a Raspberry Pi 2, Relay Channels and some other cheap products. This is a very cheap home automation system!

For my example, the home automation controls my Desk LED's, Bedside Lamp and Lava Lamp. It also displays the current time and current weather forecast for my area (Which I chose to remove in the picture because it shows my exact location). It can also switch my Media Center PC and Workstation PC on using a Wake on Lan feature. The camera section will also redirect to a live IP Camera feed from my old HTC One M7 with the IP Camera application on which runs 24/7. Under the miscellaneous sections was supposed to be an Alarm Clock which I could set a time for all the lights to turn on and an alarm sound to play through speakers, that'll be sure to wake me up! But I never got round to implementing that part - but there's no reason why it wouldn't work!

I'd like to mention that this project isn't for the faint of heart and some basic knowledge electronics will help you such a Soldering, Wire Striping, GPIO and Relay Switches; however everything will be explained. Also remember that messing around with electricity that comes directly from your wall socket is very dangerous and can result in a serious injury so please be careful!

Also basic knowledge of Web Servers, Python, PHP and GPIO will help you out a lot!

Finally, I will only be showing you guys how to make one lamp turn on and off from a Web GUI. As everything else if very easy to setup. Such as the World Clock being a HTML Widget and the IP Camera being a hyperlink to a IP Server.

Anyway, lets get started!

Step 1: Tools We Will Need

Time to talk about what we are going to need. For everyone's convenience I have made links to both Amazon/eBAY UK and Amazon/eBAY US to help out my International readers. So below is a parts list with links to all the products.

Important Components

Raspberry Pi Model 2 - I'll admit, you could use a Model B or B+ (Or maybe even A) but I'd just go all out and buy the best one on the market. For £30/$41 you have a tiny power house that you can use for this project and many more!

Amazon.co.uk - Link

Amazon.com - Link

Make sure you also pick up the SD Card (MicroSD), Power Supply and a case (Case not nessessory but will keep the Raspberry Pi safer!). Also the Raspberry Pi will need an internet connection for this project as the whole user interface is web-powered!

Relay Channel Module Boards - These relay switches are used only for the lights (In my example). They are what are going to be capable of switching things on or off from our Home Automation system. Now these come as modules that have so many relays. I personally bought an 8 Channel relay board for other future projects however you can get 1/2/4/8/16 so just pick which one you want. An 8 Channel relay board cost me £5.64 or $8.38 (Cheaper from China but longer shipping time!)

eBAY.co.uk - Link

eBAY.com - Link

If you want to buy a different size one, for example 2 channel or 4 channel just search "x channel relay module board" x being the number of channels you want. Each channel can control one function.

Raspberry Pi Jumper Cables - These are used to connect the Raspberry Pi to the Relay Module Board so we can communicate with it. They are very cheap, very reusable and come in a variety of different colours! They cost around £4 or $5 for around 40 of them. It also means we don't need to solder!

eBAY.co.uk - Link

eBAY.com - Link

Additional Tools you may already have!

Now for tools most of us will already have lying around. I won't bother posting purchase pages for these just because most of us have these tools at hand!

Philips/Crosshead Screwdriver (PH0/PH1 works best)

Flat Head Screwdriver

Wire Snippers

Wire Strippers

Craft Knife/Stanley Knife/Any Sharp Knife

Soldering Iron/Cable Crimps/Cable Blocks

Cable Flex (For extending our original wires to our lamps, ideal to keep everything in it's original place, most lamps use 2 core flex)

That should be all the tools we need for this project!

Step 2: Software

I ain't kidding when I say this is a large project. This is going to require an equal amount of effort in both hardware and software. So let's talk about the software we are going to need for this project. I will put links to all the different softwares need and setting each software up will be an individual step as it can be quite complicated. So lets dive in!

Raspbian - We are going to need an operating system for our Raspberry Pi to run. For this I will be using Raspbian as it comes pre-installed with Python and is a piece of cake to set-up and use. We don't need a pretty GUI however because we will just be using it's command line interface

Download - Link

* Ignore these tools if you are using a Keyboard, Monitor and Mouse!

PuTTY * - We are going to need to be able to connect to our Raspberry Pi via SSH. So PuTTY is the perfect client to do this.

Download - Link

Nmap * - If you need to know what the IP Address of your Raspberry Pi is we are going to use Nmap to find it.

Download - Link

FileZilla - We are going to need a way to transfer files onto the Raspberry Pi using SFTP which is FTP over SSH. Usful stuff

Download - Link

Win32DiskImager - This will be used to burn Raspbian onto an SD Card for use in our Raspberry Pi

Download - Link

We will also need a Web Server for our Raspberry Pi but we will get that later because we will be using apt-get.

I won't be showing how to install a Raspberry Pi because it's super simple and I am assuming you guys know how to setup one because you are reading this guide. So let's move on...



Step 3: Designing the GUI

This whole project is driven using a Web GUI I made. It's super simple and uses mostly HTML with little bits of PHP too. Some basic HTML knowledge will help you with this because I won't be showing you how I designed my website as this is a Raspberry Pi guide; not a web design guide. However I do have a video tutorial which is based on the same design I have used. You can watch in the video I have attached.

However you are going to need to create buttons. For example I made a "On" and "Off" image and linked that too a page which makes a nice big button. I'll talk later in a few steps about setting these buttons up to make lights turn on and off.

If you're interested, the image sizes are 200x100.

When you design your website make sure that your extensions are *.php and not *.html just to keep things all the same. However we are only going to be using two PHP files for one light.

Once you've designed your website and it looks the way you want too, we can move onto the next step. The next step is hardware so bare that in mind.

Step 4: Wiring Up the Lamp, Relay Connectors and Raspberry Pi.

I have tried my best to make a simple diagram which will help you wire up this. It's not too hard so don't fear. First of all let's prepare our Raspberry Pi and our Relay Connector. I recommend looking at the diagrams I have created to help you out. Depending on how many lights/lamps you want to add will depend on how many jumper wires you use.

The first image shows you how to wire up your Raspberry Pi to the 8 Channel Relay board using the Jumper cables. I recommend using Red for Positive and Black for Negative just to keep things simple.

Now that you've wired this up we can prepare our lamp. Please refer to the diagram featuring the lamp. I apologize for my crude designs, but hopefully they make sense. Please read the step by step instructions incase you are confused.

1. Cut the flex cable in half using Wire cutters and then strip back the insulation.

2. You will have two wires inside the flex - Blue(Neutral) and Brown(Live) (In most cases). Strip back the insulation on both of these wires and twist the ends up to make a neat end.

3. Connect the two blue ends of the wire together using a connector block or solder. Connect the brown wire to the relay channel using the middle and left side of one relay switch. Fasten this down with the screw and make sure it's secure. Ensure that the wires cannot short out.

Take a look at the final diagram to make sure you have everything setup correctly.

Now let's move onto testing our creation so far. Once we know it works we can start making it work via the web.

Step 5: Testing Our Lamp With the Relay Channel

So the way this works is very simple. Hopefully you guys will know what a Relay Switch is, but in case you don't I'll explain it anyway.

The Relay Switch works how a switch normally works, however it operates using an electrical source (The raspberry pi). Our raspberry pi cannot turn the lamp on by itself, so we use a relay switch to simple break the circuit when we want the lamp off and then connect the circuit when we want it in. This is controlled by an electrical magnet in the relay switch. You will hear a distinctive click when the relay switch is opened or closed. You can refer to the diagram for a visual representation of a relay switch.

I have included a picture of what my raspberry pi looked like when I had connected up the lamp, relay board and raspberry pi. Now lets test it!

Plug in your raspberry pi to a mouse, keyboard and monitor or use NMAP and PuTTY. Ensure that Python is installed as Python will be half of the scripting language we will be using. Open up Python and we can type in commands to make the lamp turn on and off. I will be using my windows machine for screenshots, however it is the same on the raspberry pi.

In the Raspberry Pi command line you will want to type in these commands, pressing <ENTER> after each one.

import RPi.GPIO as GPIO //This imports the GPIO Library into Python so we can use the GPIO Pins on the Pi.

GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.OUT) //We are using GPIO 2 as our first pin, which is where the green wire is connected to the first relay switch on the baord

GPIO.output(2, False) //Depending on how you wired the lamp to the relay will depend whether the this command will turn the lamp on or off. Changing False to True will turn the lamp off/on. If this works, then you are ready to continue. If you have problems read on:

Troubleshooting

When you type GPIO.output(2, False/True) you should hear a distinctive clicking noise coming from the relay channel, if you do not hear this then make sure that your relay board has power (From the raspberry pi) and that the wire going from the first channel is connected to GPIO Pin 2.

If you hear the clicking noise but the lamp does not turn on with either GPIO.output(2, False) or GPIO.output(2, True) then make sure that you have the wires from the lamp in the right sockets on the relay channel.

Step 6: Adding Web Functionality

By now you should have a website that you can transfer over to the Raspberry Pi. Watch the video by gigafide to learn how to turn your Raspberry Pi into a webserver so it can host our Web GUI. Once you've done that and you have verified that you can view your website on another computer we can move onto making the website turn on our lamp.

Inside your website directory create a new php file called "lampon.php" and inside put in this code:

<?php

system("echo raspberry | sudo -S python /var/www/PiHome/scripts/lights/lampon.py"); header( 'Location: '<page>.php' ) ;

?>

This will execute a script called lampon.py which we will create in a second. Make sure your that your file path (In bold) is the same. Also in bold I have entered a location for where the page will direct when the script runs, set this as your homepage or the lights page - your choice. Save this file.

Now create a folder in the website directory called "scripts" and inside there create another folder called "lights" and then inside there create a new file called "lampon.py" - This will be the python script that turns our lamp on. Inside there enter this code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.OUT)

GPIO.output(2, False)

And save it.

Now go back to your website design and make sure the hyperlink for your "On" button links to "lampon.php". Now when you click the button the lampon.php script will execute the lampon.py python script resulting in the lamp turning on. Now we can make it turn off.

Inside the website directory create a new file called lampoff.php. Inside this file enter this code:

<?php

system("echo raspberry | sudo -S python /var/www/PiHome/scripts/lights/lampoff.py"); header( 'Location: ' <page>.php' ) ;

?>

Again, make sure your file path is the same so that this works. Also set your redirection to redirect to the page of your choice.

Now make a new file in the scripts\lights\ folder called lampoff.py. Inside this file enter this code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.OUT)

GPIO.output(2, True)

Save this file.

Now hyperlink lampoff.php to your "Off" button. This should make your lamp turn off. If is the right way, swap the scripts around and it should work perfectly.

You now have a Website that can control your lights! The next step will be the conclusion.

?>

Step 7: Conclusion

So that's it guys! Controlling lights from your raspberry pi. I really liked this project and had fun working on it. If this guide seems a bit rushed it's because there is only 7 days left of the competition and I am super busy. I wish I had some video footage of this project however I had to dismantle the system after a week because my girlfriend kept complaining about it.

It's a great start in learning how you can control things like lights using a website. You can easily add more lights onto this and also add other neat things like an alarm clock. I had a button that switched all my lights on and off at the click of a button. It was awesome!

You can really go wild with this, I've just given you the baby steps.

If you have any problems or questions then feel free to contact me:

www.jackktutorials.com/contact

I have loads of different ways you can contact me - but I recommend emailing me.

Well that's it. I'll be ecstatic if I win this competition, but thanks guys! I hope you enjoyed my first Instructables.

<p>very helpful. can I use water pump for automatic irrigation instead of lamp</p>
<p>Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..</p><p><a href="http://mittutop.com/home-automation-in-chennai/" rel="nofollow">Home Automation in Chennai</a></p><p><a href="http://mittutop.com/home-security-in-chennai/" rel="nofollow">Home Security in Chennai</a></p>
<p>hi!! ..its good....</p><p>can we use java programming language instead of python?</p>
<p>If you use pi4j you can let java control the Rasperry GPIO pins</p><p><a href="http://pi4j.com/" rel="nofollow">http://pi4j.com/</a></p>
<p>No, Raspberry pi GPIO is Configured with Python but may Dual library mode may work!</p>
<p>Python can control GPIO. If you can can control GPIO with Java then Java will work</p>
<p>Ok. Found this little project very nice except that I'm running into a problem I can't figure it out.</p><p>I'm using a 4-relay module instead of 8-relay board. it shouldn't matter, anyway. first,I'm getting an error form Python (see attach, and I have no idea why because I'm not using any channels) then Module gets powered but no click on any relay so therefore lamp won't turn ON regardless the state (0, 1 or True/False). I've checked all wiring, pin-outs, Neutral and Hot, to make sure all is good. Still, relays won't engage.</p><p>any advice please?</p><p>much appreciated. thx</p><p>TangoRom</p><p>PS: I've posted the pics just once, not sure why preview shows me each pic three times!</p>
<p>This is late Reply But it will be Useful for others too. your problem is once you have used your GPIO.2 s High or Low you should release it and make it free other wise it was high the keeps like that!</p><p>Thanks</p>
<p>I think these guys sent me the wrong board. I've ordered a 5V relay module (from Amazon, JBtek as seller) and the label on those say 12V so it'll make sense that there's no enough power - not sure about current. If that's the issue I'll return it right away. Can anyone please confirm this?</p><p>thank you.</p>
<p>Yes, I think your are right.</p><p>The relays on my module report 5V, not 12V.</p><p>Cheers</p>
<p>I hope that's the case, I already asked Amazon for a replacement.</p><p>Thank you.</p>
<p>Hi tangorom,</p><p>I am not sure taht you are sending the signal to the right GPIO. Are you sure that you plugged the signal cable (white in your picture) to the GPIO2 (pin number 3 on the Pi)? The error you get is a warning, so to some extent it can be ignored, but if you plugged the red cable to pin no 2 (the 5V) the black to one of the GRND and the white to GPIO2 (pin no. 3), it should work.</p><p>Hope this helps...</p>
<p>Wiring is correct as you mentioned. I've noticed that relays are labeled 12V instead of 5V. This is what I've ordered:</p><p><a href="http://www.amazon.com/gp/product/B00KTEN3TM?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s00" rel="nofollow">http://www.amazon.com/gp/product/B00KTEN3TM?psc=1&amp;...</a></p><p>and received the unit from pic I've already posted. Haven't noticed when I've got it.</p><p>Anyway, about the Python warning, I know it can be ignored but still not sure why is there. I tried both versions of Python (2 &amp; 3) and got the same warning messages.</p><p>Thx</p>
<p>I tried doing some automation using Apache, didn't really work for me. So I tried using node and it works wonders. I even made a tutorial to help the community: https://github.com/deepanshpahwa/Home-Automation</p>
<p>Hey Jack,</p><p>I have a http 500 issue. I set the permissions to 755 and I don't have a .htaccess file to give me trouble. It seems to have stumped me(doesn't take much :P). If you have any thoughts, that'd be great!</p>
<p>Huh, I tested the other relay (8 channel) and this one works. But one-channel does not. So it seems to be a problem with a relay.</p>
<p>Hi,</p><p>in fact, it seems to be a problem with python program. Relay is working OK. I tested it like this:</p><p>- connected GND to Ground on RPi</p><p>- VCC to 5.0 VDC Power on RPi</p><p>When I connect IN to Ground on RPi, relay clicks.</p><p>I also tried to connect IN (on relay) to GPIO4 (on RPi). When I enter (in Python)</p><p>GPIO.setup(4, GPIO.OUT)</p><p>...relay clicks and the green diode switches on. But nothing happens, when I enter:</p><p>GPIO.output(4, False) or GPIO.output(4, True)</p>
<p>Hi, I have bought a <a href="https://www.aliexpress.com/item/Free-Shipping-1PCS-5V-low-level-trigger-One-1-Channel-Relay-Module-interface-Board-Shield-For/32501322820.html?spm=2114.13010608.0.73.9SJLfn" rel="nofollow">rele from AliExpress</a>, and it has marks VCC, GND and IN.</p><p>So I connected it to RaspberryPi3 like this (PIN marks on RPi <a href="https://4.bp.blogspot.com/-UeG-dGQjy9c/V8mDiJjlkhI/AAAAAAAACZ8/gkBuEQdnZ4MIyog77WL_qPkHd8Wl_8XLwCLcB/s1600/hardware.jpg" rel="nofollow">are from this picture</a>):</p><p>- GND to PIN 6 (Ground) <br>- IN to PIN 3 (GPIO 8 SDA1 (I2C)) <br>- VCC to PIN 2 (5.0 VDC Power)</p><p>I created two Python scripts - lightson.py and lightsoff.py and when I run them this happens:</p><p>$python lightsoff.py <br></p><p>$ python lightson.py <br>lightson.py:4:<br> RuntimeWarning: This channel is already in use, continuing anyway. Use<br> GPIO.setwarnings(False) to disable warnings.<br> GPIO.setup(2, GPIO.OUT)</p><p>Why does this warning happens?</p>
<p>with easier, i also made instruction to make remote light switch. Come to my blog for detail, thanks.</p><p>http://engineer2you.blogspot.com/2016/06/raspberry-pi-remote-light-switch.html</p>
<p>Hi. I am gonna try doing this project and i think i can handle it. Before i start i have some question regarding the project. I have basic knowledge of rpi, arduino but someone can teach me as i have not tried making anything with relay and web server before.</p><p>1) The control panel is made as a website does it mean anyone with the url can access the webpage and control my switches?</p><p>2) Example there is a master bedroom which i would like to control 2 bedside lamp, one fan on the other end of the room and also bathroom light via relay, does it mean i have to connnect 1 wire from each point to the raspberry? If the distance is too long then can i use a few rpi instead? Or what's the usual practice if few light point is far from each other. </p>
<p>Hey Jack Thank you for this project i like it but i stell have some errors </p><p>1. when i click to link it open lampon.php but it tell me server error 500 and the led don't wotk.</p><p>2. i don't know about php what shoud i put here</p><p>&lt;?php</p><p>system(&quot;echo raspberry | sudo -S python/var/www/PiHome/scripts/lights/lampoff.py&quot;); header( 'Location: '&lt;page&gt;<br>.php' ) ;</p><p><br>?&gt;</p><p><br></p><p>in the '&lt;page&gt;'.php ????</p>
<p>updates:</p><p>just got the right unit (5V instead of the 12 V module); everything is all good. works beautiful. thx everyone.</p>
<p>Hi Jack,</p><p>Nice project! I was going to borrow some ideas from it to control some small DC motors, all powered by a 12V battery. What I'm not sure though (I am new to DYI electronics), is how many of the relays you can control/power from the Pi at any time. Looking at the Relay module specs, it seems that each Relay draws 15-20mA, and googling around I found out that the Pi can provide a mx of 16mA on each GPIO, and a max of 50mA in total:</p><p><a href="http://raspberrypi.stackexchange.com/questions/9298/what-is-the-maximum-current-the-gpio-pins-can-output" rel="nofollow">http://raspberrypi.stackexchange.com/questions/929...</a></p><p>So I guess that no more than 2 or 3 relays can be powered by the Pi at any time, ot the Pi is going to fail. Am I correct or am I missing something? Thanks!</p><p>Davide </p>
I have bought a couple of raspberry pi's to play with but have not gotten into the capabilities of the gpio, I'm still working on learning Python. To set up the relay board I would do one of four things. One being use transistors take the load of using the relay board. I just glanced at one and if I were to guess it isn't using the power to directly run the relays. Second is an if that would use only a few gpio pins as a serial connection to free up pins on the pi for other uses. Third to serially use an arduino to be the middle man also freeing up gpio pins. Third a wifi or Ethernet sheild on an arduino to be the middle man between pi and relay board allowing the relay module to be placed in an optimal spot and also being able to run remote relays placed in light switch boxes etc.. I hope this helped. I am slightly new to the diy electronics community but hacker from birth.
<p>Hi, Many thanks for your comments.</p><p>In fact, I don't need to control that many relays. Probably 4 or 5 in total (so with one single relay module. The doubt I had was that the relay would be powered by the GPIOs *(so with a 50mA max), but it seems that they are powered by the 5V (pin 2) of the Pi, which I have been told gets the power directly from the USB/power supply, and the GPIOs are used only to close and open the relays.</p><p>Thanks and regards</p>
<p>Hi, thanks for showing an interest in my project. I too am an amature at electronics, but I'll explain my theory anyway.</p><p>The first hurdle you have is that the Raspberry Pi only has one 5V Pin, Pin 2. Pin 2 connects to the relay to provide it with power to operate the switches. You could try attaching more than one relay to the single pin simply because all it does is provide power.</p><p>The relay's only use power when the relay is closed. So if you had 3 banks of 8 relays then the raspberry pi has to power 24 switches - which may be too much for the Pi.</p><p>Hope this helps,</p><p>Jackk</p>
<p>Hi Jackk, thanks for your reply. Actually I'm not going to connect 3 banks of relays, but just one. I posted the question also on other RPi forums and I think that bottom line is that the relay module is powered by the 5v pin, not the GPIOs, so if I keep the total load (Pi+peripherals) below the Amps that the power supply can provides (2A in my case), it should be all right. The GPIOs are used only to send the signal to close/open the relay switches.</p><p>Many thanks.</p>
<p>interesting, nice one</p>
<p>You're connection system is interesting but I found a future project to integrate Raspberry Pi to other devices via internet so the you can manage a web gui over their or even your personal server: https://www.kickstarter.com/projects/572738346/iottly-the-first-iot-open-source-distro-for-makers it would be interesting to amplify your work.</p>
<p>Are you Elena, related to one of the people running that Kickstarter campaign? If so, please disclose as such whenever you are promoting it.</p><p>I note the three comments on your account are promoting this Kickstarter.</p>
<p>Nice! Thank you. I want this. Are there relay channels that have a clamp for both wires so you don't have to splice only one of the wires?</p><p>Also is there a cheaper wifi compatible arduino board for stuff like this?</p><p>I also have to look for something like this that can control dimming of larger (e.g. 60W) LED lights.</p>
<p>Also this could be nice to integrate into a box of a power extension cord!</p>
<p>Hi!</p><p>I think this is exactly what I seek for...</p><p>I'm building an house and I want to know if I can connect the whole house electric power board to something like RPI or Arduino.</p><p>I have knowledge with programming .. but with electricity not...</p>
<p>Thanks for this post. It is exactly what I was looking for to get started.</p>
<p>If you need any help, let me know :D</p>
Wouldn't it be easier using a app (ios/android) called Blynk instead of setting up a web server for simple buttons.
I wasn't aware of Blynk - however with a website you can more things to it and make a control panel that doesn't just control lights :)
<p>Well I am the author, of course I made it!</p>

About This Instructable

171,932views

1,121favorites

License:

More by JackkTutorials:Raspberry Pi Home Automation - Control lights, computers, CCTV and more! 
Add instructable to: