Last active
July 31, 2017 15:13
-
-
Save shmam/d773b07cbd6e800b6347696d6ad66c77 to your computer and use it in GitHub Desktop.
Flask Color Change code for RPI3 Python API Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!flask/bin/python | |
import time, sys | |
import RPi.GPIO as GPIO | |
from flask import Flask | |
redPin = 11 #Set to appropriate GPIO | |
greenPin = 15 #Should be set in the | |
bluePin = 22 #GPIO.BOARD format | |
app = Flask(__name__) | |
def blink(pin): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.output(pin, GPIO.HIGH) | |
def turnOff(pin): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.output(pin, GPIO.LOW) | |
def redOn(): | |
blink(redPin) | |
def redOff(): | |
turnOff(redPin) | |
def greenOn(): | |
blink(greenPin) | |
def greenOff(): | |
turnOff(greenPin) | |
def blueOn(): | |
blink(bluePin) | |
def blueOff(): | |
turnOff(bluePin) | |
def yellowOn(): | |
blink(redPin) | |
blink(greenPin) | |
def yellowOff(): | |
turnOff(redPin) | |
turnOff(greenPin) | |
def cyanOn(): | |
blink(greenPin) | |
blink(bluePin) | |
def cyanOff(): | |
turnOff(greenPin) | |
turnOff(bluePin) | |
def magentaOn(): | |
blink(redPin) | |
blink(bluePin) | |
def magentaOff(): | |
turnOff(redPin) | |
turnOff(bluePin) | |
def whiteOn(): | |
blink(redPin) | |
blink(greenPin) | |
blink(bluePin) | |
def whiteOff(): | |
turnOff(redPin) | |
turnOff(greenPin) | |
turnOff(bluePin) | |
def turnAllOff(): | |
redOff() | |
blueOff() | |
greenOff() | |
@app.route('/light/<light_color>') | |
def changelight(light_color): | |
if light_color == 'red': | |
turnAllOff() | |
redOn() | |
elif light_color == 'yellow': | |
turnAllOff() | |
yellowOn() | |
elif light_color == 'green': | |
turnAllOff() | |
greenOn() | |
elif light_color == 'blue': | |
turnAllOff() | |
blueOn() | |
elif light_color == 'magenta': | |
turnAllOff() | |
magentaOn() | |
elif light_color == 'cyan': | |
turnAllOff() | |
cyanOn() | |
elif light_color == 'null': | |
turnAllOff() | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get install python3-flask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment