Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import MFRC522
  6. import signal
  7. import time
  8.  
  9. import os
  10.  
  11. GPIO.setmode(GPIO.BOARD)
  12.  
  13. # BUZZER NOTIFICA
  14. GPIO.setup(15, GPIO.OUT)
  15.  
  16. # PIN LED
  17. GPIO.setup(16, GPIO.OUT) #LED ROSSO
  18. GPIO.setup(18, GPIO.OUT) #LED VERDE
  19.  
  20.  
  21. GPIO.setup(7, GPIO.IN)   # button pin setup
  22.  
  23. #INSERIMENTO 1, RIMOZIONE 0
  24. mode_dispensa = 0
  25. GPIO.output(16, False)
  26. GPIO.output(18, True)
  27.  
  28. def my_callback(signal):
  29.     global mode_dispensa
  30.     if mode_dispensa==0:
  31.         mode_dispensa=1
  32.         GPIO.output(16, True)
  33.         GPIO.output(18, False)
  34.     else:
  35.         mode_dispensa=0
  36.         GPIO.output(16, False)
  37.         GPIO.output(18, True)
  38.  
  39. GPIO.add_event_detect(7, GPIO.FALLING, callback=my_callback, bouncetime=300)  
  40.  
  41. continue_reading = True
  42.  
  43. # Capture SIGINT for cleanup when the script is aborted
  44. def end_read(signal,frame):
  45.     global continue_reading
  46.     print "Ctrl+C captured, ending read."
  47.     continue_reading = False
  48.     GPIO.cleanup()
  49.  
  50. # Hook the SIGINT
  51. signal.signal(signal.SIGINT, end_read)
  52.  
  53. # Create an object of the class MFRC522
  54. MIFAREReader = MFRC522.MFRC522()
  55.  
  56. # Welcome message
  57. print "Welcome to the MFRC522 data read example"
  58. print "Press Ctrl-C to stop."
  59.  
  60.  
  61.  
  62.  
  63. # This loop keeps checking for chips. If one is near it will get the UID and authenticate
  64. while continue_reading:
  65.  
  66.     # Scan for cards    
  67.     (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  68.  
  69.     # Get the UID of the card
  70.     (status,uid) = MIFAREReader.MFRC522_Anticoll()
  71.  
  72.     # If we have the UID, continue
  73.     if status == MIFAREReader.MI_OK:
  74.  
  75.         # Print UID
  76.    
  77.         # This is the default key for authentication
  78.         key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  79.        
  80.         # Select the scanned tag
  81.         MIFAREReader.MFRC522_SelectTag(uid)
  82.  
  83.         # Authenticate
  84.         status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
  85.  
  86.         # Check if authenticated
  87.         if status == MIFAREReader.MI_OK:
  88.  
  89.             #print "It now looks like this:"
  90.             # Check to see if it was written
  91.             backdata = MIFAREReader.MFRC522_Read(8)
  92.             #print "\n"
  93.  
  94.             # Turn Buzzer on
  95.             GPIO.output(15, True)
  96.  
  97.             # Wait 1 second
  98.             time.sleep(0.3)
  99.  
  100.             # Turn Buzzer off
  101.             GPIO.output(15, False)
  102.            
  103.             #print backdata
  104.                
  105.             contenuto_tag = ''.join(map(chr, filter(None, backdata)))
  106.            
  107.             os.system("php %s/../trigger_RFID.php %s %d"%(os.path.dirname(os.path.abspath(__file__)),contenuto_tag, mode_dispensa))
  108.            
  109.  
  110.            
  111.             MIFAREReader.MFRC522_StopCrypto1()
  112.            
  113.         else:
  114.             print "Authentication error"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement