Last active
August 29, 2015 14:14
-
-
Save Andygmb/ed2ae417c5db128fef43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Importing all the necessary modules so we can use them in this script. | |
| from flask import Flask | |
| import RPi.GPIO as GPIO | |
| import Queue | |
| import threading | |
| import time | |
| # Create the Flask object and assign it to the app variable. | |
| app = Flask(__name__) | |
| # If someone goes to 127.0.0.1 or 127.0.0.1/ they will be presented with | |
| # buttons.html | |
| @app.route("/") | |
| def hello(): | |
| return render_template("buttons.html") | |
| # If someone goes to 127.0.0.1/yellow/1/, flask will grab whatever is inbetween each | |
| # slash and assign them to their respective variable names (defined in @app.route) | |
| # For example, if I go to 127.0.0.1/red/0/, it will assign colour = red and value = 0 | |
| # we need to then pass those variables to the function "turn_on_or_off" | |
| @app.route("/<colour>/<value>/") | |
| def turn_on_or_off(colour, value): | |
| # global means we are making the following variable ("looping") into | |
| # a global variable which can be used by any function within this script. | |
| # It is considered to be in the "global namespace" - if you did not | |
| # make the variable global, it would ONLY be accessable within the function | |
| # which it was created in. | |
| # The reason we make it global is so that the loop_till_false function can access it at any time | |
| global looping | |
| # Now we give looping an actual value to hold - the value we received from the app.route. | |
| # For example, let's assume that someone went to 127.0.0.1/red/1/ | |
| # that means that currently, value = "1". | |
| # We want to turn this value into a Boolean (True or False), but at the moment | |
| # it is currently a string. All strings (except empty strings ("")) when evaluated | |
| # as a Boolean will equal True. We can turn anything into a Boolean by using Bool(value) | |
| # So what we need to do is turn it into an integer value, which we can then | |
| # check if it's True or False (1 or 0). | |
| # We do that by doing int("1") | |
| # which turns our "1" string into an integer value of 1 (notice the lack of quotes, this means it's not a string) | |
| # When we have our integer value of 1 or 0 | |
| # we can then use Bool(1) to see if it's True or False | |
| # then we set looping to be whatever Boolean value we get (in the case of 1, True) | |
| looping = bool(int(value)) | |
| # The function NewThread is given a function to run (loop_till_false) in a seperate thread | |
| # We do this because python runs everything on the same thread, meaning if we have | |
| # an infinite loop in our flask server, the entire script will be waiting for | |
| # the loop to finish to continue handling web requests. | |
| NewThread(loop_till_false).start() | |
| # Flask requires us to respond with something for the web request to finish, | |
| # so you can just return anything at this point. | |
| return "Anything" | |
| def loop_till_false(): | |
| # while looping: actually means | |
| # While the variable is True: | |
| # do the following code | |
| # If however looping is False, the loop will not run and the function is finished. | |
| # So once the variable looping = False, the loop is finished. | |
| while looping: | |
| GPIO.output(4, True) | |
| time.sleep(1) | |
| GPIO.output(4, False) | |
| time.sleep(1) | |
| # This code handles setting up a new thread object for the callback function (the function we pass to it) | |
| # to be executed in a seperate thread. Once our callback function is done, the thread will be deleted. | |
| class NewThread(threading.Thread): | |
| def __init__(self, callback_function): | |
| threading.Thread.__init__(self) | |
| self.callback_function = callback_function | |
| # run = the code to be executed when the Thread object is created | |
| def run(self): | |
| self.callback_function() | |
| # if __name__ == "__main__" is just standard python syntax that is explained here: | |
| # http://stackoverflow.com/questions/419163/what-does-if-name-main-do | |
| # it's not necessary to understand why this is needed. | |
| if __name__ == "__main__": | |
| # Again, we set GPIO to be a global object so that the seperate thread can access it. | |
| global GPIO | |
| GPIO.setwarnings(False) | |
| GPIO.setmode(GPIO.BCM) | |
| GPIO.setup(4, GPIO.OUT) | |
| app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment