WiredPi
, (*1)
WiredPi is a library for connecting php and the Raspberry Pi GPIO via wiringPi., (*2)
Dev branch is master branch.
, (*3)
Table of Contents
- Installation
-
Usage
, (*4)
Installation
The recommended way to install is through Composer., (*5)
{
"require": {
"se/wired-pi": "dev-master"
}
}
WiredPi uses internally the WiringPi library. A big thank you @drogon. To install it follow the following steps:, (*6)
$ cd /opt
$ sudo mkdir wiringpi
$ sudo chown $USER ./wiringpi
$ cd wiringpi
$ git clone git://git.drogon.net/wiringPi ./
$ git pull origin
$ ./build
(The package git-core is needed for git operations. Install it via sudo apt-get install git-core. ), (*7)
, (*8)
Usage
Basic usage
Require the composer autload file and create a new Board., (*9)
require_once __DIR__.'/vendor/autoload.php';
use \SE\Component\WiredPi;
$platform = new WiredPi\Platform\RaspberryPi();
$board = new WiredPi\Board($platform);
The next step is to apply a port map to the port so it knows what pins can be controlled., (*10)
$map = new WiredPi\PortMap(array(
// number of the GPIO-pin,
18
// you can set default options too, STATE_ON turns the pin by default on
23 => array(WiredPi\Port::STATE => WiredPi\Port::STATE_ON)
));
Now add the port map to your board instance and refresh it. The settings take effect immediately., (*11)
$board->setPorts($map);
$board->refresh(); // applies the current state to the microcontroller
// Modification of ports need to be applied again
$board->getPort(18)->on();
$board->refresh();
Read pin
For reading the status of your pin you can use the platform instance you passed to the board., (*12)
$port = $board->getPort(18);
$status = $platform->read($port); // returns 0 or 1
By default the pins are set to OUT., (*13)
For receiving the status of a pin from IN set the mode to IN., (*14)
$port = $board->getPort(18);
$port->setMode(WiredPi\Port::MODE_IN);
// Do something when Pin switches to 1
while(true) {
if($platform->read($port) == '1') {
print sprintf('Pin %s went to %s', $port, '1')
break;
}
usleep(5000); // Let the system catch up
}
Prototyping server
WiredPi includes a protyping server to control pins without the need to setup them from within a php script.
Run it by using the built in php server (Since PHP 5.4.0)., (*15)
$ php -S localhost:8000 scripts/server.php
, (*16)
For more examples have a look at examples/., (*17)