2017 © Pedro Peláez
 

library php-gpio

GPIO-related utils & toolkit PHP library

image

tgeindre/php-gpio

GPIO-related utils & toolkit PHP library

  • Wednesday, June 15, 2016
  • by t-geindre
  • Repository
  • 3 Watchers
  • 2 Stars
  • 40 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 43 % Grown

The README.md

PHP-GPIO master build

A simple library to read/write Raspberry PI GPIOs with PHP., (*1)

Forked from ronanguilloux/php-gpio and tested with Atoum., (*2)

Install

This library can be installed using Composer:, (*3)

$ composer require tgeindre/php-gpio

Read/Write Raspberry Pi GPIOs

To read/write Raspberry Pi GPIOs, use the PhpGpio\Gpio class. The instanciation of this class requires an array of pins numbers you will use., (*4)

You can define a specific list of pins numbers, according to your usage and your Rasberry Pi version, or you can use the PhpGpio\Utils\Pi class to automaticly find all availables pins:, (*5)

<?php
namespace myproject;

$pi = new PhpGpio\Utils\Pi;
$pi->getAvailablePins(); // int array

Accessing to the GPIOs requires root permissions, so make sure your code is running with enought permissions. Remember: you should never run your webserver as root., (*6)

Here is a simple example of Gpio class usage:, (*7)

<?php
namespace MyProject;

use PhpGpio\GpioInterface;
use PhpGpio\Gpio;

// Both pins are available on all raspi versions
define('PIN_IN', 4);
define('PIN_OUT', 7);

$gpio = new Gpio([PIN_IN, PIN_OUT]);

// First, setup pins with correct directions
$gpio
     ->setup(PIN_IN, GpioInterface::DIRECTION_IN) // Makes it readable
     ->setup(PIN_OUT, GpioInterface::DIRECTION_OUT) // Writeable
;

// read PIN_IN value and display it
$value = $gpio->read(PIN_IN);
var_dump($value); // string

// write 1 on PIN_OUT
$gpio->write(PIN_OUT, GpioInterface::IO_VALUE_ON);
sleep(1);

// After 1 second, write 0 on PIN_OUT
$gpio->write(PIN_OUT, GpioInterface::IO_VALUE_OFF);

// Then free all pins
// (use the unexport() method to free pins one by one)
$gpio->unexportAll();

Check this page if you need a complete list of availables pins on your Raspberry Pi version., (*8)

Demo

Here is a simple demo using a Raspberry Pi 3 and some LED (click on the image to see the video):, (*9)

Demo video, (*10)

Source code, (*11)

Sensors

Currently, this library only supports MCP analogic to digital converter (ADC) with 4 or 8 channels:, (*12)

MCP3002, MCP3004 & MCP3008

mcp3004, (*13)

Given that the Rapsberry Pi supports only digital inputs, an ADC, such as a MCP, is required to read analogic data., (*14)

MCP3002, MCP3004 and MCP3008 work the same way but have dedicated classes for each version: * PhpGpio\Sensor\Mcp\Mcp3002 * PhpGpio\Sensor\Mcp\Mcp3004 * PhpGpio\Sensor\Mcp\Mcp3008, (*15)

It use a SPI interface wich requires, at least, 4 pins: * MISO - Master Input Slave Ouput, * MOSI - Master Output Slave Input, * CLK - Clock, * CS - Channel Select (one dedicated pin for each component using the same SPI interface)., (*16)

Provided classes use a software communication system so you can use any pins you wich., (*17)

Here is the connection schema of those 3 versions of MCP ADC:, (*18)

mcp, (*19)

Wiring: * VDD__ (power supply) on 3.3V, * __VREF on 3.3v, * AGND on ground (GND), * CLK on clock pin, * DOUT__ (digital output) on MISO pin, * __DIN__ (digital input) on MOSI pin, * __CS__ (channel select) on CS pin, * __DGND on ground (GND)., (*20)

Now you are ready to read some data:, (*21)

namespace MyProject;

use PhpGpio\Gpio;
use PhpGpio\Sensor\Mcp\Mcp3008;

// Defining pins mapping according to your setup
$pinsMapping = [
    'MISO' => 17,
    'MOSI' => 8,
    'CLK' => 23,
    'CS' => 24,
];

// Setup a Gpio class
$gpio = new Gpio(array_values($pins));

// Then we can instanciate the MCP class
$mcp = new Mcp3008(
    $gpio
    $mapping['CLK'],
    $mapping['MOSI'],
    $mapping['MISO'],
    $mapping['CS']
);

// Now let's read some data on the first channel
while (true) {
    echo $mcp->read(['channel' => 0]), "\n";
    // every second
    sleep(1);
}

The Versions

15/06 2016

dev-feature/add-gpio-server

dev-feature/add-gpio-server https://github.com/t-geindre/php-gpio

GPIO-related utils & toolkit PHP library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thierry Geindre

output gpio raspberry input pin hardware raspberry pi port sensors chipset chip

14/06 2016

dev-master

9999999-dev https://github.com/t-geindre/php-gpio

GPIO-related utils & toolkit PHP library

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Thierry Geindre

output gpio raspberry input pin hardware raspberry pi port sensors chipset chip

14/06 2016

v1.0.0

1.0.0.0 https://github.com/t-geindre/php-gpio

GPIO-related utils & toolkit PHP library

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Thierry Geindre

output gpio raspberry input pin hardware raspberry pi port sensors chipset chip

30/05 2016

v0.0.1

0.0.1.0 https://github.com/t-geindre/php-gpio

GPIO-related utils & toolkit PHP library

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Thierry Geindre

output gpio raspberry input pin hardware raspberry pi port sensors chipset chip