Hardware Reference
In-Depth Information
# set the pins to low to start with
function setlow {
echo 0 > /sys/class/gpio/gpio4/value
echo 0 > /sys/class/gpio/gpio17/value
}
check
export
direction
setlow
This script is separated into four functions.
The first function checks to see if the script has already run by checking for the presence of the
exported gpio4 directory.
The second function exports the GPIO pins, exactly like you did on the command line.
After that, the next function sets the direction of the pins: once again, this is nothing you have
not done before.
The last section is not really necessary but I like to set the GPIO pins to a known state.
You can now use this script to set up the GPIO pins automatically. The gpio-setup.sh script takes no command-line
arguments and will exit quietly upon failure or success.
Next up, take a look at Listing 10-3.
Listing 10-3. The Simple HTML Page to Control the GPIOs
<!--
Description : Simple HTML page to activate the cgi scripts
Author : Brendan Horan
-->
<html>
<head>Pi Power Control</head>
<body>
<form action="on.cgi">
<p>Turn On: <button>ON</button>
</form>
<form action="off.cgi">
<p>Turn Off: <button>OFF</button>
</form>
</body>
</html>
As you can see, there is not much to this index page at all. You have two buttons that run either the on.cgi or the
off.cgi scripts.
Next up will be the two common gateway interface (CGI) scripts because I have just referred to them. Ruby's
WEBrick HTTP server by default supports CGI scripts. The CGI is a method for a web server to access and run a script
on itself. I will be using a bash script to form my CGI scripts. Take a look at Listing 10-4 to see the on.cgi script.
 
Search WWH ::




Custom Search