Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Created November 18, 2019 06:53
Show Gist options
  • Save sebastianknopf/8a7811f82799f33b22f0b132327b5733 to your computer and use it in GitHub Desktop.
Save sebastianknopf/8a7811f82799f33b22f0b132327b5733 to your computer and use it in GitHub Desktop.
Control Raspi GPIO with PHP
<?php
define('GPIO_HIGH', 1);
define('GPIO_LOW', 0);
function gpio_exec($cmd)
{
$output = array();
$return = 0;
exec($cmd, &$output, &$return);
return [$return, implode('\n', $output)];
}
function gpio_export($pin)
{
gpio_exec('echo "' . $pin . '" > /sys/class/gpio/export');
}
function gpio_unexport($pin)
{
gpio_exec('echo "' . $pin . '" > /sys/class/gpio/unexport');
}
function gpio_in($pin)
{
gpio_exec('echo "in" > /sys/class/gpio' . $pin . '/direction');
}
function gpio_out($pin)
{
gpio_exec('echo "out" > /sys/class/gpio' . $pin . '/direction');
}
function gpio_write($pin, $value)
{
gpio_exec('echo "' . $value . '" > /sys/class/gpio' . $pin . '/value');
}
function gpio_read($pin)
{
$result = gpio_exec('echo /sys/class/gpio' . $pin . '/value');
if($result[1] == '1') {
return GPIO_HIGH;
} else {
return GPIO_LOW;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment