Powering Raspberry Pi Projects with PHP
A Raspberry Pi is a brilliant tiny computer that you can power off of a micro USB cable. The most recent model has WiFi, an ethernet port, 4 USB ports and an HDMI port. There’s also a micro SD card slot, which is where the “hard drive” goes.
It’s capable of running Raspbian Linux, which is a Debian based Linux distribution. This makes it familiar to Ubuntu users who can then just sudo apt-get install
all the things.
Like with any Linux machine, you can install PHP on it and make a website – but we can do so much more than that!
Equipment
Firstly, we will need a Raspberry Pi. The most recent model has onboard WiFi, which is very useful for easy SSH access. We will also need some electronics equipment. It is a really good idea to obtain a starter kit containing a breadboard, jumper wires, resistors, LEDs and push buttons. You can buy these sorts of kits on adafruit and similar packs can be found much cheaper on Amazon and eBay.
Setup
The Raspberry Pi official website has an excellent quick start guide to help you get up and running. Once you have Raspbian Linux running on your machine, you can use this command to install PHP:
sudo apt-get install php5
Alternatively, install PHP7. It’s a bit more complicated, but performs better. This guide explains it well.
General Purpose Input Output (GPIO) Pins
On the corner of a Raspberry Pi board are two rows of metal pins that we can connect electronic circuits to. Some of the pins are grounded, some are 5V, some are 3.3V and most of them are GPIO pins.
Credit: www.raspberrypi.org
When a GPIO pin is configured as an output pin, we can tell the Pi to set its voltage to be high (3.3V) or low (0V). This allows us to turn things on and off.
When a GPIO pin is configured as an input pin, we can check to see the voltage on that pin and also detect when that voltage changes. This allows us to read sensors and also detect things like button presses.
There is plenty more that we can use these pins for, such as communication via Serial, i2c or SPI – but for now, this is all we are required to know.
Getting Started with PiPHP
PiPHP: GPIO is a PHP library that can be installed via composer that allows you to control these GPIO pins. Here is an example of how we might use the library to blink an LED a few times when a button press is detected. The diagram below shows how you could connect an LED to GPIO pin 2 and a push button to GPIO pin 3 using a breadboard. A breadboard is a reusable base for making electronic circuits. The rows are electronically connected, as are the two rails that span the length of the board on each of its sides. We connect the first two rails to the ground and 3.3V pins in the Raspberry Pi.
The two resistors are important. The first resistor (~220 Ω) limits the current to the LED. The second resistor (~10 kΩ) is a pull-up resistor for the button.
Note: Some of the pins on the Raspberry Pi have internal pull-up resistors, so this is not always required (but it does not harm).
Back on the Raspberry Pi, make yourself a project directory and then use composer to install piphp/gpio
:
composer require piphp/gpio
If you place the file below inside your project directory and then execute it (php led-blink.php
) you should find that pressing the button causes the LED to blink 5 times, as it does in the video.
<?php // led-blink.php
require_once 'vendor/autoload.php';
use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\InputPinInterface;
use PiPHP\GPIO\Pin\OutputPinInterface;
// This GPIO object can be used to retrieve pins and create interrupt watchers
$gpio = new GPIO();
// Configure pin 2 as an output pin and retrieve an object that we can use to change it
$ledPin = $gpio->getOutputPin(2);
// Configure pin 3 as an input pin and retrieve an object that we can use to observe it
$buttonPin = $gpio->getInputPin(3);
// Configure this pin to trigger interrupts when the voltage rises.
// ::EDGE_FALLING and ::EDGE_BOTH are also valid.
$buttonPin->setEdge(InputPinInterface::EDGE_RISING);
// Create an interrupt watcher (this is a type of event loop)
$interruptWatcher = $gpio->createWatcher();
// Register a callback for handling interrupts on the button pin
$interruptWatcher->register($buttonPin, function () use ($ledPin) {
echo 'Blinking LED...' . PHP_EOL;
// Toggle the value of the LED five times
for ($i = 0; $i < 5; $i++) {
$ledPin->setValue(OutputPinInterface::VALUE_HIGH);
usleep(100000);
$ledPin->setValue(OutputPinInterface::VALUE_LOW);
usleep(100000);
}
// Returning false would cause the loop below to exit
return true;
});
// Loop until an interrupt callback returns false, this code will iterate every 5 seconds
while ($interruptWatcher->watch(5000));
Going Further
We have seen how we can use PHP to respond to electronic inputs and control electronic outputs. Once we have this understanding, we can get more and more adventurous with our inputs and outputs to create more and more awesome projects.
Speaking personally, I had great fun making a drinks machine using a flow sensor and a windscreen washer fluid pump.
Talk preparation for @phpday could be going better. pic.twitter.com/Z9IWGEQftq
— Andrew Carter (@AndrewCarterUK) May 13, 2016
I used some nifty electronics with a TIP120 transistor so that I could control the higher voltage and current that the pump needed from the Raspberry Pi. The flow sensor outputs a pulse after a specific volume of fluid has flown through it. All my code had to do was count the interrupts from the flow sensor and then turn the pump off. The gardeners amongst us could use a similar arrangement to automate the watering of plants!
Have any Raspberry PI + PHP projects you’d like to share? Let us know!