#!/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(LEDRGPIO.OUT)  # LED rouge en mode output
GPIO.setup(LEDJGPIO.OUT)  # LED jaune en mode output
GPIO.setup(LEDVGPIO.OUT)  # LED verte en mode output
GPIO.setup(PORTEGPIO.OUT)  # relais porte en mode output
GPIO.setup(BOUTONGPIO.INpull_up_down=GPIO.PUD_UP)  # bouton porte en mode intput

GPIO.output(LEDJGPIO.HIGH)  # LED jaune On
GPIO.output(LEDRGPIO.LOW)   # LED rouge Off
GPIO.output(LEDVGPIO.LOW)   # LED verte Off
GPIO.output(PORTEGPIO.HIGH)  # porte fermée

MIFAREReader MFRC522.MFRC522()

try:    
    while True:
        if (GPIO.input(BOUTON) == True):
            GPIO.output(PORTEGPIO.HIGH)                   
                        
        else :      
            print "Bonjour , la porte est ouverte"  
            GPIO.output(PORTEGPIO.LOW)                
            
        (statusTagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

        if status == MIFAREReader.MI_OK:
            print "Carte détectée"

        (statusuid) = 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(LEDVGPIO.HIGH)  # LED verte On
                    GPIO.output(LEDRGPIO.LOW)   # LED rouge Off
                    GPIO.output(LEDJGPIO.LOW)   # LED jaune off                   
                    GPIO.output(PORTEGPIO.LOW)   # Ouverture porte
                    print "Bonjour , la porte est ouverte"
                    time.sleep(5)                   
                    
                    GPIO.output(PORTEGPIO.HIGH)   # Fermeture porte
                    print "la porte est fermée"
                    GPIO.output(LEDJGPIO.LOW)   # LED jaune off
                    GPIO.output(LEDVGPIO.LOW)   # LED verte Off
                    GPIO.output(LEDRGPIO.HIGH)  # LED rouge on
                    time.sleep(2)
                    GPIO.output(LEDJGPIO.HIGH)  # LED jaune On
                    GPIO.output(LEDRGPIO.LOW)   # LED rouge Off
                    GPIO.output(LEDVGPIO.LOW)   # LED verte Off
                    
                                            
            else:
                print "Carte non reconnue, accés non autorisé"
                GPIO.output(LEDRGPIO.HIGH)  # LED rouge On
                GPIO.output(LEDJGPIO.LOW)   # LED jaune off
                time.sleep(2)
                GPIO.output(LEDRGPIO.LOW)  # LED rouge Off                
                GPIO.output(LEDJGPIO.HIGH)  # LED jaune On
                
except KeyboardInterrupt:
    GPIO.cleanup()