#!python3 import paho.mqtt.client as mqtt import time import sys import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.IN) #Dit komt van het magneetcontact deur GPIO.setup(22, GPIO.OUT) #Dit gaat naar spanningsdeler van de transistorschakeling GPIO.setwarnings(False) def aan(x): #if (GPIO.input(27) == 1 and (x == 1 or x =='1')): GPIO.output(22, GPIO.LOW) #print("relay on") #print(m_decode) time.sleep(600) def uit(y): #if (GPIO.input(27) == 1 or GPIO.input(27) == 0 and (y == 0 or y =='0')): GPIO.output(22, GPIO.HIGH) #print("relay off") #print(m_decode) def my_callback(channel): if (m_decode == 1 or m_decode == '1') and (GPIO.input(27) == 1): aan(m_decode) else: uit(0) GPIO.add_event_detect(27, GPIO.BOTH, callback=my_callback) def on_log(client, userdata, level, buf): print("log: "+buf) def on_connect(client, userdata, flag, rc): if rc==0: print("Connection ok") else: print("Bad connection Returned code=", rc) def on_disconnect(client, userdata, flags, rc=0): print("Disconnected result code "+str(rc)) def on_message(client, userdata, msg): global m_decode topic=msg.topic m_decode=str(msg.payload.decode("utf-8", "ignore")) print("Bericht ontvangen",m_decode) # if (m_decode==1 or m_decode=='1' ): # #os.system("sudo python sirene_aan.py") # print(m_decode) # else: # #os.system("sudo python sirene_uit.py") # print(m_decode) broker="192.168.178.199" client = mqtt.Client("python1") #create a new instance client.on_connect=on_connect #bind call back function client.on_disconnect=on_disconnect client.on_log=on_log client.on_message=on_message print("Connecting to broker ", broker) client.connect(broker) client.loop_start() client.subscribe("Gang beneden/sirene") #client.publish("gang/test", "true") #time.sleep(400) client.loop_stop #client.disconnect() while True: pass