from webob import Request, Response import RPi.GPIO as GPIO # Import library for GPIO (pin) access GPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-26 on the connector) GPIO.setwarnings(False) # Disable the warning "This channel is already in use" GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output GPIO.output(22, 0) # Turn off the LED when the program starts class WebApp(object): def __call__(self, environ, start_response): # capture the request coming from the browser req = Request(environ) print("**** Serving request ****") # Look for on_off_variable in the request: x = req.params.get('on_off_variable') # save content of on_off_variable in x print(x) if x == "on": # if button ON pressed... GPIO.output(22, 1) # turn on LED elif x == "off": # if button OFF pressed... GPIO.output(22, 0) # turn off LED html = """ Turn on or off the led:
""" # create and return the response to the browser resp = Response(html) return resp(environ, start_response) application = WebApp() # main program - the web server if __name__ == '__main__': from wsgiref.simple_server import make_server port = 8080 httpd = make_server('', port, application) print('Serving HTTP on port %s...'%port) httpd.serve_forever()