-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttlisten.py
35 lines (29 loc) · 899 Bytes
/
mqttlisten.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
import paho.mqtt.client as paho
import RPi.GPIO as GPIO
import time
def kapiac():
pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
GPIO.output(pin, GPIO.LOW)
time.sleep(1)
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
if "kapi" in str(msg.payload):
try:
kapiac()
print("Kapı açıldı.")
except KeyboardInterrupt as e:
print(e)
finally:
GPIO.cleanup()
client = paho.Client()
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect("broker.mqttdashboard.com", 1883) # Public broker
client.subscribe("testtopic/1") #Listening path
client.loop_forever()