Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mraa
- import time
- from flask import Flask, render_template, request
- app = Flask(__name__)
- a = mraa.Gpio(7) #set pin number
- b = mraa.Gpio(13) #set pin number
- pins = {
- 7 : {'name': 'coffee maker', 'state': False},
- 13: {'name': 'lamp', 'state': False}
- }
- def a_helper(a, changePin, state, act_state):
- a.dir(mraa.DIR_OUT) #set output
- a.write(act_state)
- pins[changePin]['state'] = state
- # message = 'Turned ' + deviceName + ' on'
- def b_helper(b, changePin, state, act_state):
- b.dir(mraa.DIR_OUT) #set output
- b.write(act_state)
- pins[changePin]['state'] = state
- # message = 'Turned ' + deviceName + ' on'
- @app.route('/')
- def main():
- a.dir(mraa.DIR_IN) #set input
- b.dir(mraa.DIR_IN) #set input
- templateData = {
- 'pins': pins
- }
- return render_template('new_response.html', **templateData)
- @app.route('/<changePin>/<action>')
- def action(changePin, action):
- changePin = int(changePin)
- deviceName = pins[changePin]['name']
- if action == 'on':
- if changePin == 7:
- a_helper(a, changePin, True, 1)
- message = 'Turned ' + deviceName + ' on'
- else:
- b_helper(b, changePin, True, 1)
- message = 'Turned ' + deviceName + ' on'
- if action == 'off':
- if changePin == 7:
- a_helper(a, changePin, True, 0)
- message = 'Turned ' + deviceName + ' off'
- else:
- b_helper(b, changePin, True, 0)
- message = 'Turned ' + deviceName + ' off'
- for pin in pins:
- pins[pin]['state'] = False
- a.dir(mraa.DIR_IN) #set input
- b.dir(mraa.DIR_IN) #set input
- templateData = {
- 'message': message,
- 'pins': pins
- }
- return render_template('new_response.html', **templateData)
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=8182, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement