-
Notifications
You must be signed in to change notification settings - Fork 1
/
pir_pub.py
executable file
·70 lines (55 loc) · 2.68 KB
/
pir_pub.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 json
import awsiot
import logging
from signal import pause
try:
from gpiozero import MotionSensor
except ImportError:
logging.error("Unable to import gpiozero")
pass
def pub(topic, value):
if topic is not None and len(topic) > 0:
for t in topic:
publisher.publish(t,
json.dumps(
{args.shadow_var: value, awsiot.MESSAGE: "{} {}".format(args.shadow_var, value)}))
publisher.publish(awsiot.iot_thing_topic(args.thing), awsiot.iot_payload(awsiot.REPORTED, {args.shadow_var: value}))
def motion():
logging.info("{} {} {}".format(args.shadow_var, args.pin, args.high_value))
pub(args.topic, args.high_value)
def no_motion():
logging.info("{} {} {}".format(args.shadow_var, args.pin, args.low_value))
if args.low_topic:
pub(args.low_topic, args.low_value)
else:
pub(args.topic, args.low_value)
if __name__ == "__main__":
parser = awsiot.iot_arg_parser()
parser.add_argument("-p", "--pin", help="gpio pin (using BCM numbering)", type=int, required=True)
parser.add_argument("-q", "--queue_len",
help="The length of the queue used to store values read from the sensor. (1 = disabled)",
type=int, default=1)
parser.add_argument("-w", "--sample_rate",
help="The number of values to read from the device " +
"(and append to the internal queue) per second",
type=float, default=100)
parser.add_argument("-x", "--threshold",
help="When the mean of all values in the internal queue rises above this value, " +
"the sensor will be considered active by the is_active property, " +
"and all appropriate events will be fired",
type=float, default=0.5)
parser.add_argument("-s", "--shadow_var", help="Shadow variable", required=True)
parser.add_argument("-y", "--high_value", help="high value", default=1)
parser.add_argument("-z", "--low_value", help="low value", default=0)
parser.add_argument("-o", "--low_topic", nargs='*', help="Low topic")
args = parser.parse_args()
logging.basicConfig(filename=awsiot.LOG_FILE, level=args.log_level, format=awsiot.LOG_FORMAT)
publisher = awsiot.MQTT(args.endpoint, args.rootCA, args.cert, args.key)
pir = MotionSensor(args.pin,
queue_len=args.queue_len,
sample_rate=args.sample_rate,
threshold=args.threshold)
pir.when_motion = motion
pir.when_no_motion = no_motion
pause()