Hardware Reference
In-Depth Information
In this example, we are using three buttons and the name is
appbtn
.
A value is assigned to each button that determines the desired action
when a button is clicked. For example, when a Christmas tree button
is clicked, the lights need to be turned on. This action can be executed
based on the value that is returned during the button press.
5.
The home page is defined in the index class. The
GET
method is used to render the
web page and
POST
for buton click acions.
class index:
def GET(self):
form = appliances_form()
return render.index(form, "Raspberry Pi Christmas lights
controller")
def POST(self):
userData = web.input()
if userData.appbtn == "tree":
global state
state = not state
elif userData.appbtn == "Santa":
#do something here for another appliance
elif userData.appbtn == "audio":
os.system("mpg321 /home/pi/test.mp3")
GPIO.output(25,state)
raise web.seeother('/')
In the
POST
method, we need to monitor the button clicks and perform
an action accordingly. For example, when the button with the
tree
value
is returned, we can change the Boolean value,
state
. This in turn switches
the state of the GPIO pin
25
. Earlier, we connected the power tail switch
to pin
25
.
6. The
index
page ile that contains the form and butons is as follows:
$def with (form,title)
<html>
<head>
<title>$title</title>
<link rel="stylesheet" type="text/css" href="/static/styles.
css">
</head>
<body>
<P><center><H1>Christmas Lights Controller</H1></center>
<br />
<br />
<form class="form" method="post">
$:form.render()
</form>