Created
November 18, 2019 06:53
-
-
Save sebastianknopf/8a7811f82799f33b22f0b132327b5733 to your computer and use it in GitHub Desktop.
Control Raspi GPIO with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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