Sbs-ula
Posts: 24
Joined: Mon Oct 23, 2017 11:22 am

Database for NFC reader

Sun Nov 26, 2017 5:10 pm

Hello everyone

I have two raspberrys on different locations with NFC readers (RC522). They have the basic python program to open the door with one relay:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MFRC522
import signal
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(31, GPIO.OUT)
GPIO.output(31, GPIO.LOW)

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print " -- Beendet!"
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

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

def relay():
     GPIO.output(35, GPIO.LOW)
     time.sleep(2.4)
     GPIO.output(35, GPIO.HIGH)
     GPIO.cleanup()


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

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

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

        if uid == [11,19,92,128,247]:
	    relay()
        elif uid == [26,4,150,12,22]:
	    relay()
        elif uid == [2,4,29,75,10]:
	    relay()
        elif uid == [1,4,0,3,23]:
	    relay()
        elif uid == [16,4,11,7,6]:
	    relay()
        elif uid == [11,4,15,9,30]:
	    relay()
        elif uid == [11,4,18,68,6]:
	    relay()
        elif uid == [16,4,5,13,6]:
	    relay()
        elif uid == [11,4,20,234,186]:
	    relay()
        elif uid == [11,4,3,73,13]:
	    relay()

        else:
            print "wrong tag"

You can see there are many if arguments and I have already shorted the list. And the same program work on the secound raspberry.

I think if I have one database (with sql or what else) on one server (maybe one of the raspberry), then can I manage it very better.

Have you any ideas for this problem?

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Database for NFC reader

Sun Nov 26, 2017 9:16 pm

Yes this is a classic example of where a database is needed. You make the data base with all the tags in it then when a tag gets scanned you look up the database to see if that tag is in it. :)

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Database for NFC reader

Mon Nov 27, 2017 9:15 am

There's some code I built at this weekend's Basingstoke Hackathon on github, it's not fully debugged, but it publishes MQTT messages when an RFID is seen by a reader. I've got a MySQL database in my design to hold card credentials.

https://github.com/DougieLawson/BSK_Hackathon

Our design has a funky addition that a "presence" event published from a card reader will trigger a music player.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”