daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jan 15th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import MFRC522
  6. import signal
  7. import serial
  8. import time, os, json
  9. y=0
  10. zaeler = 0
  11. uid0 = 0
  12. uid1 = 0
  13. uid2 = 0
  14. uid3 = 0
  15. a = 0
  16. i = 0
  17. s = serial.Serial('/dev/ttyACM0', 9600) # Namen ggf. anpassen
  18. s.isOpen()
  19. time.sleep(5)
  20. GPIO.setmode(GPIO.BOARD);
  21. GPIO.setwarnings(False)
  22.  
  23. SEND_URL        = "https://gcm-http.googleapis.com/gcm/send";
  24. SENDER_CHANNEL  = "/topics/bewegung";
  25. COOLDOWN        = 10; # Seconds that should be waited between messages
  26. NUM_MODULES     = 2; # amount of used modules (motion detector, magnetic switch$
  27.  
  28. MODULES = range(NUM_MODULES);
  29. MODULES[0] = { "name": "Eingangstuer", "gpio": 13};
  30. MODULES[1] = { "name": "Seitentuer", "gpio": 15};
  31.  
  32. def sentMessage(channel):  
  33.     for m in MODULES:
  34.         if channel == m["gpio"]:
  35.             if not ("lastMessageSent" in m) or ("lastMessageSent" in m and m["lastMessageSent"]+COOLDOWN <= time.time()):
  36.                 m["lastMessageSent"] = int(time.time());
  37.                 obj = { "O": m["name"],
  38.                 "T": m["lastMessageSent"]};
  39.                 message = json.JSONEncoder().encode(obj).replace('"', '\\"');
  40.                 os.system('curl --header "Authorization: key="'+AUTH_KEY+' --header Content-Type:"application/json" '+SEND_URL+' -d \'{"to":"'+SENDER_CHANNEL+'", "data": {"message": "'+message+'" }}\'');
  41.                 i=2
  42.                 return;
  43.  
  44.  
  45. print "Bereit"
  46. s.write("a")
  47.  
  48. while True:
  49.  
  50.  
  51.     # Capture SIGINT for cleanup when the script is aborted
  52.     def end_read(signal,frame):
  53.             global continue_reading
  54.             continue_reading = False
  55.             GPIO.cleanup()
  56. # Hook the SIGINT
  57.     signal.signal(signal.SIGINT, end_read)
  58.  
  59. # Create an object of the class MFRC522
  60.     MIFAREReader = MFRC522.MFRC522()
  61.  
  62.     # Scan for cards
  63.     (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  64.     # If a card is found
  65.     if status == MIFAREReader.MI_OK:
  66.         print"Karte gefunden"
  67.  
  68.     # Get the UID of the card
  69.         (status,uid) = MIFAREReader.MFRC522_Anticoll()
  70.  
  71.         uid0 = int(uid[0])
  72.         uid1 = int(uid[1])
  73.         uid2 = int(uid[2])
  74.         uid3 = int(uid[3])
  75.    
  76.         if uid0 == 37 and uid1 == 244 and uid2 == 3 and uid3 == 83:
  77.             print"richtige Karte"
  78.             if i==1:
  79.                 print"Aktiviert"
  80.                 s.write("d")
  81.                 i=0
  82.  
  83.             else:
  84.                 if i==0:
  85.                     os.system ("sudo ./send 10101 1 0")
  86.                     i=1
  87.                     s.write("e")
  88.                     print ("Deaktiviert")
  89.                 else:
  90.                     if i==2:
  91.                         os.system ("sudo ./send 10101 1 0")
  92.                         i=1
  93.                         s.write("e")
  94.                         print ("Deaktiviert")
  95.                     else:
  96.                         s.write("f")
  97.                         i=2
  98.             time.sleep(3)  
  99.  
  100. #---------------------------------------------------------------------------   
  101.  
  102.     if i==0:
  103.             # Setup
  104.         pass
  105.         for m in MODULES:
  106.                 GPIO.setup(m["gpio"], GPIO.IN);
  107.                 GPIO.add_event_detect(m["gpio"], GPIO.RISING, callback=sentMessage);
  108.  
  109.                
  110.     # s.write("c")
  111.     # print "Sende c"
  112.     # os.system ("sudo ./send 10101 1 1")
  113.    
  114.  
  115. #----------------------------------------------------------------------------
  116.  
  117.     if i==2:
  118.             a=2
  119.  
  120. #----------------------------------------------------------------------------
  121.  
  122.     if i==1:
  123.         #Deaktiviert
  124.         zaeler = zaeler + 1
  125. #----------------------------------------------------------------------------
  126.         time.sleep(0.2)
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
 
Top