SHARE
TWEET

Untitled

a guest Dec 10th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paho.mqtt.client as mqtt
  2. import RPi.GPIO as GPIO
  3. import sys
  4. import time
  5. import ssl
  6. import os
  7.  
  8. brokerAddress="3.83.104.216" #Mqtt-Server IP address
  9. port=8883
  10.  
  11. GPIO.setmode(GPIO.BCM)
  12. GPIO.setwarnings(False)
  13.  
  14. buzzerPin ="11"
  15. buttonPin = 26
  16. ledPin="12"
  17. lockPin="17"
  18.  
  19. userApp="/home/pi/pinControl.out writepin"
  20.  
  21. GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  22.  
  23. def my_callback(buttonPin):
  24.     os.system(userApp+' '+ledPin+' 0')
  25.     os.system(userApp+' '+lockPin+' 0')
  26.     GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  27.  
  28. def on_message(client, userdata, msg):
  29.     print("Topic: " + msg.topic)
  30.     print("Message received: " + str(msg.payload.decode('utf8')))
  31.     if "Alert" in str(msg.payload.decode('utf8')):
  32.         os.system(userApp+' '+ledPin+' 1')
  33.         os.system(userApp+' '+lockPin+' 1')
  34.  
  35.     if "Lock" in str(msg.payload.decode('utf8')):
  36.         os.system(userApp+' writepin '+lockPin+' 1')
  37.  
  38.  
  39.     if "Unlock" in str(msg.payload.decode('utf8')):
  40.         os.system(userApp+' writepin '+lockPin+' 0')
  41.  
  42.     GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=my_callback)
  43.  
  44.    
  45. def on_connect(client, userdata, flags, rc):
  46.     print("Alarm System set up and connected :"+str(rc))    
  47.  
  48. def subscribe():
  49.     #client id should be unique
  50.     client = mqtt.Client(client_id="RPi", clean_session=True, userdata=None, transport="tcp")
  51.     client.tls_set('ca.crt',cert_reqs=ssl.CERT_NONE)
  52.     client.username_pw_set("conor","conor")
  53.     client.on_connect = on_connect
  54.     client.on_message=on_message
  55.     print("Setting Up Alarm System...")
  56.     client.connect(brokerAddress,port)
  57.     client.loop_start()
  58.     client.subscribe("test")
  59.     client.loop_forever()
  60.  
  61. subscribe()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top