-
Notifications
You must be signed in to change notification settings - Fork 0
/
garagecontroller.py
111 lines (89 loc) · 2.63 KB
/
garagecontroller.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python2.7
# Purpose: Process MQTT trigger message from Home Assistant RPi to open garage door.
# Original Source: https://hackaday.io/project/9901/instructions
import sys
import mosquitto
import RPi.GPIO as GPIO
import time
#import picamera
firstRun = 1
doorSensorPIN = 18
relayPIN = 17
relayCmdTopic = 'garage/relay'
cameraTopic = 'garage/camera'
doorStatusTopic = 'garage/status'
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPIN, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(doorSensorPIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # activate input with PullUp
def getDoorStatus():
if GPIO.input(doorSensorPIN):
return 'closed'
else:
return 'open'
def publishDoorStatus():
time.sleep(0.5)
doorStatus = getDoorStatus()
print("door status is now... " + doorStatus)
mqttc.publish(doorStatusTopic, doorStatus, 0, False)
def on_connect(mosq, obj, rc):
print("rc: "+str(rc))
def on_message(mosq, obj, msg):
print ("TOPIC: " + msg.topic + " - PAYLOAD: " + str(msg.payload))
global firstRun
if msg.topic == relayCmdTopic:
if not firstRun:
print("not the first run, so allow relay execution.")
if msg.payload == 'open':
gdo_relay()
elif msg.payload == 'close':
gdo_relay()
else:
print("first run. don't allow relay execution.")
#elif msg.topic == doorStatusTopic:
# if msg.payload == 'get':
# doorStatus = getDoorStatus()
# #print("DOOR IS " + doorStatus)
# mqttc.publish(doorStatusTopic, doorStatus, 0, False)
firstRun = 0
print("----------------------------------------------------")
def on_publish(mosq, obj, mid):
print("mid: "+str(mid))
print("")
print("")
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
print("")
def on_log(mosq, obj, level, string):
print(string)
def gdo_relay():
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPIN, GPIO.OUT, initial=GPIO.HIGH)
print "Garage door triggered."
GPIO.output(relayPIN, False)
time.sleep(0.5)
GPIO.output(relayPIN, True)
publishDoorStatus()
def gdo_cam():
print "Processing request for visual update."
# camera.capture('/var/www/html/gdoCam/visual_status.jpg')
def main():
print "Garage Door Opener MQTT script"
print ""
if __name__ == "__main__":
main()
try:
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.username_pw_set(<username>, <password>)
mqttc.connect(<mqtt broker address>, 1883, 60)
mqttc.subscribe("garage/#", 0)
publishDoorStatus()
rc = 0
while rc == 0:
rc = mqttc.loop()
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()