To switch the LED on and off through a webinterface, we can use Ye Olde cgi-bin.
(Set the gpio0 to output first, this script assumes that.)
- Code: Select all
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>LED Example</title>'
echo '</head>'
echo '<body>'
echo '<h2>Lightcontrol, switch LED on gpio0</h2><hr/>'
echo "<br/><form method=GET action=\"${SCRIPT}\">"\
'<button type="submit" name="But1" value="On">On</button>'\
'<button type="submit" name="But2" value="Off">Off</button>'\
'</form><hr/>'
# Make sure we have been invoked properly.
if [ "$REQUEST_METHOD" != "GET" ]; then
echo "<hr>Script Error:"\
"<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."\
"<br>Check your FORM declaration and be sure to use METHOD=\"GET\".<hr>"
exit 1
fi
# No looping this time, just extract the data you are looking for with sed:
XX=`echo "$QUERY_STRING" | sed -n 's/^.*But1=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
YY=`echo "$QUERY_STRING" | sed -n 's/^.*But2=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
# echo "val_x: " $XX
# echo '<br>'
# echo "val_y: " $YY
# echo '<br>'
# echo "val_z: " $ZZ
if [ "$XX" = "On" ]; then
echo 1 > /sys/class/gpio/gpio0/value
fi
if [ "$YY" = "Off" ]; then
echo 0 > /sys/class/gpio/gpio0/value
fi
echo 'The LED is now: '
ZZ=`cat /sys/class/gpio/gpio0/value`
if [ "$ZZ" = "1" ]; then
echo 'On'
else
echo 'Off'
fi
echo '<br></body></html>'
Put this in /www/cgi-bin, named LED for instance. then chmod 755 LED to make it executable.
Now go to http://Vocore-IP-number/cgi-bin/LED and switch it on and off.
For more info on cgi-bin scripting, see http://www.yolinux.com/TUTORIALS/BashShellCgi.html
Above script is based on that article