Python Forum
Code doesn't loop after first RFID scan
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code doesn't loop after first RFID scan
#1
Hi all,

Please excuse me for maybe a dumb question within this forum but I am currently working on my university final project and need to do some coding for it. The reason why I'm posting is just because I haven't got much time left and the code I have "built" does not loop at all - there is the code:

import RPi.GPIO as GPIO
import sys
import MySQLdb
import MFRC522GPS
import time
from time import strftime,localtime,sleep
import logging
import thread

def connect():
    return MySQLdb.connect(host='', user='', passwd='', db='')
    MySQLdb.close()

MIFAREReader = MFRC522GPS.MFRC522()

def setGpio():

    GPIO.setwarnings(False)

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(40, GPIO.OUT, initial=GPIO.HIGH) #power indicator
    GPIO.setup(38, GPIO.OUT, initial=GPIO.HIGH) #red LED
    GPIO.setup(36, GPIO.OUT, initial=GPIO.HIGH) #green LED
    GPIO.setup(32, GPIO.OUT, initial=GPIO.HIGH) #blue LED

def write(cardId,row):
    db = connect()
    c = db.cursor()
    currentTime=strftime("%Y-%m-%d %H:%M:%S", localtime())
    c.execute("""INSERT INTO zone1 (rfid_no, ticket_type, time) VALUES (%s, %s, %s)""",(cardId, row[2], currentTime))
    db.commit()
    db.close()
    action = "Come In!"
    return action

try:
    setGpio()
    while True:
            GPIO.output(40, GPIO.LOW) #Power indicator
            print("=============================================")
            print("Please scan Smart-Band")
            print("=============================================")
            def identity():
                reading = True
                while reading:
        
                    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

                    if status == MIFAREReader.MI_OK:
                        print("Card detected. Identifying...")
                        GPIO.output(32, GPIO.LOW) #blue LED
    
                    (status,backData) = MIFAREReader.MFRC522_Anticoll()
                    if status == MIFAREReader.MI_OK:
                        print ("Card ID: "+str(backData[0])+""+str(backData[1])+""+str(backData[2])+""+str(backData[3])+""+str(backData[4]))
                        MIFAREReader.AntennaOff()
                        reading = False
                        return str(backData[0])+str(backData[1])+str(backData[2])+str(backData[3])+str(backData[4])                   
            def read():
                cardId=identity()
                return cardId 
                       
            def readTag(action):
                if (action):
                    cardId=read()
                    db = connect()
                    c = db.cursor()
                    find = c.execute("SELECT *FROM personal_info WHERE rfid_no = " + cardId)
                    if find:
                        for row in c.fetchall():
                                check = c.execute("SELECT zone_1 FROM access WHERE type=%s AND zone_1=%s", (row[2], 1))
                                if check:
                                    name = write(cardId,row)
                                    GPIO.output(32, GPIO.HIGH) #blue LED
                                    GPIO.output(36, GPIO.LOW) #green LED
                                    sleep(2.0)
                                    GPIO.output(36, GPIO.HIGH)
                                    print (name)
                                else:
                                    print "denied"
                                    GPIO.output(32, GPIO.HIGH) #blue LED
                                    GPIO.output(38, GPIO.HIGH) #red LED
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.LOW)
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.HIGH)
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.LOW)
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.HIGH)
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.LOW)
                                    sleep(0.2)
                                    GPIO.output(38, GPIO.HIGH)
            choice = readTag
            readTag(choice)
                   
except KeyboardInterrupt:
    GPIO.cleanup()
Being honest with you all, if I would have more time I would give it another attempt to find the problem but I really need some help now Sad Cry

And sorry, just realised that probably I had to post it in Homework section not here, I am happy if this post will be moved to other section if that will help me faster :)
Reply
#2
Quote:Being honest with you all, if I would have more time I would give it another attempt to find the problem
Make your attempt and ask if you still have problems!
Reply
#3
(May-01-2018, 02:10 AM)Larz60+ Wrote:
Quote:Being honest with you all, if I would have more time I would give it another attempt to find the problem
Make your attempt and ask if you still have problems!
Will work on it today and leave a comment whether there was any progress or not.
Reply
#4
Ok so seems like I've found the issue where I was identifying MIFAREReader outside def identity().
But now I faced with another problem which is "can't open device: too many open files, aborted", I had same problem with another code which is similar to this one but it had been fixed by closing MySQLdb, but with this one everything looks fine to me:
def connect():
    return MySQLdb.connect(host='', user='', passwd='', db='')
    MySQLdb.close()

def setGpio():
# Show no warnings
    GPIO.setwarnings(False)
# Use GPIO pin numbers
    GPIO.setmode(GPIO.BOARD)
# GPIO setting for LED
    GPIO.setup(40, GPIO.OUT, initial=GPIO.HIGH) #power indicator
    GPIO.setup(38, GPIO.OUT, initial=GPIO.HIGH) #red LED
    GPIO.setup(36, GPIO.OUT, initial=GPIO.HIGH) #green LED
    GPIO.setup(32, GPIO.OUT, initial=GPIO.HIGH) #blue LED
# Write new card registration to database 
def write(cardId,row):
    db = connect()
    c = db.cursor()
    currentTime=strftime("%Y-%m-%d %H:%M:%S", localtime())
    c.execute("""INSERT INTO zone1 (rfid_no, ticket_type, time) VALUES (%s, %s, %s)""",(cardId, row[2], currentTime))
    db.commit()
    db.close()
    action = "Come In!"
    return action

try:
    setGpio()
    while True:
            GPIO.output(40, GPIO.LOW) #Power indicator
            print("=============================================")
            print("Please scan Smart-Band")
            print("=============================================")
            
            def identity():
                reading = True
                while reading:
                    
                    MIFAREReader = MFRC522GPS.MFRC522()
        
                    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

                    if status == MIFAREReader.MI_OK:
                        print("Card detected. Identifying...")
                        GPIO.output(32, GPIO.LOW) #blue LED
    
                    (status,backData) = MIFAREReader.MFRC522_Anticoll()
                    if status == MIFAREReader.MI_OK:
                        print ("Card ID: "+str(backData[0])+""+str(backData[1])+""+str(backData[2])+""+str(backData[3])+""+str(backData[4]))
                        MIFAREReader.AntennaOff()
                        reading = False
                        return str(backData[0])+str(backData[1])+str(backData[2])+str(backData[3])+str(backData[4])
#Fetch Card UID                    
            def read():
                cardId=identity()
                return cardId 
                       
            def readTag(action):
                if (action):
#Identify ticket type
                    cardId=read()
                    db = connect()
                    c = db.cursor()
                    c.execute("SELECT *FROM personal_info WHERE rfid_no = " + cardId)
                    for row in c.fetchall():
                        print row[2]
                    check = c.execute("SELECT zone_1 FROM access WHERE type=%s AND zone_1=%s", (row[2], 1))
                    if check:
                                name = write(cardId,row)
                                GPIO.output(32, GPIO.HIGH) #blue LED
                                GPIO.output(36, GPIO.LOW) #green LED
                                sleep(2.0)
                                GPIO.output(36, GPIO.HIGH)
                                print (name)
                    else:
                                print "denied"
                                GPIO.output(32, GPIO.HIGH) #blue LED
                                GPIO.output(38, GPIO.HIGH) #red LED
                                sleep(0.2)
                                GPIO.output(38, GPIO.LOW)
                                sleep(0.2)
                                GPIO.output(38, GPIO.HIGH)
                                sleep(0.2)
                                GPIO.output(38, GPIO.LOW)
                                sleep(0.2)
                                GPIO.output(38, GPIO.HIGH)
                                sleep(0.2)
                                GPIO.output(38, GPIO.LOW)
                                sleep(0.2)
                                GPIO.output(38, GPIO.HIGH)
                    db.close()
            choice = readTag
            readTag(choice)
                   
except KeyboardInterrupt:
    GPIO.cleanup()
Any suggestions what is missing in this code Wall Wall Wall ?
Reply
#5
Since you are not showing imported files, I'm not sure this answer is correct.
But it looks like you are instantiating (line 38) each and every iteration of, not only the read loop, but multiplied by every iteration of the while True loop.
try moving the statement on line 38 to just before line 28.
Reply
#6
(May-01-2018, 11:01 AM)Larz60+ Wrote: Since you are not showing imported files, I'm not sure this answer is correct.
But it looks like you are instantiating (line 38) each and every iteration of, not only the read loop, but multiplied by every iteration of the while True loop.
try moving the statement on line 38 to just before line 28.

Ok, will try it and will keep you updated on results! Pray
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code works but doesn't give the right results colin_dent 2 710 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,781 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 886 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,477 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Scan for Bluetooth device korenron 0 2,616 Jan-10-2022, 01:06 PM
Last Post: korenron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,011 Dec-18-2021, 02:38 AM
Last Post: knight2000
  While loop doesn't exit KenHorse 3 2,005 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  My code doesn't work, can someone help me? aldasrasickas 5 2,796 Dec-21-2020, 02:26 PM
Last Post: aldasrasickas
  Convert looping home security to a one time scan of switches and sensors duckredbeard 0 1,750 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  scan drives in windows from Cygwin RRR 1 1,628 Nov-29-2020, 04:34 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020