Second LanguageGetting started with PHP on Raspberry Pi is easy. We show how to build a simple PHP app to control an LED. Python and Scratch are by far the most popular options for programming Raspberry Pi. With a bit of work, however, you can teach Raspberry Pi to understand other languages, including PHP. You might want to use PHP on Raspberry Pi for several reasons. If you've already built a PHP-based app, and you want to integrate it with Raspberry Pi, for example, it makes sense to stick to PHP. Perhaps you are more familiar with writing code in PHP than Python, or maybe you just want to give another programming language a try. Before you can start working with PHP on Raspberry Pi, you need to install PHP itself on the machine. In most situations, PHP is used in tandem with a web server like Apache. Installing both PHP and Apache on Raspberry Pi is a matter of running: sudo apt-get install apache2 php5 Instead of Apache, you can install a more lightweight server like lighttpd using the sudo apt-get install lighttpd php5 command. Building BridgesProbably the easiest way to use PHP with Raspberry Pi is through the Another approach is to deploy the Wiring Pi library
[1] for working with GPIO pins and then use the library with PHP via the sudo apt-get install git-core Then, clone the Wiring Pi Git repository by running git clone git://git.drogon.net/wiringPi Switch to the resulting cd wiringPi ./build To make sure Wiring Pi is installed and works properly, run the To put Wiring Pi to practical use, I'll build a super-simple
PHP app for controlling an LED. Connect an LED with a resistor to GPIO pin 17 and GND as shown in Figure 1. Open the terminal on your Raspberry Pi (or connect to it via SSH) and switch to the Listing 1 Simple PHP App to Control an LED 01 <html> 02 <head> 03 <meta name="viewport" content="width=device-width" /> 04 <title>LED Control</title> 05 </head> 06 <body> 07 LED Control: 08 <form method="get" action="gpio.php"> 09 <input type="submit" value="ON" name="on"> 10 <input type="submit" value="OFF" name="off"> 11 </form> 12 <?php 13 $setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out"); 14 if(isset($_GET['on'])){ 15 $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1"); 16 echo "LED is on"; 17 } 18 else if(isset($_GET['off'])){ 19 $gpio_off = shell_exec("/usr/local/bin/gpio -g write 17 0"); 20 echo "LED is off"; 21 } 22 ?> 23 </body> 24 </html> Figure 1: LED wiring diagram. The key element of the app is an HTML form containing Now, point the browser to http://127.0.0.1/gpio.php (replace 127.0.0.1 with the actual IP address of the Raspberry Pi) and use the buttons to turn the LED connected to the Raspberry Pi on and off (Figure 2). Figure 2: Behold the simple PHP app in all its bare-bones glory. If the buttons don't work, most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the www-data ALL=NOPASSWD: ALL Also, make sure that the Even this very simple PHP app can be put to several uses with a minimum of tweaking. For example, I use a simple transistor switch connected to Raspberry Pi to control my film SLR camera [3]. The original solution used a simple Python script to control the switch. To run the script, I had to do this via an SSH connection, which wasn't very practical in many situations. So, I modified the example PHP script for use with the transistor switch (Listing 2). Listing 2 PHP App to Control a Transistor Switch 01 <html> 02 <head> 03 <meta name="viewport" content="width=device-width" /> 04 <title>Trigger</title> 05 </head> 06 <body> 07 Trigger switch: 08 <form method="get" action="switch.php"> 09 <input type="submit" value="Trigger" name="switch"> 10 </form> 11 <?php 12 $setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out"); 13 if(isset($_GET['switch'])){ 14 $gpio_off = shell_exec("/usr/local/bin/gpio -g write 17 1"); 15 sleep (0.5); 16 $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 0"); 17 echo "Done!"; 18 } 19 ?> 20 </body> 21 </html> All I had to do was remove one of the buttons and modify commands in the Taking the gpio-php RouteAlthough using Wiring Pi via wget http://getcomposer.org/composer.phar php composer.phar create-project --stability='dev'ronanguilloux/php-gpio php-gpio uses a handful of simple API calls to set GPIO pins and change their states from a PHP script. To enable this functionality, however, the script must contain the following statements that load the library and set up a GPIO pin: require 'vendor/autoload.php'; use PhpGpio\Gpio; $gpio = new GPIO(); $gpio->setup(17, "out"); In this example, the last statement sets GPIO pin 17 for output. Controlling the pins requires two more commands:
The first sets the pin's state to Using these commands, you can quickly whip up a simple PHP script that blinks the LED connected to GPIO pin 17 (Listing 3). Listing 3 Simple PHP Script to Blink LED 01 <?php 02 require 'vendor/autoload.php'; 03 use PhpGpio\Gpio; 04 $gpio = new GPIO(); 05 $gpio->setup(17, "out"); 06 while (true){ 07 $gpio->output(17, 1); 08 sleep(1); 09 $gpio->output(17, 0); 10 sleep(1); 11 } To run the script, execute the The solution is simple: Use the The only problem is that the entire PHP app pauses while the called script runs. In the case of the blinking LED script, that might not be an issue, but it could be a serious problem if you call a script that takes longer to complete. Fortunately, an easy fix is available. You can run the script in the background and discard output by redirecting the script: shell_exec('sudo php path/to/php-gpio/\ blink_led.php > /dev/null 2> /dev/null &'); Again, because all PHP scripts on Raspberry Pi must be run with root privileges, you need to add the The php-gpio library can be put to other, more advanced uses, too. The php-gpio-web GitHub repository, for example, contains a simple web app [5] that demonstrates how to create a web interface to control an LED, and the temperature-pi project [6] shows how to read and log data from a temperature sensor. In this article, I briefly outlined the possible approaches to using PHP on Raspberry Pi. And the example scripts offer a few pointers on controlling GPIO via PHP scripts. So if you want to use PHP with Raspberry Pi, you now know where to start. Infos
The Author Dmitri Popov has been writing exclusively about Linux and open source software for many years, and his articles have appeared in Danish, British, US, German, Spanish, and Russian magazines and websites. Dmitri is an amateur photographer, and he writes about open source photography tools on his Scribbles and Snaps blog at http://scribblesandsnaps.wordpress.com. |