se/wired-pi

WiredPi is a library for connecting php and the Raspberry Pi GPIO via wiringPi

v0.1-beta 2013-12-01 15:47 UTC

This package is not auto-updated.

Last update: 2024-11-05 02:18:05 UTC


README

Latest Stable Version SensioLabsInsight

WiredPi is a library for connecting php and the Raspberry Pi GPIO via wiringPi.

Dev branch is master branch.

Build Status

Table of Contents
  1. Installation
  2. Usage

Installation

The recommended way to install is through Composer.

{
    "require": {
        "se/wired-pi": "dev-master"
    }
}

WiredPi uses internally the WiringPi library. A big thank you @drogon. To install it follow the following steps:

$ 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. )

Usage

Basic usage

Require the composer autload file and create a new Board.

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.

$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.

$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.

$port = $board->getPort(18);
$status = $platform->read($port); // returns 0 or 1

By default the pins are set to OUT.

For receiving the status of a pin from IN set the mode to IN.

$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).

$ php -S localhost:8000 scripts/server.php

For more examples have a look at examples/.