Sunday, 7 February 2016

Setting up R_PI apache2 server for Home Automation

Here's a very brief set of steps to install and set up apache2

// [add “-y” to bypass Y/n? go ahead]
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2 [-y]
sudo apt-get install php5 libapache2-mod-php5 [-y]

// set up permissions for /var, /var/www, /var/www/html folders
sudo addgroup www-data
sudo chown -R www-data:www-data /var
sudo adduser pi www-data

// let everyone rw www
cd /var/www/
sudo chmod 777 www
cd /var/www
sudo chmod 777 html

// delete index.html from /html
cd /var/www/html
sudo rm index.html
// remove /html
cd /var/www
sudo rm html

//restart apache
sudo service apache2 restart

// change the document root for apache2
cd /etc/apache2/sites-available/
sudo nano 000-default.conf
// change the line to "DocumentRoot /var/www" Exit ^X - "Y" and save
LIGHTS CONTROLLER

Now you are ready to put files in /var/www/. The first should be a new server index file. For the Home Automation project the index.html file is:

Screen Shot 2016 02 07 at 20 44 58

And then also in the /var/www/ folder put the gpio.php file:

Screen Shot 2016 02 07 at 20 43 54

When you point your browser at the R_IP "IP address", e.g. raspberrypi.local, you will see the index.html file

Screen Shot 2016 02 07 at 20 46 09

And when you click one of the two buttons R_PI gPIO 17 or 27 will switch ON or OFF.

What is all this HTML and PHP?

Screen Shot 2016 02 08 at 14 08 53

In the HTML file create a "form", define the action to take when this form is sent from client to server, i.e. execute "gpio.php" program on the server. Send data to the server using the method "POST" if the button "table_on" or "ON", table_off" or "OFF" are clicked. "Table Lights:" is the text to put on the form, before creating these buttons. The "submit" creates the button with a value written on it of "ON". (the second "input" creates a second button "OFF"). Pressing the button sends a value of "True" only when a button is pressed.

Screen Shot 2016 02 08 at 14 14 57

In the PHP file, the variable "$_POST['table_on']" is tested by "isset(...) function to see if it is set "True" becaue this button was pressed. If so it executes the command in "exec(...);"

No comments: