dicamarques
Posts: 4
Joined: Sat Nov 29, 2014 4:53 pm

Home automation project, Web page help

Sat Nov 29, 2014 5:02 pm

I want to make some simple automation project, just small scale. I want to have some temperature sensors, some relays to control the lights and some basic alarm system.
Python seems easy to program but the issue is that I want to use a webpage as the main GUI, so that I can remotely access the house with any device. Now here comes the reason of this post, I don't know how to make a webpage to control the GPIO, I've seen some examples of already made pages to control the GPIO but I don't understand anything about PHP.
If possible, I would like to see some examples of how to control and show gpio info on a web page.
Thanks.

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Home automation project, Web page help

Sat Nov 29, 2014 5:16 pm

Take a look at the back issues of The MagPi magazine (http://themagpi.com).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
SteveDee
Posts: 343
Joined: Thu Dec 29, 2011 2:18 pm
Location: Sunny Southern England
Contact: Website

Re: Home automation project, Web page help

Sun Nov 30, 2014 9:06 pm

Here is my extremely simple PHP/GPIO example: http://captainbodgit.blogspot.co.uk/201 ... ccess.html

AJStubbsy
Posts: 5
Joined: Thu Jun 25, 2015 4:33 pm

Re: Home automation project, Web page help

Fri May 13, 2016 1:29 pm

If anyone could help me with my code I'd be massively grateful. I've been following tutorials but they haven't quite been what I'm looking for and so I've tried to tweak code myself. Unsurprisingly it's not working.

I've got my Raspberry Pi wired up to my temperature controller, so I can short the HW and CH heating buttons and simulate them being pressed, to advance the heating or hot water. I've also got two of the GPIO's as inputs to detect when the LEDs are on. That's all working but I'm trying to control the GPIO from a web page using some html and php code. The web page will display but I can't get the buttons and the feedback from the LEDs to work. Any help would be superb.

Code: Select all

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Heating Controller</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
	background-color: #333;
}
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	color: #C00;
	font-size: 24px;
}
</style>
</head>

	<body bgcolor="#666666" text="#FFFF66">
	
	<h1 align="center">Heating Controller</h1>
	<form method="get" action="PiStat.php">
		<p align="center">
		  <input type="submit" style="width:80px; height:80px; font-size:large; margin-right:30px; color:#000;" value="HW" name="hw">
		  <input type="submit" style="width:80px; height:80px; font-size:large; color:#000" value="CH" name="ch">
	  </p>

	</form>
	
	<?php

#set GPIO 4 OUT for HW switch
	exec("sudo gpio mode 4 out");
	if(isset($_GET['hw'])){
		exec("sudo gpio write 4" "1" );
		sleep (0.3);
		exec("sudo gpio write 4" "0" );

	}

#set GPIO 26 OUT for HW switch
	exec("sudo gpio mode 26 out");
	if(isset($_GET['hw'])){
		exec("sudo gpio write 26" "1");
		sleep (0.3);
		exec("sudo gpio write 26" "0");

	}
	
#set GPIO 5 IN for HW LED
	exec("sudo gpio mode 5 in");
	exec("sudo gpio read 5", $hwled);
	if($hwled == "1") {
	
echo "HW ON"

	}

	else {

echo "HW OFF"

	}
		
#set GPIO 22 IN for CH LED
	exec("sudo gpio mode 22 in");
	exec("sudo gpio read 22", $chled);
	if($chled == "1") {
	
echo "CH ON"

	}

	else {

echo "CH OFF"

	}

	?>

</body>

</html>

User avatar
SteveDee
Posts: 343
Joined: Thu Dec 29, 2011 2:18 pm
Location: Sunny Southern England
Contact: Website

Re: Home automation project, Web page help

Fri May 13, 2016 5:13 pm

It may be a user group/permissions problem. See my post: http://captainbodgit.blogspot.co.uk/201 ... ccess.html

AJStubbsy
Posts: 5
Joined: Thu Jun 25, 2015 4:33 pm

Re: Home automation project, Web page help

Fri May 13, 2016 6:46 pm

Really appreciate the help SteveDee. I'll try to sort privileges in a bit. Did the code actually make sense then, or is it better practice to use the $variables?
Cheers

User avatar
SteveDee
Posts: 343
Joined: Thu Dec 29, 2011 2:18 pm
Location: Sunny Southern England
Contact: Website

Re: Home automation project, Web page help

Fri May 13, 2016 7:31 pm

Why not copy / paste my 21 line php code and see if it works? If it doesn't work, it probably means you have a problem outside your code. If it does work, you probably have a problem with you code.

You could do all this in less than 10 minutes.

AJStubbsy
Posts: 5
Joined: Thu Jun 25, 2015 4:33 pm

Re: Home automation project, Web page help

Sun May 15, 2016 8:50 am

I can only get html code working for some reason and basic php, like displaying php info on the index.php page. When I reference your sample code, my web browser says the page is not available. I've gone through the ownership steps and checked that everything is owned by www-data and written in the sudoers, so there's privileges for gpio control.

I've dabbled with so many things over the last day that I'm going back to basics! Fresh install of Jessie and I'll concentrate on getting your sample code working before I tackle my home automation code.

Thanks again.

User avatar
SteveDee
Posts: 343
Joined: Thu Dec 29, 2011 2:18 pm
Location: Sunny Southern England
Contact: Website

Re: Home automation project, Web page help

Sun May 15, 2016 7:07 pm

AJStubbsy wrote:...when I reference your sample code, my web browser says the page is not available...
This is the place to start.

Any .html or .php file in your servers /var/www folder should be accessible using a browser via: ip address + port + file name (paths are relative).

I'd worry about this first, even though the code wont run.

AJStubbsy
Posts: 5
Joined: Thu Jun 25, 2015 4:33 pm

Re: Home automation project, Web page help

Sun May 15, 2016 7:37 pm

It's working!!
I started fresh and slowly entered the code making sure each bit was working. My main problem was syntax errors, which was causing the "page cannot be displayed" problem. Using # instead of // for my reminder text didn't help. I'm a complete novice to coding though, so it was a fluke to spot that and I'm still not sure if it actually matters. The code to turn the gpio on and off was fine but my sleep() wasn't long enough. I can now turn the heating and hot water on and off within my home network. Next step is to make it accessible from anywhere, just need to research security methods for doing this.

In case anyone stumbles across this post and wants to do the same, here's my code

Code: Select all

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Heating Controller</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
        background-color: #333;
}
body,td,th {
        font-family: Arial, Helvetica, sans-serif;
        color: #C00;
        font-size: 18px;
}
</style>
</head>

        <body bgcolor="#666666" text="#FFFF66">

        <h1 align="center">Heating Controller</h1>
        <form method="GET" action="index.php">
                <p style="float:left; margin-left:25px">
                  <input type="submit" style="width:80px; height:80px; font-size:large; margin-right:30px; color:#000;" value="HW" name="hw">
          </p>
                <p style="float:right; margin-right:25px">
                <input type="submit" style="width:80px; height:80px; font-size:large; color:#000" value="CH" name="ch">
          </p>

        </form>

        <?php

//set GPIO 4 OUT HW - BCM 4 = wPi 7
        if(isset($_GET['hw']))
        {
        system ( "gpio mode 7 out" );
        system ( "gpio write 7 1" );
        sleep (1);
        system ( "gpio write 7 0" );
	}

//set GPIO 26 OUT HW - BCM 26 = wPi 25
        if(isset($_GET['ch']))
        {
        system ( "gpio mode 25 out" );
        system ( "gpio write 25 1" );
        sleep (1);
        system ( "gpio write 25 0" );
        }

//set GPIO 5 IN HW LED - BCM 5 = wPi 21
        exec("gpio read 21", $hwled);
        if($hwled[0] == "0")
        {
        $hw = "<img src=\"/data/ON.png\"; style='position:fixed; top:200px; left:60px; width:50px; height:50px'/>";
        }else
        {
        $hw = "<img src=\"/data/OFF.png\"; style='position:fixed; top:200px; left:60px; width:50px; height:50px'/>";
        }


//set GPIO 22 IN CH LED - BCM 22 = wPi 3
        exec("gpio read 3", $chled);
        if($chled[0] == "0")
        {
        $ch = "<img src=\"/data/ON.png\"; style='position:fixed; top:200px; right:60px; width:50px; height:50px'/>";
        }else
        {
        $ch = "<img src=\"/data/OFF.png\"; style='position:fixed; top:200px; right:60px; width:50px; height:50px'/>";
        }
        echo "$hw";
        echo "$ch";
        ?>

</body>

</html>


I followed this tutorial
http://fudgetech.blogspot.co.uk/2013/09 ... ating.html

and some guidance from these tutorials for the hardwiring of the pi.

https://www.youtube.com/watch?v=9rT44WLedFg

and of course, SteveDee's guide for permissions and ownership.

Here's the end result and a screen shot of the app. The "LEDs" are just circles drawn in powerpoint then saved as png. They're sat in their own directory called data, which is in the same directory as index.php
Attachments
HC.jpg
HC.jpg (11.22 KiB) Viewed 1322 times
PiStat.jpg
PiStat.jpg (25.57 KiB) Viewed 1482 times

Return to “Automation, sensing and robotics”