|
1 | 1 | ''' |
2 | 2 | Simple Pyscan NFC / MiFare Classic Example |
3 | | -Copyright (c) 2020, Pycom Limited. |
| 3 | +Copyright (c) 2019, Pycom Limited. |
4 | 4 |
|
5 | | -This example runs the NFC discovery loop in a thread. |
6 | | -If a card is detected it will read the UID and compare it to VALID_CARDS |
7 | | -RGB LED is BLUE while waiting for card, |
8 | | -GREEN if card is valid, RED if card is invalid |
| 5 | +This example continuously sends a REQA for ISO14443A card type |
| 6 | +If a card is discovered, it will read the UID |
| 7 | +If DECODE_CARD = True, will attempt to authenticate with CARDkey |
| 8 | +If authentication succeeds will attempt to read sectors from the card |
9 | 9 | ''' |
10 | 10 |
|
11 | | -DEBUG = False # change to True to see debug messages |
12 | | - |
13 | 11 | from pycoproc_1 import Pycoproc |
14 | 12 | from MFRC630 import MFRC630 |
15 | 13 | from LIS2HH12 import LIS2HH12 |
16 | 14 | from LTR329ALS01 import LTR329ALS01 |
17 | | -import binascii |
18 | 15 | import time |
19 | 16 | import pycom |
20 | | -import _thread |
21 | 17 |
|
| 18 | +#add your card UID here |
22 | 19 | VALID_CARDS = [[0x43, 0x95, 0xDD, 0xF8], |
23 | 20 | [0x43, 0x95, 0xDD, 0xF9], |
24 | 21 | [0x46, 0x5A, 0xEB, 0x7D, 0x8A, 0x08, 0x04]] |
25 | 22 |
|
| 23 | + |
| 24 | +# This is the default key for an unencrypted MiFare card |
| 25 | +CARDkey = [ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ] |
| 26 | +DECODE_CARD = False |
| 27 | + |
26 | 28 | py = Pycoproc(Pycoproc.PYSCAN) |
27 | 29 | nfc = MFRC630(py) |
28 | 30 | lt = LTR329ALS01(py) |
29 | 31 | li = LIS2HH12(py) |
30 | 32 |
|
| 33 | +pybytes_enabled = False |
| 34 | +if 'pybytes' in globals(): |
| 35 | + if(pybytes.isconnected()): |
| 36 | + print('Pybytes is connected, sending signals to Pybytes') |
| 37 | + pybytes_enabled = True |
| 38 | + |
31 | 39 | RGB_BRIGHTNESS = 0x8 |
32 | 40 |
|
33 | 41 | RGB_RED = (RGB_BRIGHTNESS << 16) |
34 | 42 | RGB_GREEN = (RGB_BRIGHTNESS << 8) |
35 | 43 | RGB_BLUE = (RGB_BRIGHTNESS) |
36 | 44 |
|
| 45 | +counter = 0 |
| 46 | + |
| 47 | +def check_uid(uid, len): |
| 48 | + return VALID_CARDS.count(uid[:len]) |
| 49 | + |
| 50 | +def send_sensor_data(name, timeout): |
| 51 | + if(pybytes_enabled): |
| 52 | + while(True): |
| 53 | + pybytes.send_signal(2, lt.light()) |
| 54 | + pybytes.send_signal(3, li.acceleration()) |
| 55 | + time.sleep(timeout) |
| 56 | + |
37 | 57 | # Make sure heartbeat is disabled before setting RGB LED |
38 | 58 | pycom.heartbeat(False) |
39 | 59 |
|
40 | 60 | # Initialise the MFRC630 with some settings |
41 | 61 | nfc.mfrc630_cmd_init() |
42 | 62 |
|
43 | | -def check_uid(uid, len): |
44 | | - return VALID_CARDS.count(uid[:len]) |
45 | | - |
46 | | -def print_debug(msg): |
47 | | - if DEBUG: |
48 | | - print(msg) |
49 | | - |
50 | | -def send_sensor_data(name, timeout): |
51 | | - while(True): |
52 | | - print(lt.light()) |
53 | | - print(li.acceleration()) |
54 | | - time.sleep(timeout) |
55 | | - |
56 | | -def discovery_loop(nfc, id): |
57 | | - while True: |
58 | | - # Send REQA for ISO14443A card type |
59 | | - print_debug('Sending REQA for ISO14443A card type...') |
60 | | - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) |
61 | | - print_debug('Response: {}'.format(atqa)) |
62 | | - if (atqa != 0): |
63 | | - # A card has been detected, read UID |
64 | | - print_debug('A card has been detected, read UID...') |
65 | | - uid = bytearray(10) |
66 | | - uid_len = nfc.mfrc630_iso14443a_select(uid) |
67 | | - print_debug('UID has length: {}'.format(uid_len)) |
68 | | - if (uid_len > 0): |
69 | | - print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) |
| 63 | +print('Scanning for cards') |
| 64 | +while True: |
| 65 | + # Send REQA for ISO14443A card type |
| 66 | + atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) |
| 67 | + if (atqa != 0): |
| 68 | + # A card has been detected, read UID |
| 69 | + print('A card has been detected, reading its UID ...') |
| 70 | + uid = bytearray(10) |
| 71 | + uid_len = nfc.mfrc630_iso14443a_select(uid) |
| 72 | + print('UID has length {}'.format(uid_len)) |
| 73 | + if (uid_len > 0): |
| 74 | + # A valid UID has been detected, print details |
| 75 | + counter += 1 |
| 76 | + print("%d\tUID [%d]: %s" % (counter, uid_len, nfc.format_block(uid, uid_len))) |
| 77 | + if DECODE_CARD: |
| 78 | + # Try to authenticate with CARD key |
| 79 | + nfc.mfrc630_cmd_load_key(CARDkey) |
| 80 | + for sector in range(0, 16): |
| 81 | + if (nfc.mfrc630_MF_auth(uid, nfc.MFRC630_MF_AUTH_KEY_A, sector * 4)): |
| 82 | + pycom.rgbled(RGB_GREEN) |
| 83 | + # Authentication was sucessful, read card data |
| 84 | + readbuf = bytearray(16) |
| 85 | + for b in range(0, 4): |
| 86 | + f_sect = sector * 4 + b |
| 87 | + len = nfc.mfrc630_MF_read_block(f_sect, readbuf) |
| 88 | + print("\t\tSector %s: Block: %s: %s" % (nfc.format_block([sector], 1), nfc.format_block([b], 1), nfc.format_block(readbuf, len))) |
| 89 | + else: |
| 90 | + print("Authentication denied for sector %s!" % nfc.format_block([sector], 1)) |
| 91 | + pycom.rgbled(RGB_RED) |
| 92 | + # It is necessary to call mfrc630_MF_deauth after authentication |
| 93 | + # Although this is also handled by the reset / init cycle |
| 94 | + nfc.mfrc630_MF_deauth() |
| 95 | + else: |
| 96 | + #check if card uid is listed in VALID_CARDS |
70 | 97 | if (check_uid(list(uid), uid_len)) > 0: |
71 | | - print_debug('Card is listed, turn LED green') |
| 98 | + print('Card is listed, turn LED green') |
72 | 99 | pycom.rgbled(RGB_GREEN) |
| 100 | + if(pybytes_enabled): |
| 101 | + pybytes.send_signal(1, ('Card is listed', uid)) |
73 | 102 | else: |
74 | | - print_debug('Card is not listed, turn LED red') |
| 103 | + print('Card is not listed, turn LED red') |
75 | 104 | pycom.rgbled(RGB_RED) |
76 | | - else: |
77 | | - # No card detected |
78 | | - print_debug('Did not detect any card...') |
79 | | - pycom.rgbled(RGB_BLUE) |
80 | | - nfc.mfrc630_cmd_reset() |
81 | | - time.sleep(.5) |
82 | | - nfc.mfrc630_cmd_init() |
83 | | - |
84 | | -# This is the start of our main execution... start the thread |
85 | | -_thread.start_new_thread(discovery_loop, (nfc, 0)) |
86 | | -_thread.start_new_thread(send_sensor_data, ('Thread 2', 10)) |
| 105 | + if(pybytes_enabled): |
| 106 | + pybytes.send_signal(1, ('Unauthorized card detected', uid)) |
| 107 | + |
| 108 | + else: |
| 109 | + pycom.rgbled(RGB_BLUE) |
| 110 | + # We could go into power saving mode here... to be investigated |
| 111 | + nfc.mfrc630_cmd_reset() |
| 112 | + time.sleep(.5) |
| 113 | + # Re-Initialise the MFRC630 with settings as these got wiped during reset |
| 114 | + nfc.mfrc630_cmd_init() |
0 commit comments