Advertisement
neuberfran

Untitled

Sep 12th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import mraa
  2. import time
  3.  
  4. from flask import Flask, render_template, request
  5. app = Flask(__name__)
  6.  
  7. a = mraa.Gpio(7) #set pin number
  8. b = mraa.Gpio(13) #set pin number
  9.  
  10. pins = {
  11. 7 : {'name': 'coffee maker', 'state': False},
  12. 13: {'name': 'lamp', 'state': False}
  13. }
  14.  
  15. def a_helper(a, changePin, state, act_state):
  16. a.dir(mraa.DIR_OUT) #set output
  17. a.write(act_state)
  18. pins[changePin]['state'] = state
  19. # message = 'Turned ' + deviceName + ' on'
  20.  
  21. def b_helper(b, changePin, state, act_state):
  22. b.dir(mraa.DIR_OUT) #set output
  23. b.write(act_state)
  24. pins[changePin]['state'] = state
  25. # message = 'Turned ' + deviceName + ' on'
  26.  
  27. @app.route('/')
  28. def main():
  29. a.dir(mraa.DIR_IN) #set input
  30. b.dir(mraa.DIR_IN) #set input
  31.  
  32. templateData = {
  33. 'pins': pins
  34. }
  35. return render_template('new_response.html', **templateData)
  36.  
  37. @app.route('/<changePin>/<action>')
  38. def action(changePin, action):
  39. changePin = int(changePin)
  40. deviceName = pins[changePin]['name']
  41. if action == 'on':
  42. if changePin == 7:
  43. a_helper(a, changePin, True, 1)
  44. message = 'Turned ' + deviceName + ' on'
  45. else:
  46. b_helper(b, changePin, True, 1)
  47. message = 'Turned ' + deviceName + ' on'
  48. if action == 'off':
  49. if changePin == 7:
  50. a_helper(a, changePin, True, 0)
  51. message = 'Turned ' + deviceName + ' off'
  52. else:
  53. b_helper(b, changePin, True, 0)
  54. message = 'Turned ' + deviceName + ' off'
  55.  
  56. for pin in pins:
  57. pins[pin]['state'] = False
  58.  
  59. a.dir(mraa.DIR_IN) #set input
  60. b.dir(mraa.DIR_IN) #set input
  61.  
  62. templateData = {
  63. 'message': message,
  64. 'pins': pins
  65. }
  66.  
  67. return render_template('new_response.html', **templateData)
  68.  
  69. if __name__ == '__main__':
  70. app.run(host='0.0.0.0', port=8182, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement