-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
71 lines (59 loc) · 1.08 KB
/
server.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import RPi.GPIO as GPIO
from bottle import route, response, run, hook
#
# Minimal http server for GPIO control
#
# physical board pin number
GPIO.setmode(GPIO.BOARD)
GPIOS = [
7,
11,
13,
15,
19,
21,
23,
29,
31,
33,
35,
37,
8,
10,
12,
16,
18,
22,
24,
26,
32,
36,
38,
40
];
for pin in GPIOS:
print "setup GPIO", pin
GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH)
def get_pin_value(pin):
value = GPIO.input(pin)
return {
'pin' : pin,
'value' : value
}
@hook('after_request')
def enable_cors():
response.headers['Access-Control-Allow-Origin'] = '*'
# ex:
# /gpio/4/output/0
# /gpio/4/output/1
@route('/gpio/<pin:int>/output/<value:int>', method='GET')
def gpio_write(pin, value):
gpio_value = GPIO.LOW if (value == 0) else GPIO.HIGH
print 'GPIO WRITE', pin, gpio_value
GPIO.output(int(pin), gpio_value)
return get_pin_value(int(pin))
@route('/gpio/<pin:int>', method='GET')
def gpio_read(pin):
return get_pin_value(int(pin))
run(host = '0.0.0.0', port = '8080', debug = True, server='cherrypy')
GPIO.cleanup()