Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. Traceback (most recent call last): File "rfidreader.py", line 102, in <module>
  2.  
  3. "INSERT INTO Attendance VALUES ('"+str (uid) +"','"+str (cardtypename) +"')"
  4.  
  5. NameError: name 'uid' is not defined
  6.  
  7. #!/usr/bin/env python
  8. # -*- coding: utf8 -*-
  9.  
  10. import RPi.GPIO as GPIO
  11. import MFRC522
  12. import signal
  13. import time
  14. from os import getenv
  15. import pymssql
  16.  
  17. server = getenv("PYMSSQL_TEST_SERVER")
  18. user = getenv("PYMSSQL_TEST_USERNAME")
  19. password = getenv("PYMSSQL_TEST_PASSWORD")
  20. conn = pymssql.connect("192.168.1.20:1433", "sa", "Tne-project2017", "project")
  21.  
  22.  
  23. continue_reading = True
  24. # the uid of the last recognized card
  25. lastcarduid = None
  26. # the time a card uid was last seen
  27. lastcardtime = 0.0
  28.  
  29. # this long after a card has been noticed, it can be noticed again
  30. # This works because the reader generates repeated notifications of the card
  31. # as it is held agains the reader - faster than this interval.
  32. # The timer is restarted every time the reader generates a uid.
  33. # If you sometimes get repeated new card IDs when holding a card against the
  34. # reader, increase this a little bit.
  35. CARDTIMEOUT = 1.0
  36.  
  37. # Capture SIGINT for cleanup when the script is aborted
  38. def end_read(signal,frame):
  39. global continue_reading
  40. print "Ctrl+C captured, ending read."
  41. continue_reading = False
  42. GPIO.cleanup()
  43.  
  44. # Hook the SIGINT
  45. signal.signal(signal.SIGINT, end_read)
  46.  
  47. # Create an object of the class MFRC522
  48. MIFAREReader = MFRC522.MFRC522()
  49.  
  50. ## Welcome message
  51. #print "Welcome to the MFRC522 data read example"
  52. #print "Press Ctrl-C to stop."
  53.  
  54. def gettypename( typecode ):
  55. typecode &= 0x7F;
  56. if typecode == 0x00:
  57. return "MIFARE Ultralight or Ultralight C"
  58. elif typecode == 0x01:
  59. return "MIFARE TNP3XXX"
  60. elif typecode == 0x04:
  61. return "SAK indicates UID is not complete"
  62. elif typecode == 0x08:
  63. return "MIFARE 1KB"
  64. elif typecode == 0x09:
  65. return "MIFARE Mini, 320 bytes"
  66. elif typecode == 0x10 or typecode == 0x11:
  67. return "MIFARE Plus"
  68. elif typecode == 0x18:
  69. return "MIFARE 4KB"
  70. elif typecode == 0x20:
  71. return "PICC compliant with ISO/IEC 14443-4"
  72. elif typecode == 0x40:
  73. return "PICC compliant with ISO/IEC 18092 (NFC)"
  74. return "Unknown type";
  75.  
  76. # This loop keeps checking for chips. If one is near it will get the UID and authenticate
  77. while continue_reading:
  78. # Scan for cards
  79. (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  80.  
  81. # Get the UID of the card
  82. (status,rawuid) = MIFAREReader.MFRC522_Anticoll()
  83.  
  84. # If we have a good read of the UID, process it
  85. if status == MIFAREReader.MI_OK:
  86. uid = "{0[0]:02x}{0[1]:02x}{0[2]:02x}{0[3]:02x}".format(rawuid)
  87. # # Print UID
  88. # print "Card read UID: "+uid
  89. newcard = False
  90. if lastcarduid:
  91. if lastcarduid != uid or (lastcardtime and time.clock() - lastcardtime >= CARDTIMEOUT):
  92. newcard = True
  93. else:
  94. newcard = True
  95.  
  96. if newcard:
  97. # print "New Card read UID: "+uid
  98. # String payload = "{"d":{"cardUID":"";
  99. # payload += UID_HEX;
  100. # payload += "","cardtype":"";
  101. # payload += mfrc522.PICC_GetTypeName(piccType);
  102. # payload += ""}}";
  103. rawcardtype = MIFAREReader.MFRC522_SelectTag(rawuid)
  104. cardtypename = gettypename( rawcardtype )
  105. print '{ "cardUID": "'+uid+'","cardtype":"'+cardtypename+'" }'
  106. cursor = conn.cursor()
  107. cursor.execute(
  108. "INSERT INTO Attendance VALUES ('"+str (uid) +"','"+str (cardtypename) +"')"
  109. )
  110. # you must call commit() to persist your data if you don't set autocommit to True
  111. conn.commit()
  112. conn.close()
  113. # remember the last card uid recognized
  114. lastcarduid = uid
  115. # remember when it was seen
  116. lastcardtime = time.clock()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement