-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactor-lights.py
36 lines (32 loc) · 1 KB
/
reactor-lights.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
from twisted.internet import protocol, reactor, endpoints
import RPi.GPIO as GPIO
LIGHT1 = 5
LIGHT2 = 6
LIGHT3 = 13
LIGHT4 = 19
ALL_LIGHTS = [LIGHT1, LIGHT2, LIGHT3, LIGHT4]
GPIO.setmode(GPIO.BCM)
GPIO.setup(ALL_LIGHTS, GPIO.OUT, initial=0)
class Echo(protocol.Protocol):
def dataReceived(self, data):
if data == b'ls1 on':
GPIO.output(LIGHT1, 1)
elif data == b'ls1 off':
GPIO.output(LIGHT1, 0)
elif data == b'ls2 on':
GPIO.output(LIGHT2, 1)
elif data == b'ls2 off':
GPIO.output(LIGHT2, 0)
elif data == b'ls3 on':
GPIO.output(LIGHT3, 1)
elif data == b'ls3 off':
GPIO.output(LIGHT3, 0)
elif data == b'ls4 on':
GPIO.output(LIGHT4, 1)
elif data == b'ls4 off':
GPIO.output(LIGHT4, 0)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
endpoints.serverFromString(reactor, "tcp:8000").listen(EchoFactory())
reactor.run()