1

I need help with writing code on python using web servers. I want to know when a button is clicked how to tell a LED to be turned on and to relay that information back to the web server displaying if the LED is on or off. Here is my current code. import web

global status
status = [0]

render = web.template.render('web/')

urls = ( '/', 'hello')
app = web.application(urls, globals(), True)

class hello:
    def GET(self):
        getInput = web.input(turn="")
        command = str(getInput.turn)

        return """<html>
<head>
<title>Control Centre  </title>
</head>
<body>
<font size="20"> </font>
<p> <font size="20">  <b>Control  Centre</b> </font>  </p>
</body>
<p> <button type="submit1">LED 1</button> </p>
<p> <button type="submit2">LED 2</button> </p>
<p> <button type="submit3">LED 3</button> </p>
<p> <button type="submit4">LED 4</button> </p>
<p> <button type="submit5">LED 5</button> </p>
<p> <button type="submit6">LED 6</button> </p>
<p> <button type="submit7">BUZZER</button> </p>
</html>"""
         if command == "1":
                if status[0] == 0:
                    status[0] = 1
                    GPIO.output(4, GPIO.HIGH)
                    print 'LED 1 ON'
                    return render.index(status)
                elif status[0] == 1:
                    status[0] = 0
                    GPIO.output(4, GPIO.LOW)
                    print 'LED 1 OFF'
                    return render.index(status)
                else:
                    print 'error'
                    return render.index(status)
                return render.index(status)






if __name__ == "__main__":
        app.run()
Pewis
  • 57
  • 1
  • 7

1 Answers1

2

One of possible solutions is as follows:

  • Wrap a button in a <form> tag with POST as an action like (here)
  • Implement a POST handler on your server (do_POST) that will perform required logic on LED

Update:

Here are some more explanations:

In the server code above you have implemented a so-called GET handler, a function that is being called when server receives an HTTP request made via HTTP method (verb) GET. When a GET request is sent by some client application (for example a browser, when user navigates to the address of the server), upon receiving it, server's internals call your GET() function and execute its body. Your function returns the HTML code string back to the client (and browser receives it, finds the HTML code and renders it as a web page to the user).

Now, in order to send the information to the server, you need client to send an HTTP request via HTTP method (verb) POST to the server. For instance, that is what a <button> inside a <form> does when user clicks it in a browser. Sure enough, you want your web server to be able to receive and handle this POST request, performing some useful action. It is typically done by implementing a POST handler function. Check your HTTP server package's documentation for details on how to make so. I guess you will need to create a function called POST() and to put whatever logic you want to be executed inside it.

References:

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61