Control from the web

Ahh. I see the light.I have shamelessly borrowed this from one of my Camosun labs.Dark in here isn't it.


Objectives


Example

onoff.html

Concepts

Or, your life will become much more complex. See
        Velociraptor.

Code

Web form - this sends the request, on or off

<html>
<head>
<title>Turn something on or off</title>
</head>
<body>
<h1>Turn Something On and Off</h1>
<form action="/cgi-bin/onoff" method="post">
ON: &nbsp;<input name="onoff" value="On" type="radio" ><br>
OFF: <input name="onoff" value="Off" type="radio" ><br>
<input type="submit">
</form>
</body>
</html>


Wrapper - this allows the suid cgi script to be executed securely

#define REAL_PATH "/usr/lib/cgi-bin/onoff.py"
      main(ac, av)
          char **av;
      {
          execv(REAL_PATH, av);
      }


python back end - this does the work


#! /usr/bin/python

# Import the Python gpio library and the system library
import RPi.GPIO as GPIO
import sys

print "Content-type: text/html\n\n"
print "<html><head><title>This is a switch</title></head"

# Build dictionary of post inputs.
POST = {}
args = sys.stdin.read().split('&')
for arg in args:
        t = arg.split('=')
        POST[t[0]] = t[1]

# Get the input value from the POST[] array
inp = POST['onoff']

# Determine on or off and which message to display.
if inp == "On":
  print "<h1>Turned On</h1>"
  onOff = 1
elif inp == "Off":
  print "<h1>Turned Off</h1>"
  onOff = 0
else:
  print "How do I do that?"

# Turn off warnings
GPIO.setwarnings(False)

# This is the gpio pin I have the led connected to
pin =  5

#  Set the pin number mode to Broadcom rather than connector order
GPIO.setmode(GPIO.BCM)

# Set the choosen pin to be an output and set the ouput true, on.
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, onOff)

# Display the state of the pin.
print GPIO.input(pin);



References


Quiz