forked from pimoroni/Raspberry-Pi-Web-RGB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb.py
executable file
·39 lines (28 loc) · 850 Bytes
/
rgb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
from flask import Flask
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
app = Flask(__name__)
# Explorer HAT uses GPIO 27 = red, 4 = blue, 5 = green
# Pibrella uses GPIO 27 = red, 17 = yellow, 4 = green
# Map of LED names and associated GPIO pins
leds = {
'red': 27,
'blue': 4,
'green': 5
}
for color in leds.keys():
GPIO.setup(leds[color], GPIO.OUT)
@app.route("/led/<color>/<state>")
def set_led(color, state):
if color in leds.keys():
if state == 'on':
GPIO.output(leds[color], 1)
return 'LED On: {}'.format(color)
else:
GPIO.output(leds[color], 0)
return 'LED Off: {}'.format(color)
return 'Invalid LED: {}'.format(color)
app.add_url_rule("/", "index", lambda: 'Hello World!')
if __name__ == "__main__":
app.run(host='0.0.0.0')