Home Automation Using Raspberry Pi
Ever wondered how to control your fans , lights etc using your phone or computer from anywhere !
Control Diwali Christmas Lights by any mobile phone , tablet , laptop etc simply using Raspberry Pi within 1 hour. Learn to operate Relays , Control GPIO Pins On Pi Using PHP And Put them all together using AJAX .
What do you need ???
- Raspberry Pi ( Any Model ) [Control Lights With Raspberry Pi Using Relay]
- Relay Board . ( I used a 12V 4 Channel Relay )
- Christmas / Diwali Lights
- Jumper Wires And Normal Wires.
- Christmas Lights.
- Wiring Tape / Scissors /
- WiFi Router – Modem
- Wireless Network Card For Pi. ( Optional )
- Lan Cable
- Basic Knowledge of PHP and HTML.
Preparing Your Raspberry Pi (Control Lights With Raspberry Pi Using Relay)
1.) Update Your Pi . Using the command
sudo apt-get update && sudo apt-get upgrade
Updating Raspberry Pi – Spunky Technology
2.) Install WiringPi Software on it. watch How to install WiringPi .
3.) After that Install PHP on Pi .
Raspberry Pi Setup Complete
Now Our Pi Is Ready .So Now We’ll move to hardware section of Raspberry Pi Home Automation project.
Setting Up Hardware
Now we have to trigger the relay using PHP Script , For that
Control Lights With Raspberry Pi Using Relay
1.) Take two jumper Wires and Connect one of them to GPIO 24 ( Pin #18) On Pi and the Other one to GND Pin . You may refer the following diagram .
2.) Now Connect the other ends of the wire to relay board. Connect the GND To GND On Relay and GPIO Output wire to the Relay Channel Pin Number.That also depends over the relay which you are using. But The GND goes To GND on Relay And GPIO Output Goes to The Relay Input pin.
Caution ! Be very care full with the relay connections with Pi as if it causes back flow of current , It would lead to a short circuit.
3.) Now take the lights , cut one of its wire into two pieces , and connect its both ends with a wire. Then connect these wires to the NO ( Normally Open ) Port of Relay And Other to Output Pin Number. To know more about relays , you may check here.
4.) Now connect the power supply to the relay , either using 12v power adapter or as suggested in your relay . Power on the Pi and Connect it to your router.
The Final Part . PHP And AJAX
1.) Goto the Root Directory of nginx Web Server using the command –
2.) Create a new file . I’ve named it pin24on.php . Using the command-
sudo nano pin24on.php
3.) Add the following code to it.
And Save it By Pressing CTRL + O .
4.) Create another file using the same method , but name it as pin24off.php . And Add the following code to it
Save it .
5.) Now we have two files , One named as pin24on.php and other named as pin24off.php . So here pin24on.php , turns ON the pin , while pin24off.php turns it OFF. Now we need to have both these functions on one page , so now we will use AJAX to make it happen.
Actually what we’re doing over here is that , In the file pin24on.php , We set the GPIO Pin 24 To Output Mode , Nd then we put it to ON mode. That means when the page pin24on.php is loaded , the script turns the GPIO 24 On. While for Turning Off the GPIO 24 , We used ‘ 0 ‘ instead of ‘off’ , In terms of Binary language ” 1″ Means On and ” 0 ” Means Off.
Now Create A file , Named as index.php , And Add the following code to it .
So here’s the HTML Code ,
Control For Lights
CODE IS HERE :SOURCE CODE
Connect the relay to 12v Power Supply , Plug in the Christmas lights to mains Power Supply . Now goto your web browser , type the Address of your Pi followed by the controls file name . Here click on the buttons . If everything goes fine , your lights should work , If it does'nt , you feel free to ask them in comments section or by Email . Thank You And Don't Forget to like our page and Subscribe.