Olimex Support Forum

Others => UEXT => Topic started by: GeorgeFlorian on February 08, 2021, 12:29:19 pm

Title: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 08, 2021, 12:29:19 pm
Hello.

I am interested in integrating the RFID1356MIFARE with the ESP32-EVB an using it as a card reader.

The first step was to connect the RFID to an USB hub using a mini-USB cable and selecting UART mode (both LEDs blinking at the same time).

The second step was to disconnect it from USB and connect it to the ESP32 using the included UEXT ribbon.

Next I uploaded the following code to the ESP:
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);

void setup() {
  enableCore1WDT();
  delay(100);
  Serial.begin(115200);

  nfc.begin();
  delay(100);

    uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    // while (1) delay(1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
 
  // configure board to read RFID tags
  nfc.SAMConfig();
 
  Serial.println("Waiting for an ISO14443A Card ...");
 
// ...
}

void loop()
{
  delay(1);

  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
   
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
 
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
   
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ...
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
   
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
   
   // Start with block 4 (the first block of sector 1) since sector 0
   // contains the manufacturer data and it's probably better just
   // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
   
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
 
        // If you want to write something to block 4 to test with, uncomment
 // the following line and this text should be read back in a minute
        // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
        // success = nfc.mifareclassic_WriteDataBlock (4, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);
 
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
          nfc.PrintHexChar(data, 16);
          Serial.println("");
   
          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
   
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
   
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
 
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}


The above code uses https://github.com/elechouse/PN532 library.
Inside it I've modified:
void PN532_HSU::begin()
{
    _serial->begin(38400, SERIAL_8N1, 36, 4);
}

If I want to get a read I connect the 3.3V_UEXT_E jumper.

The code above returns only "Didn't find PN53x board".

If I change the code to a simple Serial read:
if(Serial1.available()) {
  Serial.print(Serial1.read());
  Serial.print(" ");
}

I get some random characters each time I connect the jumper, even when I don't have a card in front of the reader:
0 0 91 61 25 160 128 0 0 0 0 0 0 0 0 0 0 118 118 54 16 18 32 1 1 1 1 1 0 0 248 126 118 55 50 162 129 32 0 1 1 1 0 0 0 119 247 38 33 17 1 9 128 0 1 0 0 0 55 103 39 1 17 9 32 0 0 0 0 255 255 119 55 49 34 34 128 128 1 1 1 0 0 126 119 103 51 17 25 25 128 1 1 0 0 0 118 54 38 38 32 1 1 1 1 1 1 0 0 0 55 50 54 50 2 128 128 1 1 1 1 0 0 126 54 50 162 130 129 32 1 0 1 0 0 0 255 119 118 55 54 25 25 128 1 1 1 0 127 255 126 118 102 39 35 1 9 1 0 0 1 0 0 126 254 54 54 51 38 1 17 17 9 0 0 0 118 118 16 35 33 1 9 32 0 0 1 0 55 54 54 16 34 1 1 1 1 1 1 0 118 55 54 38 33 1 1 9 0 0 0 0 0 254 118 118 18 34 34 1 1 1 1 0 0 0 55 54 50 34 130 1 1 1 9 1 0 0 126 55 54 35 1 25 9 0 1 0 0 0 0 0 0 0 136 119 54 50 36 32 1 1 1 1 0 0 0 0 119 54 55 51 25 25 32 1 1 1 1 0 0 119 103 51 51 17 25 32 0 1 1 0 0 0 0 255 54 54 54 39 17 17 25 32 0 1 0 1 0 0 0 127 183 55 16 38 33 1 1 1 0 1 0 0 0 0 254 118 103 55 51 54 16 32 33 1 1 1 1 0 248 55 55 118 52 103 35 1 17 9 1 0 255 118 63 25 54 9 0 1 0 0 1 0 55 118 55 35 17 25 32 0 1 0 9 1 1 0 0 0 0 127 118 54 51 49 17 9 0 1 1 1 0 0 252 118 118 55 55 35 33 17 9 0 0 0 119 183 54 39 33 25 25 0 0 1 0 118 119 54 54 35 49 1 9 0 0 0 0 0 0 55 54 54 33 17 9 9 128 0 0 0 118 39 33 25 25 0 128 0 0 0 0 255 55 54 18 36 33 1 1 1 0 0 0 0 0 0 119 55 54 33 17 17 9 128 0 0 0 55 54 50 36 37 1 17 1 1 1 1 0 0 0 0 0 126 55 50 102 36 33 1 1 1 1 0 0 247 118 54 54 16 32 32 1 1 1 0 0 55 116 18 38 33 1 1 1 1 0 0 126 114 102 38 33 1 1 1 0 0 0 0 127 118 114 39 33 1 17 9 0 1 0 0 1 0 118 114 39 35 1 1 9 0 0 0 1 0 1 0 55 16 38 32 1 1 1 1 0 0 114 50 33 33 1 9 9 0 0 0 0 118 50 39 54 33 1 17 9

And when I put a card in front of the reader I get a normalized sequence. But I don't know where it starts or where it ends.

What is the data format of the RFID1356MIFARE output ?

What is the firmware version of RFID1356MIFARE ? Do they come shipped with the latest 2.20 firmware ? Do I have to update the firmare in order to use the RFID1356MIFARE ?

Thank you.

Later edit:

I've started thinking about connecting the RFID1356MIFARE reader using I2C interface, because that would basically mean using pins GPIO13 and GPIO16, right ?

But this prompts other questions:
- Is I2C possible with the RFID1356MIFARE board ? Where do I connect the wires with I2C ?
- In what mode should the board be to be able to use I2C ?
- Can the TX and RX pins of the ESP32-EVB be used for I2C communication instead of GPIO13 and GPIO16 ?
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 09, 2021, 03:38:52 pm
Firmware is described here: https://www.olimex.com/wiki/MOD-RFID1356MIFARE

There is also some description in the README.txt inside the package of the firmware here:

https://drive.google.com/file/d/1Fl1wAhNq9U5F0fSRMPaNGsKUO5fVeJWO/view?usp=sharing

QuoteWhat is the firmware of RFID1356MIFARE ?

It is code that allows to use RFID1356-MIFARE easier compared to using it without the firmware.

QuoteDo they come shipped with the latest 2.20 firmware ? Do I have to update the firmare in order to use the RFID1356MIFARE ?

Yes (if you purchased it recently and directly from us). If it is old purchase or unit that stayed longer on the shelves of the re-seller - most likely no. You can see the version with command "i" (without the quotes). Connect it to a personal computer, open terminal at 115200, make sure RFID is in CDC mode, type "i".

It is not required to use the latest version, but some improvements are additions are available in latest versions.

QuoteI've started thinking about connecting the RFID1356MIFARE reader using I2C interface, because that would basically mean using pins GPIO13 and GPIO16, right ?

No, no. RFID has UART option on UEXT. No I2C.

Quote- Is this possible with the RFID1356MIFARE board ? Where do I connect the wires ?

Use UEXT connectors. You need to connect GND, ESP32 RX to RFID TX, ESP32 TX to RFID RX. You need to power the RFID too, you can power it either via the USB or via the UEXT pin #1

Quote- In what mode should the board be to be able to use I2C ?

UART, UART. No I2C. Set it in UART mode to use UART.

Quote- Can the TX and RX pins of the ESP32-EVB be used instead of GPIO13 and GPIO16 for I2C communication ?

TX and RX pins are UART. Any free GPIO pin can be used for anything in ESP32 - it has amazing multiplexing and remapping options.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 09, 2021, 04:37:29 pm
Quote from: LubOlimex on February 09, 2021, 03:38:52 pmFirmware is described here: https://www.olimex.com/wiki/MOD-RFID1356MIFARE

I've bought the RFID1356MIFARE last week directly from Olimex.


I've managed to get some readable output using UART on ESP32:
13 10 45 65 65 48 52 70 48 50 67 13 10 69 82 82 58 48 120 49 52 13 10 62

// which translates to


-AA04F02C
ERR:0x14
>

This is the new code that runs inside the loop:
  start_time = millis();

  while(Serial1.available()<24 && (millis() - start_time) < MAX_MILLIS_TO_WAIT)
  {
    // hang here
  }

  if(Serial1.available()<24)
  {
    if(Serial1.available() != 0)
    {
      int foo[24];
      Serial.println("Error - < 24");

      for(int i=0; i<24; i++)
        foo[i] = Serial1.read();

      for(int i=0; i<24; i++)
        {
          Serial.print(foo[i]);
          Serial.print(" ");
        }

      Serial.println("\n");
      Serial1.flush();
    }
  }
  else
  {
    char array[24];
    for(int i=0; i<24; i++)
      array[i] = Serial1.read();
   
    for(int i=0; i<24; i++)
      {
        Serial.print(array[i]);
      }
      Serial.println();
      Serial1.flush();
      delay(1000);
  }

I've used USB-HID mode to check the RFID-card UUID and I get AA04F02C. Thus, I've concluded that the UART output is correct.

Now, the strange thing is that there is an error: ERR:0x14, which, as the documentation states, it represents: "Mifare: Authentication error (improper Key used)". But I have no idea what this means.

I'm also having trouble understanding the difference between single size UID and double size UID.
As per 'definition' https://rfidcard.com/types-of-uid-rfid-card/ : the single size UID has a size of 4bytes(Mifare Classic), which should be 32 bits and the double size UID has a size of 7bytes(Mifare Ultralight), which should be 56 bits.

But I receive only 24 bits (3 bytes) via UART: 13 10 45 65 65 48 52 70 48 50 67 13 10 69 82 82 58 48 120 49 52 13 10 62
I've tried increasing the size of the array from 24 to 32, but that just starts over with 13 10 45 65 etc. So the characters repeat themselves after 24.

Now, your documentation stops here. I can't get any more info from it.

 - I would like to know how to fix the error.
 - I would also like to know if AA04F02C is truly the card's ID or it breaks down in some other parts.
 - Why is the ESP32-EVB constanly outputs

I am asking because the libraries that I found all have support for different UID sizes and I also need to do that for this project.

Thank you.



Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 10, 2021, 08:25:22 am
You have the latest firmware, so no point of updating it.

It is best idea to leave ESP32-EVB aside for now. Just use a personal computer, USB cable, and your tags. This would reduce the chances of problems with ESP32-EVB or UART handling code.

Once you confirm that the computer setup works and once you get the comfortable with the MIFARE working, switch from the computer to the ESP32-EVB.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 10, 2021, 12:22:38 pm
Quote from: LubOlimex on February 10, 2021, 08:25:22 amYou have the latest firmware, so no point of updating it.

It is best idea to leave ESP32-EVB aside for now. Just use a personal computer, USB cable, and your tags. This would reduce the chances of problems with ESP32-EVB or UART handling code.

Once you confirm that the computer setup works and once you get the comfortable with the MIFARE working, switch from the computer to the ESP32-EVB.

Will do that that.

In the meantime can you please explain what is this error about: Mifare: Authentication error (improper Key used) ?

What authentication are we talking about ? Why do I get an authentication error on read on UART ? Where does it try to authenticate to ?
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 10, 2021, 03:01:45 pm
The error is pretty self-explanatory - you are trying to access memory that requires authentication. Did you wrote these cards? What type of cards are you using exactly? Maybe try other blank card.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 10, 2021, 05:26:31 pm
Quote from: LubOlimex on February 10, 2021, 03:01:45 pmThe error is pretty self-explanatory - you are trying to access memory that requires authentication. Did you wrote these cards? What type of cards are you using exactly? Maybe try other blank card.

I've tried two new and two used cards(that come from an working environment). All the cards return that error.

Now I'm simply trying to write to UART: Serial1.write("i"); and then read the response with Serial1.read() which returns nothing.

What data format does the RFID1356MIFARE expects on UART ? Is Serial1.write("i"); correct here ?

Do I have to configure the reader before using it ? Asking because I am looking at the command: mku - Unlocks read command and resets keys to ka=000000000000 and kb=FFFFFFFFFFFF;

Later edit:

I've connected to the reader with USB-CDC and I'm trying commands:
 - i returns :
         FRev: 2.20
         https://www.olimex.com/wiki/MOD-RFID1356MIFARE
 - erB returns ERR:0x10
 - er0 returns OK
 - er0,7 returns OK
 - kA returns Key A : 000000000000
 - kB returns Key B : 000000000000

I don't understand why erB returns Invalid parameter (range, format, ...) because it's listed as a valid command in the docs.

Also, when reading the new cards I get:
-AA06A82C
ERR:0x13

-AA04F02C
ERR:0x13

Does this mean the cards are bad ? They do have an UID.

The used cards work as expected and they return a number of blocks even when using er0,63

Also, I guess that the OK after the block sectors means that the authentication worked ?
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 11, 2021, 08:36:07 am
erB is not a valid command - B is parameter, it should be replaced by a integer that specifies the block you want to read; er0,7 is valid command

Is this test with a computer to read the cards?

The reason that I ask is that the first code that you used has some libraries that are probably incompatible with our firmware like:

PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: Brian on February 11, 2021, 09:38:13 am
Quote from: undefinedNow I'm simply trying to write to UART: Serial1.write("i"); and then read the response with Serial1.read() which returns nothing.

Maybe the firmware expect a CR ;) Try with Serial1.write("i\r"); or Serial1.println("i");


Quote from: undefinedI don't understand why erB returns Invalid parameter (range, format, ...) because it's listed as a valid command in the docs.

B is the block number in decimal, not hex. er11 will read block 0x0b if there is block 0x0b on the card.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 11, 2021, 08:04:16 pm
Quote from: LubOlimex on February 11, 2021, 08:36:07 amerB is not a valid command - B is parameter, it should be replaced by a integer that specifies the block you want to read; er0,7 is valid command

That makes a lot more sense. Thank you.

Quote from: LubOlimex on February 11, 2021, 08:36:07 amIs this test with a computer to read the cards?

The commands were sent from a Linux terminal using picocom -b 115200 /dev/ttyACM0 with the reader on USB-CDC mode.

The code is using this library: https://github.com/elechouse/PN532 and was used with the reader in UART mode.

Quote from: LubOlimex on February 11, 2021, 08:36:07 amThe reason that I ask is that the first code that you used has some libraries that are probably incompatible with our firmware like:

PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);

Can you please tell me what is your firmware compatible with ?
Do I have any chance of making it work with this library https://github.com/elechouse/PN532 ?

I am asking because it is the best library that supports PN532 over UART.
Your RFID reader supports PN532 over UART.

The integration should've been a breeze, but the docs are lacking in terms of information about how to integrate the reader with another microcontroller.
As I've already mentioned, I want to make an RFID reader using the RFID1356MIFARE with the ESP32-EVB. Both are Olimex products.

Can you please provide me with more information regarding this subject ?

For example: what does the firmware expects to receive in a command over UART ? Which of the following is correct:
- Serial1.write("i");
- Serial1.write("i\r");
- Serial1.write("i\n");
- Serial1.write("i\r\n");
- Serial1.println("i");

The only thing that the docs mention about UART is the fixed baud rate.

Quote from: Brian on February 11, 2021, 09:38:13 amMaybe the firmware expect a CR ;) Try with Serial1.write("i\r"); or Serial1.println("i");

This is a good catch. I have no idea what the firmware expects to receive on UART since there is 0 documentation on this.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 12, 2021, 08:26:46 am
Just start with some basic code at first before jumping to more complex stuff, as the one shown here:

https://www.olimex.com/forum/index.php?topic=7749.0
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 15, 2021, 12:29:27 pm
Quote from: LubOlimex on February 12, 2021, 08:26:46 amJust start with some basic code at first before jumping to more complex stuff, as the one shown here:

https://www.olimex.com/forum/index.php?topic=7749.0

I have done that, thank you.

Here is an example output that I currently get:
-0920A499
Block  0 : 09 20 A4 99 14 08 04 00 62 63 64 65 66 67 68 69
Block  1 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
OK

I am also able to send commands using Serial1.println("i");

I've set the reader to read only the first two blocks using the command er0 1

I've used the mku command on all of my cards, so I think that their keys have been reset.

Now, the thing is that I don't understand how the Mifare Authentication works, when and how do I need to use the authentication keys:

Other readers have "Load Authentication Keys" and "Authenticate" commands. Does this ?

What I still don't get is:

Questions specifically for my project:

I can only find documentation about other readers whose commands and responses differ from the RFID1356MIFARE.

I am asking these questions because you don't cover this information in your documentation.

Thank you.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 16, 2021, 10:23:04 am
Quote from: undefinedI am asking these questions because you don't cover this information in your documentation.

The main reason is that these question are already answered by the excellent MIFARE documentation published and maintained by NXP.
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 16, 2021, 01:45:35 pm
Quote from: LubOlimex on February 16, 2021, 10:23:04 am
Quote from: undefinedI am asking these questions because you don't cover this information in your documentation.

The main reason is that these question are already answered by the excellent MIFARE documentation published and maintained by NXP.

I've read through the Application Notes and the Datasheets. There is nothing regarding a WORK KEY or mkA command in there. These are things from your firmware that you do not document properly.

What is a work key ? What does the mkA command actually do ?

I've changed the keys using ka,X command and I can still read all the sectors from my cards. Please note that the cards have been in use and have God know what keys. But your reader reads every sector with any key. How is that possible ?

Also, if I set the reader to read more blocks, let's say 20, and I read the card I get two behaviors:
 - one is when I keep the card near the reader until it reads all the sectors and at the end of the block 20 it says OK
 - another is when I do not keep the card near the reader until it finishes reading the sectors up to 20, and it ends with ERR:0x14.

Why do I get a Mifare: Authentication error (improper Key used) when I don't read all the 20 sectors ?
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: LubOlimex on February 16, 2021, 02:43:49 pm
Work key is just key. mkA says to the firmware that the card would use key A.

This information about keys, authenticating mechanisms, and such is found in the datasheet of the card (tag). For the Mifare Classic ones this is the proper document where these things are explained:

https://www.nxp.com/docs/en/data-sheet/MF1S50YYX_V1.pdf
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: GeorgeFlorian on February 16, 2021, 03:35:42 pm
Quote from: LubOlimex on February 16, 2021, 02:43:49 pmWork key is just key. mkA says to the firmware that the card would use key A.

Wouldn't it have been easier to specify this in your docs ?

Also, I've changed the reader's keys using ka,X command and I can still read all the sectors from my cards. Please note that the cards have been in use and have God know what keys. But your reader reads every sector with any key. How is that possible ?

Also, if I set the reader to read more blocks, let's say 10 (er0 10), and I read the card I get two behaviors:
- one is when I keep the card near the reader until it reads all the sectors and at the end of the block 10 it says OK
- the second behavior is when I do not keep the card near the reader for the full read and it reads, for example, until block 3 and this prompts ERR:0x14.

(https://i.postimg.cc/KjbBNFq7/Screenshot-at-2021-02-16-15-31-10.png)

Why do I get a Mifare: Authentication error (improper Key used) when I don't read all the 10 sectors ?
Title: Re: How to use the RFID1356MIFARE with ESP32 on UART ?
Post by: Brian on February 16, 2021, 03:56:56 pm
QuoteAlso, if I set the reader to read more blocks, let's say 10 (er0 10), and I read the card I get two behaviors:
- one is when I keep the card near the reader until it reads all the sectors and at the end of the block 10 it says OK
- the second behavior is when I do not keep the card near the reader for the full read and it reads, for example, until block 3 and this prompts ERR:0x14.

Reading all 10 blocks take a while, so you have to keep card close. In your firmware you can show to client that reading is done by lighting green or red led, for example. Or other indication.