Pi-light

Turn on the lights in you living room from your phone!

WARNING: This is a serious security hazard if accessible from outside your local network!

What you need:

advertisement:

Building it

If you want to power the raspberry pi through the GPIO pins this is totally possible! Here is a diagram of the pinout of the raspberry pi. (you can easily find them by googling raspberry PI pinout.)

The 2 5V power pins are directly hooked up to the 5V power planes, which means you can also use them to power the device. Conveniently there also is a ground pin right next to the most right 5V ping (pin 4 Power, pin 6 ground.)

You can connect 5 volt up to those pins and your device should power up CHECK THE POLARITY it's better to double check than to waste a board.

In my case I used the following pins do drive my relays (I am only using green pins).

Pin #Channel #
111
132
153
124
165
186
327
298

Installing it

For this project I am using rasbian, which can be downloaded here: https://www.raspberrypi.org/downloads/raspbian/ Their zip download is pretty slow, but I recommend going that route.
After installation is done I connected it up to wifi (mouse and keyboard no CLI at this point), after this is done you can make the IP static so you can always reach the PI on the same IP address after startup. For this change the config file using: sudo nano /etc/dhcpcd.conf in the file add the following lines (with your information):
static ip_address=192.168.2.70/24
static routers=192.168.2.254
static domain_name_servers=192.168.2.230

also be sure to turn on SSH access so you can remotely control the PI, also set the pi to start in CLI mode instead of GUI, this will decrease load.

Finally set a password you can remember if you need to change something down the road, since this project is running on your local network (behind NAT) and not outside of that this doensn't have to be overly complicated.

sudo passwd pi

and then enter your new password.

Now when you are on a windows computer you can use a program like Putty to connect to your raspberry pi. Simply type in the IP of the raspberry PI then (hit yes for the key) once prompted to log in, use pi as your username and enter the password you set earlier. Now install Apache and then PHP by typing:

sudo apt-get update
sudo apt-get install apache2 -y
sudo apt-get install php5 libapache2-mod-php5 -y

The coding part

There are 2 major things we need to write. if you do it the same way I did it. the Front end (the webpage people get to see when they go to your page) and the script that turns on and off the lights.

This is also where the security people are going to cringe.

First to the the Directory where the webserver runs:

cd /var/www/html

Remove the default page (after going there once to make sure it works.) http://addressofyourpi/

rm index.html

Edit a new file called index.php

sudo nano index.php

Here you can write a fancy page that will display when you want to turn on and off the lights. It is important to put the PHP and input tags in here to make this work.

PHP; You can add this in the end of the file, you will have to do this for every channel CHANGE THE NUMBER:

<?php if(isset($_POST['1aan']))
{
$my_file = '1aan.stat';
fopen($my_file, 'w');
}
if(isset($_POST['1uit']))
{
$my_file = '1uit.stat';
($my_file, 'w');
}?>

Once you have saved this file you should see your page but nothing happens when you click a button, so it seems. but when you now press a button a file is created on the webserver.

Now we can create a python script to check if a file exists and then based on that turn a light on or off

Edit a new file called check.py, at the beginning add the following:

import os.path

import RPi.GPIO as GPIO
import time

lamp1 = 11
lamp2 = 13
lamp3 = 15
lamp4 = 12
lamp5 = 16
lamp6 = 18
lamp7 = 32
lamp8 = 29
GPIO.setmode(GPIO.BOARD)
GPIO.setup(lamp1, GPIO.OUT)
GPIO.setup(lamp2, GPIO.OUT)
GPIO.setup(lamp3, GPIO.OUT)
GPIO.setup(lamp4, GPIO.OUT)
GPIO.setup(lamp5, GPIO.OUT)
GPIO.setup(lamp6, GPIO.OUT)
GPIO.setup(lamp7, GPIO.OUT)
GPIO.setup(lamp8, GPIO.OUT)

Then create a loop, I will now include a screenshot of a sample so you can't just copy paste all of this, you gotta learn too ;)

it's really important to get the indents right, else python will complain about this.

Now we have to make a launcher script.

I made a file called init.sh with the following content:

#!/bin/sh
cd /var/www/html
sudo python check.py

Don't forget to make it executable using chmod 755 /var/www/html/init.sh and then add the script to the startup of the machine

sudo crontab -e

and add the line:

@reboot sh /var/www/html/init.sh

Hai, I use cookies to make things work - Made by: Elias Eskes - © 2017