I ported the original Python library to PHP. This library works almost the same as the python library.
- Hardware: Raspberry PI and PiFace module.
- PHP 5.4+ installed
- You also need to add the php SPI extension, compile it and configure, see: https://github.com/frak/php_spi
- Apache if you want to run commands from web scripts.
Don't worry, follow the guides below to get started:
Few things you need to install your environment, here is some instructions.
# Install php + git
sudo apt-get install php5 php5-dev php5-cli git
# Install php SPI
git clone git://github.com/frak/php_spi.git ~/php_spi
cd ~/php_spi
phpize
./configure --enable-spi
make
make test
sudo make install
Install with Composer:
Here is some commands to your new project in ~/myphpiface
(in your home directory):
mkdir ~/myphpiface
cd ~/myphpiface
touch ~/myphpiface/composer.json
echo '{ "require": { "pkj/raspberry-piface-api": "dev-master" } }' >> ~/myphpiface/composer.json
curl -sS https://getcomposer.org/installer | php
php composer.phar install
This will create a vendor
directory where the library is installed.
Create a PHP script named test_piface.php
(We assume that you place test_piface.php
where composer.json
file is.) and paste this in the file:
use Pkj\Raspberry\PiFace\PiFaceDigital;
dl("spi.so"); // Load the SPI extension.
require 'vendor/autoload.php';
$dev = PiFaceDigital::create();
// Run once.
$dev->init();
$dev->getLeds()[0]->turnOn();
sleep(2);
$dev->getLeds()[0]->turnOff();
// $dev->getInputPins();
// $dev->getOutputPins();
// $dev->getLeds();
// $dev->getRelays();
// $dev->getSwitches();
// Turn on relay 0
$dev->getRelays()[0]->turnOn();
// Get 0/1 of input pin 3 (There are 8 pins, 0-7)
$dev->getInputPins()[3]->getValue();
// Toggle a value on a output pin (5 in this example)
$dev->getOutputPins()[5]->toggle(); // 0
$dev->getOutputPins()[5]->toggle(); // 1
$dev->getOutputPins()[5]->toggle(); // 0
And test:
php -f test_piface.php