Hardware Reference
In-Depth Information
Listing 10-4. The on.cgi Script
#!/bin/bash
# Description : cgi script to toggle gpio 4 high -> low
# Author : Brendan Horan
# Required by cgi to set content type
echo "content-type: text/plain"
echo ""
# Pull the pin high
/bin/echo 1 > /sys/class/gpio/gpio4/value
# Sleep for 1 second to allow the remote to receive
sleep 1
# Pull the pin low
/bin/echo 0 > /sys/class/gpio/gpio4/value
# Create a file to indicate current outlet state
/bin/echo power-on > outlet-status
# Give the user a nice message
echo "Power is ON"
echo "Click your browsers back button to return"
This script is very similar to the on function in the power-ctl.sh script. There are two differences. The first is that
you need to tell the server what type of data your CGI script will return. In your case this line sets that:
echo "content-type: text/plain"
This line tells the WEBrick CGI server that this script should return plain text. As you can see, right at the bottom
of Listing 10-4 the CGI script returns plain text.
The second difference is that the script will echo some text back to the web server. In the last two lines you can
see the text that would appear inside your web browser after you click the button.
In Listing 10-5 you can see the off.cgi script.
Listing 10-5. The off.cgi Script
#!/bin/bash
# Description : cgi script to toggle gpio 17 high -> low
# Author : Brendan Horan
# Required by cgi to set content type
echo "content-type: text/plain"
echo ""
# Pull the pin high
/bin/echo 1 > /sys/class/gpio/gpio17/value
 
Search WWH ::




Custom Search