Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf8 -*-
- import RPi.GPIO as GPIO
- import MFRC522
- import signal
- import serial
- import time, os, json
- y=0
- zaeler = 0
- uid0 = 0
- uid1 = 0
- uid2 = 0
- uid3 = 0
- a = 0
- i = 0
- s = serial.Serial('/dev/ttyACM0', 9600) # Namen ggf. anpassen
- s.isOpen()
- time.sleep(5)
- GPIO.setmode(GPIO.BOARD);
- GPIO.setwarnings(False)
- SEND_URL = "https://gcm-http.googleapis.com/gcm/send";
- SENDER_CHANNEL = "/topics/bewegung";
- COOLDOWN = 10; # Seconds that should be waited between messages
- NUM_MODULES = 2; # amount of used modules (motion detector, magnetic switch$
- MODULES = range(NUM_MODULES);
- MODULES[0] = { "name": "Eingangstuer", "gpio": 13};
- MODULES[1] = { "name": "Seitentuer", "gpio": 15};
- def sentMessage(channel):
- for m in MODULES:
- if channel == m["gpio"]:
- if not ("lastMessageSent" in m) or ("lastMessageSent" in m and m["lastMessageSent"]+COOLDOWN <= time.time()):
- m["lastMessageSent"] = int(time.time());
- obj = { "O": m["name"],
- "T": m["lastMessageSent"]};
- message = json.JSONEncoder().encode(obj).replace('"', '\\"');
- os.system('curl --header "Authorization: key="'+AUTH_KEY+' --header Content-Type:"application/json" '+SEND_URL+' -d \'{"to":"'+SENDER_CHANNEL+'", "data": {"message": "'+message+'" }}\'');
- i=2
- return;
- print "Bereit"
- s.write("a")
- while True:
- # Capture SIGINT for cleanup when the script is aborted
- def end_read(signal,frame):
- global continue_reading
- continue_reading = False
- GPIO.cleanup()
- # Hook the SIGINT
- signal.signal(signal.SIGINT, end_read)
- # Create an object of the class MFRC522
- MIFAREReader = MFRC522.MFRC522()
- # Scan for cards
- (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
- # If a card is found
- if status == MIFAREReader.MI_OK:
- print"Karte gefunden"
- # Get the UID of the card
- (status,uid) = MIFAREReader.MFRC522_Anticoll()
- uid0 = int(uid[0])
- uid1 = int(uid[1])
- uid2 = int(uid[2])
- uid3 = int(uid[3])
- if uid0 == 37 and uid1 == 244 and uid2 == 3 and uid3 == 83:
- print"richtige Karte"
- if i==1:
- print"Aktiviert"
- s.write("d")
- i=0
- else:
- if i==0:
- os.system ("sudo ./send 10101 1 0")
- i=1
- s.write("e")
- print ("Deaktiviert")
- else:
- if i==2:
- os.system ("sudo ./send 10101 1 0")
- i=1
- s.write("e")
- print ("Deaktiviert")
- else:
- s.write("f")
- i=2
- time.sleep(3)
- #---------------------------------------------------------------------------
- if i==0:
- # Setup
- pass
- for m in MODULES:
- GPIO.setup(m["gpio"], GPIO.IN);
- GPIO.add_event_detect(m["gpio"], GPIO.RISING, callback=sentMessage);
- # s.write("c")
- # print "Sende c"
- # os.system ("sudo ./send 10101 1 1")
- #----------------------------------------------------------------------------
- if i==2:
- a=2
- #----------------------------------------------------------------------------
- if i==1:
- #Deaktiviert
- zaeler = zaeler + 1
- #----------------------------------------------------------------------------
- time.sleep(0.2)
Add Comment
Please, Sign In to add comment