Code doesn't loop after first RFID scan - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Code doesn't loop after first RFID scan (/thread-9845.html) |
Code doesn't loop after first RFID scan - BadgerHack - Apr-30-2018 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 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 :) RE: Code doesn't loop after first RFID scan - Larz60+ - May-01-2018 Quote:Being honest with you all, if I would have more time I would give it another attempt to find the problemMake your attempt and ask if you still have problems! RE: Code doesn't loop after first RFID scan - BadgerHack - May-01-2018 (May-01-2018, 02:10 AM)Larz60+ Wrote:Will work on it today and leave a comment whether there was any progress or not.Quote:Being honest with you all, if I would have more time I would give it another attempt to find the problemMake your attempt and ask if you still have problems! RE: Code doesn't loop after first RFID scan - BadgerHack - May-01-2018 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 ? RE: Code doesn't loop after first RFID scan - Larz60+ - May-01-2018 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. RE: Code doesn't loop after first RFID scan - BadgerHack - May-01-2018 (May-01-2018, 11:01 AM)Larz60+ Wrote: Since you are not showing imported files, I'm not sure this answer is correct. Ok, will try it and will keep you updated on results! |