neuberfran

Untitled

Aug 31st, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import mraa as x
  2. import time
  3. from flask import Flask, render_template, request
  4. app = Flask(__name__)
  5.  
  6. # GPIO.setmode(GPIO.BCM)
  7.  
  8. pins = {
  9. 7:{"gpioMraa":x.Gpio(7), "name":"coffe maker", "state":False},
  10. 8:{"gpioMraa":x.Gpio(8), "name":"lamp", "state":False}
  11. }
  12.  
  13. # Sets GPIOs as outputs with LOW State
  14. for pin in pins:
  15. pins[pin]["gpioMraa"].dir(x.DIR_OUT)
  16. pins[pin]["gpioMraa"].write(0)
  17.  
  18.  
  19. @app.route("/")
  20. def main():
  21. for pin in pins:
  22. pins[pin]["state"].dir(x.DIR_IN)
  23. pins[pin]["state"].read()
  24. templateData = {
  25. 'pins' : pins
  26. }
  27. return render_template('main.html', **templateData)
  28.  
  29. @app.route("/<changePin>/<action>")
  30. def action(changePin, action):
  31. changePin = int(changePin)
  32. deviceName = pins[changePin]['name']
  33. if action == "on":
  34. pin.dir(x.DIR_OUT)
  35. pin.write(1)
  36. # GPIO.output(changePin, GPIO.HIGH)
  37. message = "Turned " + deviceName + " on."
  38. if action == "off":
  39. pin.dir(x.DIR_OUT)
  40. pin.write(0)
  41. # GPIO.output(changePin, GPIO.LOW)
  42. message = "Turned " + deviceName + " off."
  43.  
  44. for pin in pins:
  45. pins[pin]["state"].dir(x.DIR_IN)
  46. pins[pin]["state"].read()
  47. templateData = {
  48. 'message': message,
  49. 'pins' : pins
  50. }
  51. return render_template('main.html', **templateData)
  52.  
  53. if __name__ == "__main__":
  54. app.run(host='0.0.0.0', port=8182, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment