Webinterface to control LED on gpio0

Ton
 
Posts: 22
Joined: Sun Nov 16, 2014 4:35 pm

Webinterface to control LED on gpio0

Tue Nov 18, 2014 9:29 pm

A LED can be connected to the gpio0. See http://vonger.cn/?p=473
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

Vonger
 
Posts: 896
Joined: Sun Oct 19, 2014 6:00 am

Re: Webinterface to control LED on gpio0

Tue Nov 25, 2014 2:23 pm

cgi-bin is good :) Just too slow.

User avatar
milaus
 
Posts: 9
Joined: Thu Nov 20, 2014 4:31 pm
Location: Italy

Re: Webinterface to control LED on gpio0

Sat Dec 20, 2014 5:32 pm

does not work
Attachments
Immagine.png
Immagine.png (114.22 KiB) Viewed 11232 times

Ton
 
Posts: 22
Joined: Sun Nov 16, 2014 4:35 pm

Re: Webinterface to control LED on gpio0

Sun Dec 21, 2014 10:13 am

Did you set gpio0 to output first?
That is assumed.

mozzwald
 
Posts: 1
Joined: Wed Dec 24, 2014 11:58 pm

Re: Webinterface to control LED on gpio0

Thu Dec 25, 2014 12:04 am

I used lighttp and php to create simple web interface for gpio/relay/outlet control. not the most efficient but it works. Shell script runs in background to watch switches attached to gpio.

https://mozzwald.com/articles/2014/12/2 ... ith-vocore

Ton
 
Posts: 22
Joined: Sun Nov 16, 2014 4:35 pm

Re: Webinterface to control LED on gpio0

Mon Dec 29, 2014 4:17 pm

Hi mozzwald, neat way to use the vocore.

How did you install php and lighttpd, any caveats you can share?

I wonder what extras you are going to add. For now php seems a bit heavy to do just this.

Return to VoCore & VoCore+Dock

Who is online

Users browsing this forum: No registered users and 44 guests