When I setup my Raspberry Pi server 8 months ago, one of its potential uses was for me to play with PHP and hopefully learn more about this language.
I found a bit of time this week, so decided to try making a minimal PHP program to control the Pi gpio from a remote computer or phone.
What I’ve come up with is a simple file using HTML and a few lines of PHP code.
Background
My Raspberry Pi server is using lightpd as configured in this earlier post.
I also installed wiringPi and configured the system in order to be able to access and control the gpio.
This server is also being used to automatically control a light outside the back of the house, and to monitor temperatures and graph the readings.
What is PHP
PHP is a language that runs on the server side. This results in it being processed on a server while also permitting limited communication with the client, typically through the user’s web browser on their computer.
This definition holds significance as a basic PHP file typically loads, executes its code, and then halts upon accessing a web browser.
Consider this simple “Hello world” example;
<?php
echo “Hello world!”;
?>
If you save this file on your server as (say) HelloWorld.php when you point your remote web browser at it, it will simple return the string: Hello world!
The PHP code will only run once, but each time you refresh your browser it will run again. You can see this more clearly by adding the PHP time() function to HelloWorld.php:-
<?php
echo “Hello world!”;
echo “<br>”;
echo ‘Time: ‘. date(‘H:i:s’);
?>
So if you wanted the “time” to update, you would have to keep pressing the browser refresh button to run the file again and again. The alternative is to get the browser to do it for you, something like this;
<html><head>
<title>PHP refresh</title>
<meta http-equiv=”refresh” content=”1″>
</head></html>
<?php
echo “Hello world!”;
echo “<br>”;
echo ‘Time: ‘. date(‘H:i:s’);
?>
You can also do this with a PHP method;
<?php
header(“Refresh:1”);
echo “Hello world!”;
echo “<br>”;
echo ‘Time: ‘. date(‘H:i:s’);
?>
…which is considered a better way of doing it. In both cases the update is set for 1 second intervals.
Toggle a gpio output
If we want to switch the state of a Raspberry Pi gpio pin from high to low and low to high with PHP, we need some way to control our server-side PHP program.
One way is to use HTML Forms, which is commonly used to allow users to enter and submit details, for example, on sites requiring user registration.
So here I’m using a button to toggle the GPIO pin 7 output and a text box to display the output status.
…and here is the code;
<!DOCTYPE html>
<html>
<head><title>GPIO Tog</title></head>
<h1>RaspberryPi GPIO Toggler</h1>
<body>
<form method=”POST” action=””>
<input type=”submit” name=”btnToggle” value=”TOGGLE!”><br><br>
</form>
<?php
# set pin 7 mode to output
exec(“sudo gpio mode 7 out”);
if ( isset( $_POST[“btnToggle”] ) ) {
# GPIO pin 7 toggles state
# If error codes are not 0, you have a problem
# read pin level
exec (“sudo gpio read 7”, $rReturn, $errcode );
if ( $rReturn[0] == “0”) {
$rReturn[0] = “1”;
}
else {
$rReturn[0] = “0”;
}
exec(“sudo gpio write 7 “.$rReturn[0] );
}
# read pin
exec (“sudo gpio read 7”, $rReturn, $errcode );
if ( $rReturn[0] == “0”) { ?>
Pin 7: <input type=”text” name=”Pin7″ value=<?php echo “OFF”;?> />
<?php
}
else { ?>
Pin 7: <input type=”text” name=”Pin7″ value=<?php echo “ON”;?> />
<?php
}
?>
</body></html>
Upon loading the file in a web browser, pin 7 of the gpio is configured as an output and its status is checked. The text box shows OFF or ON based on its status.
When the button is pressed by a user, the status of pin 7 is checked and then changed to the opposite state (either from low to high, or high to low). Afterward, the status is revisited and the textbox is modified.
This functions properly unless gpio pin 7 output is manipulated by a different source, like a separate browser or control software. In this situation, we must regularly check and modify the pin status by utilizing one of the refresh techniques discussed previously.