diff --git a/example/ajax-example/_reset.php b/example/ajax-example/_reset.php new file mode 100644 index 0000000..ce387af --- /dev/null +++ b/example/ajax-example/_reset.php @@ -0,0 +1,26 @@ + $val) { + $p = $pi->getPin($pin)->mode(Pin::OUTPUT); + $p->write(Pin::LOW); + $pins[$pin] = $p->read(); +} + +echo json_encode($pins); + diff --git a/example/ajax-example/_set.php b/example/ajax-example/_set.php new file mode 100644 index 0000000..4aa9e98 --- /dev/null +++ b/example/ajax-example/_set.php @@ -0,0 +1,33 @@ + 'Incorrect number of parameters']); +} else if ($argv[1] < 0 || $argv[1] > 3) { + echo json_encode(['error' => 'Incorrect pin numbers selected - only 0-3 valid']); +} else { + foreach ($pins as $pin => $val) { + $p = $pi->getPin($pin)->mode(Pin::OUTPUT); + if ($pin == $argv[1]) { + $p->write((bool)$argv[2] ? Pin::HIGH : Pin::LOW); + } + $pins[$pin] = $p->read(); + } + echo json_encode($pins); +} diff --git a/example/ajax-example/control.php b/example/ajax-example/control.php new file mode 100644 index 0000000..fe1a64f --- /dev/null +++ b/example/ajax-example/control.php @@ -0,0 +1,29 @@ +&1'); + break; + case 'set': + echo shell_exec('sudo php ' . __DIR__ . '/_set.php ' . (int)$_GET['pin'] . ' ' . (int)$_GET['onoff'] . ' 2>&1'); + break; + default: + echo json_encode(['error' => 'Bad function call']); + break; +} diff --git a/example/ajax-example/index.html b/example/ajax-example/index.html new file mode 100644 index 0000000..c9e9987 --- /dev/null +++ b/example/ajax-example/index.html @@ -0,0 +1,158 @@ + + + + +
+ +This example needs to run from your web server and shows that the GPIO pins can be toggled via a web interface using an ajax request.
+When the page loads the pins will be set to 0 and you can toggle from there.
+ +To get this working on the Raspberry Pi, the PHP binary needs to be added to the sudoers file. The easiest way to do this is to use the command:
+sudo visudo+
and then append this at the bottom of the file and save:
+www-data ALL=(ALL) NOPASSWD: /usr/bin/php+ +
Pin reports as 0
+Pin reports as 0
+Pin reports as 0
+