PN532 RFID with ESP32 (ESP-IDF) Passing uint8_t UID to a string

325 views Asked by At

I am trying to write a fairly basic program in platformio using the esp-idf framework for an esp32. I have the main bits working, but i'm struggling with something that I thought would be very basic!

I am using the pn532 library to communicate with the RFID reader over SPI. This works really well. I can get it to output the UID to the log. What I am struggling to do, is do anything with that UID.

        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)
       // char myStr[] = "hello";


        // 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 = pn532_readPassiveTargetID(&nfc, PN532_MIFARE_ISO14443A, uid, &uidLength, 0);

        if (success)
        {
            // Display some basic information about the card
            ESP_LOGI(TAG, "Found an ISO14443A card");
            ESP_LOGI(TAG, "UID Length: %d bytes", uidLength);
            ESP_LOGI(TAG, "UID Value:");

            esp_log_buffer_hexdump_internal(TAG, uid, uidLength, ESP_LOG_INFO); 

The code above is from the library example. It stores the UID in a uint8_t buffer, which is fine. But how can I use this UID value as a string?

I was expecting something along the lines of uid.ToString(), but this doesn't exist. I'm new to esp-idf, so please be gentle! I have tried searching, I've tried various things like converter(uint8t uid) but i'm not sure how to use this properly as I couldn't get it to work. I found out about nul terminators, but what exactly are these, and how can I use them?

For now I was just trying to display the UID (as a string) on the oled display. but eventually I want to pass this to a RestfulAPI to do a lookup on a database table. I had this all working ok in Arduino IDE, but i'm trying to improve it!

Thank you for your help!

Andrew

1

There are 1 answers

1
Andrew Taylor On

Ok, I'm not sure whether or not this is the right way to do it. But after plenty of fiddling around I came up with this.

I also didn't appreciate that the uint8_t was an array.

I managed to output the data as a string using:

ESP_LOGI(TAG, "STRING UID NUMBER IS %x-%x-%x-%x", uid[0], uid[1], uid[2], uid[3]);

Is this correct? or is there a better way of accomplishing this?

I'm hoping now I can use this value to compare to a value in a sql DB.... but that will have to wait until i've got more time! :)

Thanks Andrew