Skip to content

Instantly share code, notes, and snippets.

@codenamesubho

codenamesubho/rbg.py

Created Apr 28, 2017
Embed
What would you like to do?
from flask import Flask, request, jsonify
import RPi.GPIO as GPIO
app = Flask(__name__)
BLUE = 12
RED = 13
GREEN = 18
base_response = {
'speech':"Abra Ka Dabra,{color} LED glowing",
'displayText' : "Abra Kaa Daabra, {color} LED glowing",
'source' : 'Manual'}
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'GET':
text = """WELCOME to RBG<br>
/red -> red LED<br>
/blue -> blue LED<br>
/green -> green LED<br>
/clear -> clear all<br>
"""
return text
else:
req_body = request.get_json()
color = req_body['result']['resolvedQuery']
if color == 'red':
red()
if color == 'green':
green()
if color == 'blue':
blue()
response = base_response.copy()
response['speech'] = response['speech'].format(color=color)
response['displayText'] = response['displayText'].format(color=color)
return jsonify(response)
@app.route('/red')
def red():
GPIO.output(BLUE,GPIO.LOW)
GPIO.output(RED,GPIO.HIGH)
GPIO.output(GREEN,GPIO.LOW)
return "RED"
@app.route('/green')
def green():
GPIO.output(BLUE,GPIO.LOW)
GPIO.output(RED,GPIO.LOW)
GPIO.output(GREEN,GPIO.HIGH)
return "GREEN"
@app.route('/blue')
def blue():
GPIO.output(BLUE,GPIO.HIGH)
GPIO.output(RED,GPIO.LOW)
GPIO.output(GREEN,GPIO.LOW)
return "BLUE"
@app.route('/clear')
def clear():
GPIO.output(BLUE,GPIO.LOW)
GPIO.output(RED,GPIO.LOW)
GPIO.output(GREEN,GPIO.LOW)
return "Cleared"
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)
GPIO.setup(BLUE,GPIO.OUT)
GPIO.setup(RED,GPIO.OUT)
GPIO.setup(GREEN,GPIO.OUT)
app.run(host='0.0.0.0',port=5000,debug=True)
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment