RPi GPIO status update to php button and script

1k Views Asked by At

I'm stump on some php code.

I currently have a button change color by getting GPIO status on a Raspberry pi3, Red when off Green when on, I'm stuck trying to get it to execute one of the two python scripts when the button changes color. I can get it scripts executed if I use two button one for on and one for off, Would rather have it one button when status color.

Thanks in advance for any help.

Everything I have tried the page won't load . Below is what I have for status update.

<html>
<head>
</head>
<body>
<?php $status1 = trim(shell_exec("gpio -g read 12")); ?>
<?php 
if ($status1 == "1") {
echo  "<button  style=\"background-color:#FF0000; width: 400px; height:350px; font-size:60px;\">Relay 1</button>";
}  else {
echo "<button  style=\"background-color:#009900; width: 400px; height:350px; font-size:60px;\">Relay 1</button>";
}
?>
<br>
<br>
<?php echo date('Y-m-d H:i:s'); ?>
</body>
</html>
1

There are 1 best solutions below

1
On

When you want a button to send data it has to be in a form with an action and method specified, and the button has to be an (greater than)input type=submit ... (less than) - if you want to do something based on the form data, there has to be a named element, with a value, or the PHP won't see it in $_POST.

<html>
<head>
</head>
<body>
    <form name="theform" method="post" action="<?php print($_SERVER['PHP_SELF']); ?>">
<?php
//$status1 = trim(shell_exec("gpio -g read 12"));

$status="1";
if(isset($_POST['turnon'])){

    // this is where you call your python or whatever when the status is already 1
    $status="0";

    }

if(isset($_POST['turnoff'])){

    // this is where you call your python or whatever when status is already 0

    $status="1";

    }

if ($status==="1") {
echo "<input name=\"turnon\" type=\"submit\" style=\"background-color:#FF0000; width: 400px; height:350px; font-size:60px;\" value=\"Turn Relay 1 on\">";
}  else {
echo "<input name=\"turnoff\" type=\"submit\" style=\"background-color:#009900; width: 400px; height:350px; font-size:60px;\" value=\"Turn Relay 1 off\">";
}
?>
</form>
<br>
<br>
<?php echo date('Y-m-d H:i:s');

?>
</body>
</html>