#!/usr/bin/env python
#-*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
macarte = 'xxxxxxx'    # remplacer les xxxxxxxx par uid de votre carte à accepter
 
LEDR = 33       # Led rouge
LEDJ = 35       # Led jaune
LEDV = 37       # Led verte
BOUTON = 40     # bouton porte
PORTE = 38      # relais porte
GPIO.setup(LEDR, GPIO.OUT)  # LED rouge en mode output
GPIO.setup(LEDJ, GPIO.OUT)  # LED jaune en mode output
GPIO.setup(LEDV, GPIO.OUT)  # LED verte en mode output
GPIO.setup(PORTE, GPIO.OUT)  # relais porte en mode output
GPIO.setup(BOUTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # bouton porte en mode intput
GPIO.output(LEDJ, GPIO.HIGH)  # LED jaune On
GPIO.output(LEDR, GPIO.LOW)   # LED rouge Off
GPIO.output(LEDV, GPIO.LOW)   # LED verte Off
GPIO.output(PORTE, GPIO.HIGH)  # porte fermée
MIFAREReader = MFRC522.MFRC522()
try:    
    while True:
        if (GPIO.input(BOUTON) == True):
            GPIO.output(PORTE, GPIO.HIGH)                   
                        
        else :      
            print "Bonjour , la porte est ouverte"  
            GPIO.output(PORTE, GPIO.LOW)                
            
        (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
        if status == MIFAREReader.MI_OK:
            print "Carte détectée"
        (status, uid) = MIFAREReader.MFRC522_Anticoll()
        if status == MIFAREReader.MI_OK:
            UIDcode = str(uid[0]) + str(uid[1]) + str(uid[2]) + str(uid[3])
            print UIDcode
            if UIDcode == macarte:
                
                    GPIO.output(LEDV, GPIO.HIGH)  # LED verte On
                    GPIO.output(LEDR, GPIO.LOW)   # LED rouge Off
                    GPIO.output(LEDJ, GPIO.LOW)   # LED jaune off                   
                    GPIO.output(PORTE, GPIO.LOW)   # Ouverture porte
                    print "Bonjour , la porte est ouverte"
                    time.sleep(5)                   
                    
                    GPIO.output(PORTE, GPIO.HIGH)   # Fermeture porte
                    print "la porte est fermée"
                    GPIO.output(LEDJ, GPIO.LOW)   # LED jaune off
                    GPIO.output(LEDV, GPIO.LOW)   # LED verte Off
                    GPIO.output(LEDR, GPIO.HIGH)  # LED rouge on
                    time.sleep(2)
                    GPIO.output(LEDJ, GPIO.HIGH)  # LED jaune On
                    GPIO.output(LEDR, GPIO.LOW)   # LED rouge Off
                    GPIO.output(LEDV, GPIO.LOW)   # LED verte Off
                    
                                            
            else:
                print "Carte non reconnue, accés non autorisé"
                GPIO.output(LEDR, GPIO.HIGH)  # LED rouge On
                GPIO.output(LEDJ, GPIO.LOW)   # LED jaune off
                time.sleep(2)
                GPIO.output(LEDR, GPIO.LOW)  # LED rouge Off                
                GPIO.output(LEDJ, GPIO.HIGH)  # LED jaune On
                
except KeyboardInterrupt:
    GPIO.cleanup()