Visualizzazione post con etichetta Python. Mostra tutti i post
Visualizzazione post con etichetta Python. Mostra tutti i post

venerdì 9 settembre 2016

Raspberry Pi - NFC door opener


As shown in the previous tutorial, using the MFRC522-python library (https://github.com/mxgxw/MFRC522-python.git) and the data read example script, I'm able to read the UniqueID (UID) of my NFC card.

On GPIO17 (= pin 11) I have the 220v relay signal that open the electric door lock. Therefore, editing the read example script, if an allowed UID is read than the door will be opened!

I add also a log file to control if somebody try to force the system
import RPi.GPIO as GPIO
import MFRC522
import signal
import time
import logging

# Logging
logging.basicConfig(filename='/var/www/nfc.log',
                            filemode='a',
                            format='%(asctime)s %(levelname)s %(message)s',
                            datefmt='%d/%m/%Y %H:%M:%S',
                            level=logging.DEBUG)
logging.info("script initialized")

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# 220v relay - electric door opener
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(11, GPIO.OUT) ## Setup GPIO17 -> pin 11 to OUT
GPIO.output(11,1)

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while true:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
    
          # Get the UID of the card
          (status,uid) = MIFAREReader.MFRC522_Anticoll()

          # If we have the UID, continue
          if status == MIFAREReader.MI_OK:

          # My card UID
myUID = [141,113,243,56, 231]

# Check if authenticated
          if uid == myUID:
                  # open the door
                 GPIO.output(11,0)
                 time.sleep(1)
                 GPIO.output(11,1)
                  # save to log file
                  logging.info('Authenticated uid = %s' % uid)
else:
                 logging.error('Authentication failed uid = %s' % uid)

RFID-RC522 on Raspberry PI (Python)

Enable SPI interface

Edit /boot/config.txt and add the following lines
device_tree_param=spi=on
dtoverlay=spi-bcm2708
Reboot  the device.

Install Python header files

sudo apt-get install python-dev

Install SPI libraries

git clone https://github.com/lthiery/SPI-Py.git
cd SPI-Py
sudo python setup.py install

Test RFID-RC522

Use the following schema to link the RFID-RC522 to the GPIO
Name Pin # Pin name
SDA 24 GPIO8
SCK 23 GPIO11
MOSI 19 GPIO10
MISO 21 GPIO9
IRQ None None
GND Any Any Ground
RST 22 GPIO25
3.3V 1 3V3

Download the RFID-RC522 interface and test it
git clone https://github.com/mxgxw/MFRC522-python.git
cd MFRC522-python
sudo python Read.py
Touching the reader with a card, the output must be
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
Card read UID: 141,113,243,56
Size: 8
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]