Raspberry Pi PHP GPIO Guide

Description

The RaspberryPi provides an advanced solution for those that are limited by the constraints of other microcontroller based products such as the Arduino. Running various operating systems including a full Linux distribution opens the doors for your project to make use of thousands of free packages and scripts.

Introduction

Below I am using the popular Linux distribution Raspbian on the RaspberryPi which will give you one of the most flexible and well rounded Operating Systems currently available for the platform. One of the advantages of the Pi is the ease at which it can be connected to your network and by extension the internet. Using the guide below you can learn how to access the GPIO pins of your RaspberryPi using simple PHP scripts and your Web Browser of choice.

WiringPi

This fantastic library gives developers a common set of API based functions for interacting with the GPIO pins on the RaspberryPi in a manner that will be instantly familiar to anyone who has used an Arduino before. Even for a first time user this package is ideal.

First use the cd (change directory) command to change to the current user's home folder using the ~ (tilde) as a shortcut for that location. We could download the files anywhere however this location makes sense as it is sort of like your My Documents folder on a Unix/Linux machine. Next using the git command make a copy of the files in the WiringPi repository.

pi@raspberrypi ~ $ cd ~
pi@raspberrypi $ git clone git://git.drogon.net/wiringPi

Using the cd command change the current directory to the wiringPi folder. Next using a bit of Linux shorthand execute the build script to compile the application.

pi@raspberrypi ~ $ cd wiringPi
pi@raspberrypi ~/wiringPi $ ./build

For more information regarding WiringPi please visit The WiringPi Homepage.

The GPIO Utility

The GPIO Utility allows command line access for setting and retrieving current pin modes (input/output) and current values. First do a readall to see the status of all the GPIO pins at once in a handy shell friendly table.

pi@raspberrypi ~ $ gpio readall
+----------+-Rev2-+------+--------+------+-------+
| wiringPi | GPIO | Phys | Name   | Mode | Value |
+----------+------+------+--------+------+-------+
|      0   |  17  |  11  | GPIO 0 | OUT  | High  |
|      1   |  18  |  12  | GPIO 1 | OUT  | High  |
..................................................
|     19   |  30  |   5  | GPIO10 | ALT2 | Low   |
|     20   |  31  |   6  | GPIO11 | ALT2 | Low   |
+----------+------+------+--------+------+-------+

For more information regarding The GPIO Utility please visit The GPIO Utility Homepage.

Web Server

One of the greatest things about using a popular Linux distribution like Rasbian you get instant access to a collection of repositories for installing thousands of packages that often come with optimizations for the given platform your on. Below we will setup a typical LAMP (Linux, Apache, MySQL, PHP/Perl) server, since we already have the Linux part we can continue installing the remaining components.

Using first the sudo (Super User Do) modifier we use apt-get install/remove to install and uninstall various packages. First we install apache2 which will be our web server that responds to requests from web browsers connecting over port 80. apt-get will ask you if you wish to download the resources necessary to install the requested package, any time you see this just type Y on the keyboard indicating Yes.

pi@raspberrypi ~ $ sudo apt-get install apache2

For more information regarding Apache please visit The Apache Homepage.

Next we should install a database server, do so by running the command below. You will be asked to enter a MySQL root password, this can be the same as your current password or something different if you want to be more secure about it. Using complex passwords for all accounts is a good idea if you are going to allow public access to the Raspberry Pi through the internet. This would mean forwarding of ports in your router, if you simply have the Pi connected to your home network and are working with it locally from a laptop or desktop then you can relax a little.

pi@raspberrypi ~ $ sudo apt-get install mysql-server

For more information regarding MySQL please visit The MySQL Homepage.

With the above two packages installed we lastly need to install PHP which will open your Raspberry Pi to a large world of great free scripts that run with PHP. You could even run Wordpress on your Raspberry Pi once PHP is installed. First we grab just PHP5 itself, next the extensions that allows PHP to communicate with MySQL databases. Lastly we install a collection of utilities for PHP that will be needed for GPIO access later on in the article.

pi@raspberrypi ~ $ sudo apt-get install php5
pi@raspberrypi ~ $ sudo apt-get install php5-mysql
pi@raspberrypi ~ $ sudo apt-get install php5-dev

For more information regarding PHP please visit The PHP Homepage.

phpMyAdmin

This step is entirely optional but is highly recommended unless you are already familar with managing databases from the command line and prefer that method instead. Optionally if you never plan on using a MySQL database this can be skipped.

First using the cd command change the current directory to the location setup by Apache to contain files that can be accessed over the web. By default this location is /var/www and can be pointed to another directory if one was so inclined. Next using the wget (Web Get) command we download the archive containing the files necessary to run phpMyAdmin.

pi@raspberrypi ~ $ cd /var/www
pi@raspberrypi /var/www $ sudo wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/4.1.2/phpMyAdmin-4.1.2-all-languages.zip

Using the unzip command we then extract the phpMyAdmin-4.1.2-all-languages.zip file we downloaded in the previous step. Lastly as a point of convenience we rename the unzipped folder to something easier to type in your web browser by using the mv (move) command.

pi@raspberrypi /var/www $ sudo unzip phpMyAdmin-4.1.2-all-languages.zip
pi@raspberrypi /var/www $ sudo mv phpMyAdmin-4.1.2-all-languages phpmyadmin

For more information regarding phpMyAdmin please visit The phpMyAdmin Homepage.

WiringPi PHP

Using an extension developed for WiringPi we can directly access the GPIO pins from a PHP script and create applications that can connect to online services using freely available PHP based libraries and then translate those responses to real world items like bulbs and motors.

With our handy Linux skills we take ourselves back to our home directory and then take a copy of the repository with the WiringPi PHP module.

pi@raspberrypi ~ $ cd ~
pi@raspberrypi ~ $ git clone --recursive https://github.com/WiringPi/WiringPi-PHP.git

Lastly we change the directory to the WiringPi-PHP folder and execute a series of scripts to both compile the code and then to have the WiringPi-PHP module installed so it can be accessed with a PHP script.

pi@raspberrypi ~ $ cd WiringPi-PHP
pi@raspberrypi ~/WiringPi-PHP $ ./build.sh
pi@raspberrypi ~/WiringPi-PHP $ sudo ./install.sh

GPIO Web Utility

WiringPi-PHP comes with a simple test file but to really use the GPIO pins from a web browser you will need something with more options and control. Below is a simple interface I have created with all resources inside the single index.php file for simplicity's sake. Simply use the wget command to download the latest version from the GitHub repository.

pi@raspberrypi ~ $ sudo mkdir /var/www/gpio
pi@raspberrypi ~ $ cd /var/www/gpio
pi@raspberrypi /var/www/gpio $ sudo wget https://raw.github.com/WarriorRocker/wiringpi-web-utility/master/index.php

Once visited in your web browser the Web GPIO Utility provides a simple table interface allowing the user to toggle Pin Modes between Input and Output and set Pin States from Low to High. The utility reads the current pin statuses using the command line utility and uses the WiringPi-PHP functions to set pin modes and value.

Creator

Raspberry Pi PHP GPIO Guide was created by Travis Brown, email: WarriorRocker@gmail.com.