-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutlet.py
46 lines (44 loc) · 1.3 KB
/
outlet.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
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
from dotenv import load_dotenv, find_dotenv
import time
import redis
import sys
import os
# Load .env
load_dotenv(find_dotenv())
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
RELAY_PIN = 4
GPIO.setup(RELAY_PIN, GPIO.OUT)
# One shot mode
if sys.argv and len(sys.argv) > 1:
if sys.argv[1] == "up":
GPIO.output(RELAY_PIN, GPIO.HIGH)
elif sys.argv[1] == "down":
GPIO.output(RELAY_PIN,GPIO.LOW)
elif os.environ.get('REDIS_SERVER_URL'):
# Redis connection mode
port = int(os.environ.get('REDIS_SERVER_PORT'))
r = redis.StrictRedis(host=os.environ.get('REDIS_SERVER_URL'), port=port if port else 6379)
p = r.pubsub()
channel = 'slack'
if not r.exists(channel):
r.set(channel, True)
p.subscribe(channel)
while True:
try:
message = p.get_message()
if message:
command = message['data']
if type(command) == bytes:
command = command.decode('utf-8')
print(command)
if command == 'up':
GPIO.output(RELAY_PIN, GPIO.HIGH)
elif command == 'down':
GPIO.output(RELAY_PIN, GPIO.LOW)
time.sleep(0.5)
except KeyboardInterrupt:
break