Thursday, 11 February 2016

Simple home automation light control

I have been learning a lot in the last few days: HTML code and styles, PHP. After I set up the apache2 server on my Raspberry Pi (see previous post) I have connected four LEDs to the Pi on GPIO 22, 23, 24, & 25.

DSC 2178

HTML

Then I have written two programs. One is the "index.htm" served by apache2 from its "DocumentRoot" folder "/var/www" when you connect. This displays a series of four ON and four OFF buttons and a single ALL OFF button. The HTML code actions a PHP file when any button is pressed and passes, or POSTs, data about which button has been pressed. The code is:

Screen Shot 2016 02 11 at 13 04 27

Screen Shot 2016 02 11 at 13 04 38

In the first part some meta data is defined, this is for the iPhone which responds to the "viewport" name and sets its scaling to "1.0". This fills the screen with the HTML page. Next is a couple of style definitions for the ON and OFF "submit" buttons. The lower code shows how the form is implemented, with a style class used for ON and OFF buttons. When a typical button is clicked the action is to call the "gpio.php" file and pass the "name" with a POST method.

PHP

The "gpio.php" is

Screen Shot 2016 02 11 at 13 10 14

Here the values of the "name", e.g. "table_on" are checked to see if they are True or False. And the shell command line "exec(gpio.." is called to switch the LED on (1) or off (0).

No comments: