SHARE
TWEET

Untitled

a guest Nov 19th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO
  2. from flask import Flask, render_template, request
  3. app = Flask(__name__)
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. # Create a dictionary called pins to store the pin number, name, and pin state:
  8. pins = {
  9.    23 : {'name' : 'GPIO 23', 'state' : GPIO.LOW},
  10.    24 : {'name' : 'GPIO 24', 'state' : GPIO.LOW}
  11.    }
  12.  
  13. # Set each pin as an output and make it low:
  14. for pin in pins:
  15.    GPIO.setup(pin, GPIO.OUT)
  16.    GPIO.output(pin, GPIO.LOW)
  17.  
  18. @app.route("/")
  19. def main():
  20.    # For each pin, read the pin state and store it in the pins dictionary:
  21.    for pin in pins:
  22.       pins[pin]['state'] = GPIO.input(pin)
  23.    # Put the pin dictionary into the template data dictionary:
  24.    templateData = {
  25.       'pins' : pins
  26.       }
  27.    # Pass the template data into the template main.html and return it to the user
  28.    return render_template('main.html', **templateData)
  29.  
  30. # The function below is executed when someone requests a URL with the pin number and action in it:
  31. @app.route("/<changePin>/<action>")
  32. def action(changePin, action):
  33.    # Convert the pin from the URL into an integer:
  34.    changePin = int(changePin)
  35.    # Get the device name for the pin being changed:
  36.    deviceName = pins[changePin]['name']
  37.    # If the action part of the URL is "on," execute the code indented below:
  38.    if action == "on":
  39.       # Set the pin high:
  40.       GPIO.output(changePin, GPIO.HIGH)
  41.       # Save the status message to be passed into the template:
  42.       message = "Turned " + deviceName + " on."
  43.    if action == "off":
  44.       GPIO.output(changePin, GPIO.LOW)
  45.       message = "Turned " + deviceName + " off."
  46.  
  47.    # For each pin, read the pin state and store it in the pins dictionary:
  48.    for pin in pins:
  49.       pins[pin]['state'] = GPIO.input(pin)
  50.  
  51.    # Along with the pin dictionary, put the message into the template data dictionary:
  52.    templateData = {
  53.       'pins' : pins
  54.    }
  55.  
  56.    return render_template('main.html', **templateData)
  57.  
  58. if __name__ == "__main__":
  59.    app.run(host='0.0.0.0', port=80, debug=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top