Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mraa as x
- import time
- from flask import Flask, render_template, request
- app = Flask(__name__)
- # GPIO.setmode(GPIO.BCM)
- pins = {
- 7:{"gpioMraa":x.Gpio(7), "name":"coffe maker", "state":False},
- 8:{"gpioMraa":x.Gpio(8), "name":"lamp", "state":False}
- }
- # Sets GPIOs as outputs with LOW State
- for pin in pins:
- pins[pin]["gpioMraa"].dir(x.DIR_OUT)
- pins[pin]["gpioMraa"].write(0)
- @app.route("/")
- def main():
- for pin in pins:
- pins[pin]["state"].dir(x.DIR_IN)
- pins[pin]["state"].read()
- templateData = {
- 'pins' : pins
- }
- return render_template('main.html', **templateData)
- @app.route("/<changePin>/<action>")
- def action(changePin, action):
- changePin = int(changePin)
- deviceName = pins[changePin]['name']
- if action == "on":
- pin.dir(x.DIR_OUT)
- pin.write(1)
- # GPIO.output(changePin, GPIO.HIGH)
- message = "Turned " + deviceName + " on."
- if action == "off":
- pin.dir(x.DIR_OUT)
- pin.write(0)
- # GPIO.output(changePin, GPIO.LOW)
- message = "Turned " + deviceName + " off."
- for pin in pins:
- pins[pin]["state"].dir(x.DIR_IN)
- pins[pin]["state"].read()
- templateData = {
- 'message': message,
- 'pins' : pins
- }
- return render_template('main.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