77lazy777
Posts: 4
Joined: Fri Jan 23, 2015 6:04 pm

Read data from NFC tag

Fri Feb 13, 2015 1:00 pm

I have a pi B+ (with WheezyRasp) and EXPORE NFC daughter-board and some NFC tags with some data. I want to read the data from my RasPi. So far I've only managed to read the UID of the NFC tag using the card-polling software provided by EXPLORE-NFC. Can someone please give me some pointers?

[Also, I'm sorry if this is the wrong place for the doubt]

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Read data from NFC tag

Fri Feb 13, 2015 1:26 pm

First step would be to identify the tags you have. ( I dont know the EXPLORE-NFC solution, but I know a lot about contactless-smartcards.) Either the EXPLORE-NFC library can tell you directly what kind of tags it is or you can deduce it from parameters the cards return after you have established contact. The parameters that can be used is usually called "SAK", "ATQ" or "ATS".

If you have an NFC enabled smartphone you can use the app "taginfo" from NXP also to identify ( and read the contents of ) your tags.

My guess would be that your tags are either Mifare Ultralight (has 7byte UID) or Mifare Classic (usually 4 byte UID). Both these cards has there memory organized in blocks/pages/sectors. Just google it and you should find good information.

So look in the API and see if you find functions that refer to those cardtypes or has "read_block/page/sector" in their names.

NB!
I just realised that my response relates more to when you want to use the tags outside of "NFC" standards. If you are only after NDEF data then the tag type is not that important and might be already handled automatically by the library.

This guy has same ideas as I maybe you can learn something from his code.
https://github.com/svvitale/nxppy

svvitale
Posts: 1
Joined: Tue Mar 10, 2015 7:48 pm

Re: Read data from NFC tag

Tue Mar 10, 2015 7:51 pm

Hi there,

I'm the maintainer of nxppy as mentioned in the post above. Perhaps I can help if you're still having trouble. Were you able to install the module and successfully read the UID of one of your NFC tags?

-Scott

77lazy777
Posts: 4
Joined: Fri Jan 23, 2015 6:04 pm

Re: Read data from NFC tag

Wed Mar 11, 2015 8:10 am

Yes, I have used the polling software and am able to read the UID of the tag. The thing is, I want to read the data stored in the tags (Mifare Ultralite and mifare classic tags). With some research I was able to read the data in the Mifare Ultralite which was stored in pages starting from 4. But I'm having trouble reading data from Mifare Classic, which has a different method of storing data. Do know of some API or anything that can help me read the data?

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Read data from NFC tag

Tue Mar 17, 2015 1:18 pm

In Mifare-1 (Classic) tags you have to know the key of the sector you are trying to read. So there is an "authentication" part of the process that you cant skip.

Each sector has 4 blocks, each block is 16 bytes. In the three first blocks in each sector you can store data, in the fourth you have the key data. There are two keys for each sector, A and B, each is 48 bit, or 6 bytes.

Typical key values:
A = 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
B = 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5
A&B = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
A&B = 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF

What function call are you using to read the UL data ?

77lazy777
Posts: 4
Joined: Fri Jan 23, 2015 6:04 pm

Re: Read data from NFC tag

Sun Mar 22, 2015 11:47 pm

Thanks for the reply.

So, I'm using:

block_data = mifare.read_block(4)
print(block_data)

and

mifare.write_block(10, b'abcd')

for UltraLite tags. And it works perfectly fine.

How do I read from MiFare Classic tags though??

lucat
Posts: 3
Joined: Thu May 28, 2015 10:01 pm

Re: Read data from NFC tag

Fri May 29, 2015 9:22 am

Hello Svvitale, I am an Italian student.
I'm working on a small project with Raspberry and Pi2 Explore NFC.
I installed your libraries nxppy and work well, I have some difficulty in writing pi + 4 bytes simultaneously, even if I start from a different block.
My biggest problem, however, is another, use this code 1UID.py to read the contents of the card UID Mifare Ultralight. I created a form in PHP 8.1 with winodos wampserver (+ ssh2) but I can not get the UID from the script 1UID.py, when the launch of PHP in Windows restiuisce me this:

Script UID
Authentication Successful! Resource id #3


script.php Windows 8.1 Wampserver PHP 5 (+ssh2 extension)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title> FORM HTML </title>
</head>
<body background="image\cyber4.jpg">
<body>

<h1> Script UID </h1>

<?php

$connect = ssh2_connect('192.168.178.27', 22);
if (ssh2_auth_password($connect, 'pi', 'raspberry')) {
echo "Authentication Successful!\n";
} else {
echo('Authentication Failed...');
}

$stream = ssh2_exec($connect,'/home/pi/Desktop/Prj/NFC_NXP/1UID.py');

stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream);
echo $stream
?>
</body>
</html>

script python on Raspberry pi2 (1UID.py)
import nxppy
import time

mifare = nxppy.Mifare()
#time.sleep(1)

# Select the first available tag and return the UID
uid = mifare.select()
print (uid);

# Read 16 bytes starting from block 10
# (each block is 4 bytes, so technically this reads blocks 10-13)
#block1 = mifare.read_block(10)
#block2 = mifare.read_block(11)
#block3 = mifare.read_block(12)
#block4 = mifare.read_block(13)
#blockread = block1
#print (blockread)

#time.sleep(2)

# Write a single block of 4 bytes and print the newblock written
#mifare.write_block(10, b'erch')
#mifare.write_block(11, b'triv')
#mifare.write_block(12, b'ison')
#mifare.write_block(13, b'ne')

#blockwrite = mifare.read_block(10)
#print (blockwrite)

Return to “Other projects”