Ich hab mir ein PHP Script geschrieben um die Richtung und den Wert der GPIO Pins ändern zu können...
Dazu gesagt habe ich erst später (wurde hier in Post#2 erwähnt) erfahren das es sowas bereits gibt, fand es aber trotzdem interessant das selber zu programmieren
Vorraussetzungen:
Spoiler anzeigen
- wiringPi
- Ein zusätzliches Script "sudowebscript.sh" , was dem "www-data" Benutzer erlaubt bestimmte Befehle mit root -Rechten auszuführen:
/var/sudowebscript.shBash
Alles anzeigen#!/bin/bash # # sudo web script allowing user www-data to run commands with root privilegs case "$1" in rpirev) gpio -v | grep "Raspberry Pi is a revision" | awk {'print $7'} ;; gpio) gpio $(echo $@ | cut -d' ' -f2-) ;; gpiounexport) echo $2 > /sys/class/gpio/unexport ;; gpiodirection) echo $2 > /sys/class/gpio/gpio${3}/direction ;; gpiovalue) echo $2 > /sys/class/gpio/gpio${3}/value ;; *) echo "ERROR: invalid parameter: $1 (for $0)"; exit 1 ;; esac exit 0
- Das Script " sudowebscript.sh " muss in /etc/sudoers eingetragen werden:
Das eigentliche PHP Script:
/var/www/GPIO.php
PHP
<!DOCTYPE html>
<html>
<head>
<title>GPIO PINs Konfigurieren</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
// v0.4
$_SELF=$_SERVER['PHP_SELF'];
$PREcmd = 'sudo ';
$SudoWebScript = '/var/sudowebscript.sh ';
# RaspberryPI Rev1 GPIO pin's
$GPIO["1"] = "0,1,4,17,21,22,10,9,11,18,23,24,25,8,7";
# RaspberryPI Rev2 GPIO pin's
$GPIO["2"] = "2,3,4,17,27,22,10,9,11,18,23,24,25,8,7";
$DIRECTIONS = "in/out/pwm/clock/up/down/tri";
#debug
#showarray($_POST);
/*
echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "1" > /sys/class/gpio/gpio17/value
*/
if (isset($_POST) AND !empty($_POST)) {
if (isset($_POST["unexportall"])) {
exec(''.$PREcmd.''.$SudoWebScript.'gpio unexportall',$output,$error);
} else {
$newPIN = $_POST["PIN"];
$newDIRECTION = $_POST["DIRECTION"];
$newVALUE = $_POST["VALUE"];
if (!file_exists("/sys/class/gpio/gpio".$newPIN."")) {
# export
exec(''.$PREcmd.''.$SudoWebScript.'gpio export '.$newPIN.' '.$newDIRECTION.'',$output,$error);
}
# Direction
exec(''.$PREcmd.''.$SudoWebScript.'gpio mode '.$newPIN.' '.$newDIRECTION.'',$output,$error);
# Value
exec(''.$PREcmd.''.$SudoWebScript.'gpiovalue '.$newVALUE.' '.$newPIN.'',$output,$error);
#checkexec($output,$error);
}
if ((isset($error) AND !empty($error)) OR (isset($output) AND !empty($output))) {
$status = "ERROR";
$err="";
foreach($output AS $line) { $err.="$line<br/>\n"; }
} else {
$status = "OK";
}
if ($status == "OK") { $fontcolor="009900"; }
if ($status == "ERROR") { $fontcolor="FF0000"; }
echo ("<font face='Arial, Helvetica, sans-serif' size='3' color='".$fontcolor."'>".$status."</font><br/>".$err."<br/>\n");
}
# get RaspberryPI Revision
exec(''.$PREcmd.''.$SudoWebScript.'rpirev',$output,$error);
$RPIrevision = $output[0];
echo "<font face='Arial' size='3' color=FF0000><b>RaspberryPI rev".$RPIrevision.":</b></font>\n";
echo "<table class='GPIO' border=1 cellpadding=3 bordercolorlight>\n";
echo "<tr>\n";
echo "<th class='header' align='center' valign='middle'>PIN</th>\n";
echo "<th class='header' align='center' valign='middle'>DIRECTION</th>\n";
echo "<th class='header' align='center' valign='middle'>VALUE</th>\n";
echo "<th class='header' align='center' valign='middle'>ACTIVE</th>\n";
echo "<th class='header' align='center' valign='middle'> </th>\n";
echo "</tr>\n";
foreach (explode(",",$GPIO[$RPIrevision]) AS $pin) {
if (file_exists("/sys/class/gpio/gpio".$pin."")) {
$fontcolor="009900";
$ACTIVE = "Yes";
$Direction = trim(file_get_contents("/sys/devices/virtual/gpio/gpio".$pin."/direction"));
$Value = trim(file_get_contents("/sys/devices/virtual/gpio/gpio".$pin."/value"));
} else {
$ACTIVE = "No";
$fontcolor="FF0000";
$Direction = "";
$Value = "";
}
echo "<tr>\n";
echo "<form method='POST' id='GPIO' name='GPIOform' action='".$_SELF."'>\n";
echo "<input type='hidden' name='PIN' value='".$pin."'>\n";
echo "<td class=tab align='right' valign='middle'><b>".$pin."</b></td>\n";
echo "<td class=tab align='middle' valign='middle'><select name='DIRECTION'>\n";
foreach (explode("/",$DIRECTIONS) AS $DIR) {
if ($Direction == $DIR) {
echo '<option value="'.$DIR.'" selected="selected">'.strtoupper($DIR).'</option>';
} else {
echo '<option value="'.$DIR.'">'.strtoupper($DIR).'</option>';
}
}
echo "</select></td>\n";
echo "<td class=tab align='right' valign='middle'><select name='VALUE'>\n";
if ($Direction == "pwm") {
for ($i = 0; $i <= 1023; $i++) {
if ($Value == $i) {
echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
} else {
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
} else {
if ($Value == 1) { echo '<option value="1" selected="selected">1</option>'; } else { echo '<option value="1">1</option>'; }
if ($Value == 0) { echo '<option value="0" selected="selected">0</option>'; } else { echo '<option value="0">0</option>'; }
}
echo "</select></td>\n";
echo "<td class=tab align='middle' valign='middle'><font face='Arial, Helvetica, sans-serif' size='2' color='".$fontcolor."'>".$ACTIVE."</font></td>";
echo "<td><input id='Set' type='submit' value='Set' class='Submit'></td>\n";
echo "</form>\n";
echo "</tr>\n";
}
echo "</table><br/>\n";
# unexport all
echo "<form method='POST' id='GPIO' name='GPIOresetform' action='".$_SELF."'>\n";
echo "<input type='hidden' name='unexportall' value='unexportall'>\n";
echo "<input id='Reset' type='submit' value='unexport all' class='Submit'>\n";
echo "</form>\n";
//______________________________________________________________________________________
function checkexec($output,$error="") {
global $output,$error;
if (!empty($output) OR !empty($error)) {
echo "<b style='font-size:13px; text-align:left; color:#FF0000;'>Returncode: $error</b><br/>\n";
echo "<pre><b style='font-size:13px; text-align:left;'>\n";
var_dump($output);
echo "</b></pre>\n";
flush();
unset($output);
unset($error);
}
}
function showarray($array) {
echo "<pre><b style='font-size:13px; text-align:left; color:#c8c8c8;'>\n";
var_dump($array);
echo "</b>\n";
flush();
}
?>
</body>
</html>
Alles anzeigen
Allerdings habe ich auch als root ein seltsames Problem