-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_sub.py
executable file
·70 lines (52 loc) · 2.18 KB
/
relay_sub.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
#!/usr/bin/env python
import argparse
import time
import sys
import yaml
import logging
from cloud_tools import Subscriber
import wiringpi as relay
LOG_FILE = '/var/log/iot.log'
def pulse(pin, pulse_length=1):
relay.pinMode(pin, 1)
relay.digitalWrite(pin, 1)
time.sleep(pulse_length)
relay.digitalWrite(pin, 0)
def my_callback(client, userdata, msg):
logging.info("relay_sub {} {} {}".format(msg.topic, msg.qos, msg.payload))
pulse(args.pin)
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--endpoint", help="AWS IoT endpoint", required=True)
parser.add_argument("-i", "--clientID", help="Client ID", default='')
parser.add_argument("-r", "--rootCA", help="Root CA file path", required=True)
parser.add_argument("-c", "--cert", help="Certificate file path")
parser.add_argument("-k", "--key", help="Private key file path")
parser.add_argument("-p", "--pin", help="gpio pin (using BCM numbering)", type=int, required=True)
parser.add_argument("-t", "--topic", help="MQTT topic(s)", nargs='+', required=True)
parser.add_argument("-f", "--input_file", help="input file (yaml format)", default=None)
parser.add_argument("-l", "--log_level", help="Log Level", default=logging.INFO)
args = parser.parse_args()
logging.basicConfig(filename=LOG_FILE, level=args.log_level)
subscriber = Subscriber(args.endpoint, args.rootCA, args.key, args.cert, args.clientID)
relay.wiringPiSetup()
# Load configuration file
if args.input_file is not None:
f = open(args.input_file)
topics = yaml.safe_load(f)
for t in topics[args.endpoint]:
logging.info("Subscribing to {}".format(t))
subscriber.subscribe(t, my_callback)
time.sleep(2) # pause between subscribes (maybe not needed?)
for t in args.topic:
logging.info("Subscribing to {}".format(t))
subscriber.subscribe(t, my_callback)
time.sleep(2) # pause
# Loop forever
try:
while True:
time.sleep(0.2) # sleep needed because CPU race
pass
except (KeyboardInterrupt, SystemExit):
sys.exit()