Getting Started with Remote Control of a Raspberry Pi's OS and GPIO port by PHP

James Farnsworth uses a combination of HTML, PHP and shell script to produce a most impressive demonstration. It is very satisfying to reproduce his set-up and to use a web page to control a distant Raspberry Pi. (A shorter version of this page was first published on CodeShow; we republish it here to give it the continued attention that it deserves).

For details of installing the Apache web server, required for the tasks described on this page, see Getting Started with a Web Server on the Pi. (The preceding page provides details of accessing the Raspberry Pi remotely by SSH).

Remote Control of a Raspberry Pi's OS by PHP

Here are some Linux commands you can try:

  • echo "texttodisplay" - writes the text in quotes
  • pwd - writes the current directory you are currently working in
  • ls - lists the contents of the directory you are in
  • cd /directory/otherdirectory - changes what directory you are in (paths from the origin of the Linux file system start with a '/', paths from your current directory start with './', and paths from the parent directory of the one you are in start with '../'
  • whoami - writes what user account you are logged on as
  • ps aux - write all the processes that the Raspberry Pi is running (a lot - as with any computer)
  • gpio -g write 24 1 - turns GPIO24 (connected to an LED) on. You can replace 24 with 23, also connected to an LED, and 1 with 0, to turn off the GPIO pin

    This instruction is part of the WiringPi GPIO utility which I installed on the Raspberry Pi to simplify GPIO operation. I recommend this tutorial for installing and using the GPIO utility. Also, set up the pins first with the commands gpio -g mode 23 out and gpio -g mode 24 out.

These screenshots show inputs to and outputs from the first three of these commands.

Echo Input

Echo Input

Echo Input

Echo Output

Input of pwd

Input of pwd

Output of pwd

Output of pwd

Input of ls

Input of ls

Output of ls

Output of ls

This method does not work with commands that require keyboard input during their execution, since it can only run executables rather than relay key presses. Also, it does not work with some more advanced command line features such as using '>' to relay output to a file since output is already relayed to the commandoutput file instead. Whether or not these work is down to how commands are interpreted by the OS.

Since the most recent command's output is shared between all computers on the site, you could execute a command using one computer, then move to another and refresh (F5) the remote access web page to see the output. You could even try having a conversation between computers using the 'echo' command!

How it works:

The HTML on this page shows the command          This PHP script writes a file           This command file is read by another program,
output file, and sends the commands to a  -----> with the command in it, and      -----> which then carries out the command and
PHP script when 'Submit' is clicked.             sends you back to the 1st page.         puts it in the command output file.

PHP script: sendcommand.php

<?php 
$f=fopen("command","w");
fwrite($f,$_POST['command']);
fclose($f);
sleep(2); 
header('location:/remote');
?>

The folder remote must have the appropriate write permission to enable the script to create the file and write to it.

The PHP file redirects to /remote because that is a folder in /var/www, inside which is the remote page under the name index.html. I redirected to the folder rather than the file name so it would look a bit neater in the URL bar.

The sendcommand.php script uses a separate file called command. It creates command and puts the command in it. The shell script below will check every few seconds if a file called command exists. If it does, then it will execute the command in the command file and then delete it. The output of the command is then written over what was previously in the commandoutput file. (This file must have the appropriate write permission to enable the script to write to it).

Code of runwebcommand.sh

#!/bin/sh

#delete any command left previously - start on a clean slate
if [ -f /var/www/remote/command ]; then
  rm /var/www/remote/command
fi

#loop forever
while :
do
  if [ -f /var/www/remote/command ]; then #if command file exists
    $(cat /var/www/remote/command) > /var/www/remote/commandoutput #execute and put result into commandoutput
    rm /var/www/remote/command  #then delete command file
  fi
  sleep 1
done
You can put the script in the /var/www/remote folder and run it in the background with the command sudo /var/www/remote/runwebcommand.sh &. Alternatively, you can make it run in the background automatically by adding this command to the crontab file:

@reboot sudo /var/www/remote/runwebcommand.sh &

(The command crontab -e opens the crontab file in an editor).

Remote access will give the user root access to the Raspberry Pi, since we have used 'sudo' in the crontab entry for running the shell script to execute commands. You could omit the sudo to remove root privileges (but be careful that the user whose crontab runs runwebcommand.sh is not a superuser like 'pi' - who could just prefix any command with sudo).

You could even create another user with limited privileges and not a superuser, and add runwebcommand.sh to their crontab instead to enforce those privileges upon remote access. Remember that, if something is going wrong, you can terminate runwebcommand.sh using ps aux | grep runwebcommand.sh to find the process ID (PID) and kill [insert PID] to kill it.

 

Source code of HTML/HTML5 for form and iframe in /var/www/remote/index.html

<form method = "post" action = "sendcommand.php" >
 Enter command:  <input type = "text" name = "command" />
<br /> <input type = "submit" />
</form>

<iframe src = "commandoutput" sandbox = "" ></iframe>

Remote Control of a Raspberry Pi's GPIO by PHP

Each PHP script submitted when a button is clicked is intended to run on a web server on the Raspberry Pi.

The form looks like this screenshot:

GPIO Form

GPIO Form

How does it work?

                                    PHP script which executes
Radio             Uses post         commands to change the              LEDs turn on or
Buttons    -----> to send    -----> state of the Raspberry Pi's  -----> off to user's
(on/off).         data.             GPIO (General Purpose Input         command.
                                    Output) pins.

PHP Scripts for GPIO Control

setupgpio.php

<?php  
exec ( 'gpio -g mode 23 out' );
exec ( 'gpio -g mode 24 out' );
header ( 'location:/gpio' ); 
?>

changegpio.php

<?php
exec ( "gpio -g write 23 " . $_POST [ 'gpio23' ]);
exec ( "gpio -g write 24 " . $_POST [ 'gpio24' ]);
header ( 'location:/gpio' );
?>

HTML and HTML5 Source Code for the GPIO Form

The form code should be in the file /var/www/gpio/index.html.

HTML5

<form action = "setupgpio.php" >
  <input type = "submit" value = "Set up pins 23 &amp; 24" ></input>
</form>
<br />
<form method = "post" action = "changegpio.php" > 
GPIO23:
<input type = "radio" name = "gpio23" id = "on23" value = "1" checked = "checked" /> 
<label for = "on23" > On </label> <input type = "radio" name = "gpio23" id = "off23" value = "0" /> 
<label for = "off23" > Off </label>
<br /> 
GPIO24:  
<input type = "radio" name = "gpio24" id = "on24" value = "1" checked = "checked" /> 
<label for = "on24" > On </label>
<input type = "radio" name = "gpio24" id = "off24" value = "0" />
<label for = "off24" > Off </label>
</form>

HTML

<form action = "setupgpio.php" > 
  <input type = "submit" value = "Set up pins 23 & 24" >
</form> 
<form method = "POST" action = "changegpio.php" >
GPIO23:  
<input type = "radio" name = "gpio23" value = "1" checked > On  <input type = "radio" name = "gpio23" value = "0" > Off 
<br /> 
GPIO24: 
<input type = "radio" name = "gpio24" value = "1" checked > On  <input type = "radio" name = "gpio24" value = "0" > Off 
<br />
<input type = "submit" > 
</form>
Programming - a skill for life!

Setting up the Raspberry Pi, installing Lazarus, use of the GPIO port (with and without PiFace), web servers and remote control