Close

Web Page

A project log for Home automation on the cheap

Using an esp8266, arduino nano, a salvaged usb power supply, and a couple relays you can automate anything

thjubeckthjubeck 07/21/2016 at 05:030 Comments

Time for the webpage. The page is written in php it is not the easiest way but I liked writing it. If you would like a tutorial on how to set up a Linux LAMP server please reply in the comments and I will try to get that up.

<?php

if (isset($_POST["ON0"])) {
 $fp = fsockopen("10.125.1.20", 23, $errno, $errstr);  // Replace the  IP address with your own
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "2");
    fclose($fp);
	}
 }
 
else if (isset($_POST["ON1"])) {
  $fp = fsockopen("10.125.1.20", 23, $errno, $errstr);   // Replace the  IP address with your own
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "3");
    fclose($fp);
	}
} 

else if (isset($_POST["ON2"])) {
  $fp = fsockopen("10.125.1.21", 23, $errno, $errstr);  // Replace the  IP address with your own
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "2");
    fclose($fp);
	}
} 

if (isset($_POST["ON3"])) {




$f_pointer =fsockopen("10.125.1.20", 23, $errno, $errstr);  // Replace the  IP address with your own
fwrite($f_pointer,'4'); 
$fanstat=fread($f_pointer, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer);

$f_pointer1 =fsockopen("10.125.1.20", 23, $errno, $errstr);   // Replace the  IP address with your own
fwrite($f_pointer1,'5'); 
$lightstat=fread($f_pointer1, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer1);


$f_pointer2 =fsockopen("10.125.1.21", 23, $errno, $errstr);   // Replace the  IP address with your own
fwrite($f_pointer2,'4'); 
$lampstat=fread($f_pointer2, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer2);



if ($lightstat == '1'){
	$nLightstat= 2;
	
}
else if ($lightstat == '0'){
	$nLightstat= 0;
}


if ($lampstat == '1'){
	$nLampstat = 0;
}

else if ($lampstat == '0'){
	$nLampstat = 2;
}

  $fp = fsockopen("10.125.1.20", 23, $errno, $errstr);      // Replace the  IP address with your own
  $fp1 = fsockopen("10.125.1.21", 23, $errno, $errstr);    // Replace the  IP address with your own
if (!$fp ) {
    echo "$errstr ($errno)<br />\n";
} 
else {
    fwrite($fp, $nLightstat);
    fwrite($fp1,$nLampstat);

    fclose($fp);
    fclose($fp1);

	}
} 






$f_pointer =fsockopen("10.125.1.20", 23, $errno, $errstr); // Replace the  IP address with your own
fwrite($f_pointer,'4'); 
$fanstat=fread($f_pointer, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer);

$f_pointer1 =fsockopen("10.125.1.20", 23, $errno, $errstr); // Replace the  IP address with your own
fwrite($f_pointer1,'5'); 
$lightstat=fread($f_pointer1, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer1);

$f_pointer2 =fsockopen("10.125.1.21", 23, $errno, $errstr); // Replace the  IP address with your own
fwrite($f_pointer2,'4'); 
$lampstat=fread($f_pointer2, 1); //1 is the character length to be read. arduino sends a char length data
fclose($f_pointer2);

if ($lightstat=='0') {
   $Source="/images/offbulb.png";
   $stat = "Off";
} else {
  $Source="/images/onbulb.png";
  $stat = "On";
}


if ($fanstat=='0') {
   $Source1="/images/fan.png";
    $stat1 = "Off";
} else {
    $Source1="/images/fan.gif";
     $stat1 = "On";
}

if ($lampstat=='0') {
   $Source3="/images/offbulb.png";
    $stat2 = "Off";
} else {
    $Source3="/images/onbulb.png";
     $stat2 = "On";
}



$verz="1.0";

?>

<html>
<body>

<center><h1>MY LIGHTS</h1><b>Version <?php echo $verz; ?></b></center><br/>
<center><b>Lights <?php echo $stat; ?> <img src="<?php echo $Source; ?>"  alt="Light Off" height="42" width="42" </a> </b></center>
<center><b>Fan <?php echo $stat1?>  <img src="<?php echo $Source1; ?>"  alt="Light On" height="42" width="42" </a> </b></center>
<center><b>Lamp <?php echo $stat2?>  <img src="<?php echo $Source3; ?>"  alt="Light On" height="42" width="42" </a> </b></center>


<form method="post" action="temp.php" >
<center><h3>Bedroom</h3>

<input type="submit" value="Lights" name="ON0"  action="temp.php" width=20% height=10%><br/><br/>

<input type="submit" value="Fan" name="ON1"  action="temp.php"width=20% height=10%  ><br/><br/>

<input type="submit" value="Lamp" name="ON2"  action="temp.php" width=20% height=10%  ><br/><br/>

<input type="submit" value="Night Mode" name="ON3" action="temp.php" width=20% height=10%  ><br/><br/>

<!--

<br/></center>

<b><img src=temp_graphShort.png></a></b><br/>

<b><img src=temp_graphtoday.png></a></b><br/>

<b><img src=temp_graph.png></a></b><br/>

<b><img src=temp_graphmonth.png></a></b><br/>

<b><img src=temp_graphyear.png></a></b><br/> 

<center><b><img src="stream.php"></b><br/></center>-->



</form>

</body>
</html>

There is some code that is repeated but this is so the status will show correctly without reloading the entire page each time.

You may have to allow the script root / admin privileges to access the com port.

I have marked where to change the ip address. In this example, I am using two different nodes so there are two different ip addresses used.

Discussions