MQTT - NFC/RFID Bricklet

This is the description of the MQTT API bindings for the NFC/RFID Bricklet. General information and technical specifications for the NFC/RFID Bricklet are summarized in its hardware description.

An installation guide for the MQTT API bindings is part of their general description.

Examples

The example code below is Public Domain (CC0 1.0).

Scan For Tags

Download (example-scan-for-tags.txt)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Change XYZ to the UID of your NFC/RFID Bricklet

setup:
    # Handle incoming state changed callbacks
    set current_tag_type to "mifare_classic"

    subscribe to tinkerforge/callback/nfc_rfid_bricklet/XYZ/state_changed
        if a message is received
            # message contains the current state as int with symbols and idle as bool
            if idle
                set current_tag_type to (current_tag_type + 1) % 3
                publish '{"tag_type": current_tag_type}' to tinkerforge/request/nfc_rfid_bricklet/XYZ/request_tag_id
            endif

            if state == "RequestTagIDReady"
                subscribe to tinkerforge/response/nfc_rfid_bricklet/XYZ/get_tag_id
                    if a message is received
                        # message contains tag_type, id_length and tag_id
                        print tag_type, id_length and tag_id
                    endif
                endsubscribe

                publish '' to tinkerforge/response/nfc_rfid_bricklet/XYZ/get_tag_id
            endif
        endif
    endsubscribe

    publish '{"register": true}' to tinkerforge/register/nfc_rfid_bricklet/XYZ/state_changed # Register state_changed callback

    # Start scan loop
    publish '{"tag_type": "mifare_classic"}' to tinkerforge/request/nfc_rfid_bricklet/XYZ/request_tag_id

API

All published payloads to and from the MQTT bindings are in JSON format.

If an error occures, the bindings publish a JSON object containing the error message as member _ERROR. It is published on the corresponding response topic: .../response/... for .../request/... and .../callback/... for .../register/....

Basic Functions

request/nfc_rfid_bricklet/<UID>/request_tag_id
Request:
  • tag_type – Type: int, Range: See symbols
Response:
  • no response

To read or write a tag that is in proximity of the NFC/RFID Bricklet you first have to call this function with the expected tag type as parameter. It is no problem if you don't know the tag type. You can cycle through the available tag types until the tag gives an answer to the request.

Currently the following tag types are supported:

  • Mifare Classic
  • NFC Forum Type 1
  • NFC Forum Type 2

After you call request/nfc_rfid_bricklet/<UID>/request_tag_id the NFC/RFID Bricklet will try to read the tag ID from the tag. After this process is done the state will change. You can either register the register/nfc_rfid_bricklet/<UID>/state_changed callback or you can poll request/nfc_rfid_bricklet/<UID>/get_state to find out about the state change.

If the state changes to RequestTagIDError it means that either there was no tag present or that the tag is of an incompatible type. If the state changes to RequestTagIDReady it means that a compatible tag was found and that the tag ID could be read out. You can now get the tag ID by calling request/nfc_rfid_bricklet/<UID>/get_tag_id.

If two tags are in the proximity of the NFC/RFID Bricklet, this function will cycle through the tags. To select a specific tag you have to call request/nfc_rfid_bricklet/<UID>/request_tag_id until the correct tag id is found.

In case of any Error state the selection is lost and you have to start again by calling request/nfc_rfid_bricklet/<UID>/request_tag_id.

The following symbols are available for this function:

For tag_type:

  • "mifare_classic" = 0
  • "type1" = 1
  • "type2" = 2
request/nfc_rfid_bricklet/<UID>/get_tag_id
Request:
  • empty payload
Response:
  • tag_type – Type: int, Range: See symbols
  • tid_length – Type: int, Range: [4, 7]
  • tid – Type: [int, ...], Length: 7, Range: [0 to 255]

Returns the tag type, tag ID and the length of the tag ID (4 or 7 bytes are possible length). This function can only be called if the NFC/RFID is currently in one of the Ready states. The returned ID is the ID that was saved through the last call of request/nfc_rfid_bricklet/<UID>/request_tag_id.

To get the tag ID of a tag the approach is as follows:

  1. Call request/nfc_rfid_bricklet/<UID>/request_tag_id
  2. Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  3. Call request/nfc_rfid_bricklet/<UID>/get_tag_id

The following symbols are available for this function:

For tag_type:

  • "mifare_classic" = 0
  • "type1" = 1
  • "type2" = 2
request/nfc_rfid_bricklet/<UID>/get_state
Request:
  • empty payload
Response:
  • state – Type: int, Range: See symbols
  • idle – Type: bool

Returns the current state of the NFC/RFID Bricklet.

On startup the Bricklet will be in the Initialization state. The initialization will only take about 20ms. After that it changes to Idle.

The functions of this Bricklet can be called in the Idle state and all of the Ready and Error states.

Example: If you call request/nfc_rfid_bricklet/<UID>/request_page, the state will change to RequestPage until the reading of the page is finished. Then it will change to either RequestPageReady if it worked or to RequestPageError if it didn't. If the request worked you can get the page by calling request/nfc_rfid_bricklet/<UID>/get_page.

The same approach is used analogously for the other API functions.

The following symbols are available for this function:

For state:

  • "initialization" = 0
  • "idle" = 128
  • "error" = 192
  • "request_tag_id" = 2
  • "request_tag_id_ready" = 130
  • "request_tag_id_error" = 194
  • "authenticating_mifare_classic_page" = 3
  • "authenticating_mifare_classic_page_ready" = 131
  • "authenticating_mifare_classic_page_error" = 195
  • "write_page" = 4
  • "write_page_ready" = 132
  • "write_page_error" = 196
  • "request_page" = 5
  • "request_page_ready" = 133
  • "request_page_error" = 197
request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page
Request:
  • page – Type: int, Range: [0 to 216 - 1]
  • key_number – Type: int, Range: See symbols
  • key – Type: [int, ...], Length: 6, Range: [0 to 255]
Response:
  • no response

Mifare Classic tags use authentication. If you want to read from or write to a Mifare Classic page you have to authenticate it beforehand. Each page can be authenticated with two keys: A (key_number = 0) and B (key_number = 1). A new Mifare Classic tag that has not yet been written to can be accessed with key A and the default key [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF].

The approach to read or write a Mifare Classic page is as follows:

  1. Call request/nfc_rfid_bricklet/<UID>/request_tag_id
  2. Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  3. If looking for a specific tag then call request/nfc_rfid_bricklet/<UID>/get_tag_id and check if the expected tag was found, if it was not found go back to step 1
  4. Call request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page with page and key for the page
  5. Wait for state to change to AuthenticatingMifareClassicPageReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  6. Call request/nfc_rfid_bricklet/<UID>/request_page or request/nfc_rfid_bricklet/<UID>/write_page to read/write page

The following symbols are available for this function:

For key_number:

  • "a" = 0
  • "b" = 1
request/nfc_rfid_bricklet/<UID>/write_page
Request:
  • page – Type: int, Range: [0 to 216 - 1]
  • data – Type: [int, ...], Length: 16, Range: [0 to 255]
Response:
  • no response

Writes 16 bytes starting from the given page. How many pages are written depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte (one page is written)
  • NFC Forum Type 1 page size: 8 byte (two pages are written)
  • NFC Forum Type 2 page size: 4 byte (four pages are written)

The general approach for writing to a tag is as follows:

  1. Call request/nfc_rfid_bricklet/<UID>/request_tag_id
  2. Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  3. If looking for a specific tag then call request/nfc_rfid_bricklet/<UID>/get_tag_id and check if the expected tag was found, if it was not found got back to step 1
  4. Call request/nfc_rfid_bricklet/<UID>/write_page with page number and data
  5. Wait for state to change to WritePageReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page.

request/nfc_rfid_bricklet/<UID>/request_page
Request:
  • page – Type: int, Range: [0 to 216 - 1]
Response:
  • no response

Reads 16 bytes starting from the given page and stores them into a buffer. The buffer can then be read out with request/nfc_rfid_bricklet/<UID>/get_page. How many pages are read depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte (one page is read)
  • NFC Forum Type 1 page size: 8 byte (two pages are read)
  • NFC Forum Type 2 page size: 4 byte (four pages are read)

The general approach for reading a tag is as follows:

  1. Call request/nfc_rfid_bricklet/<UID>/request_tag_id
  2. Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  3. If looking for a specific tag then call request/nfc_rfid_bricklet/<UID>/get_tag_id and check if the expected tag was found, if it was not found got back to step 1
  4. Call request/nfc_rfid_bricklet/<UID>/request_page with page number
  5. Wait for state to change to RequestPageReady (see request/nfc_rfid_bricklet/<UID>/get_state or register/nfc_rfid_bricklet/<UID>/state_changed callback)
  6. Call request/nfc_rfid_bricklet/<UID>/get_page to retrieve the page from the buffer

If you use a Mifare Classic tag you have to authenticate a page before you can read it. See request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page.

request/nfc_rfid_bricklet/<UID>/get_page
Request:
  • empty payload
Response:
  • data – Type: [int, ...], Length: 16, Range: [0 to 255]

Returns 16 bytes of data from an internal buffer. To fill the buffer with specific pages you have to call request/nfc_rfid_bricklet/<UID>/request_page beforehand.

Advanced Functions

request/nfc_rfid_bricklet/<UID>/get_identity
Request:
  • empty payload
Response:
  • uid – Type: string, Length: up to 8
  • connected_uid – Type: string, Length: up to 8
  • position – Type: char, Range: ["a" to "h", "z"]
  • hardware_version – Type: [int, ...], Length: 3
    • 0: major – Type: int, Range: [0 to 255]
    • 1: minor – Type: int, Range: [0 to 255]
    • 2: revision – Type: int, Range: [0 to 255]
  • firmware_version – Type: [int, ...], Length: 3
    • 0: major – Type: int, Range: [0 to 255]
    • 1: minor – Type: int, Range: [0 to 255]
    • 2: revision – Type: int, Range: [0 to 255]
  • device_identifier – Type: int, Range: [0 to 216 - 1]
  • _display_name – Type: string

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here. If symbolic output is not disabled, the device identifier is mapped to the corresponding name in the format used in topics.

The display name contains the NFC/RFID's name in a human readable form.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the corresponding .../register/... topic and an optional suffix. This suffix can be used to deregister the callback later.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

register/nfc_rfid_bricklet/<UID>/state_changed
Register Request:
  • register – Type: bool
Callback Response:
  • state – Type: int, Range: See symbols
  • idle – Type: bool

A callback can be registered for this event by publishing to the .../register/nfc_rfid_bricklet/<UID>/state_changed[/<SUFFIX>] topic with the payload "true". An added callback can be removed by publishing to the same topic with the payload "false". To support multiple (de)registrations, e.g. for message filtering, an optional suffix can be used.

If the callback is triggered, a message with it's payload is published under the corresponding .../callback/nfc_rfid_bricklet/<UID>/state_changed[/<SUFFIX>] topic for each registered suffix.

This callback is called if the state of the NFC/RFID Bricklet changes. See request/nfc_rfid_bricklet/<UID>/get_state for more information about the possible states.

The following symbols are available for this function:

For state:

  • "initialization" = 0
  • "idle" = 128
  • "error" = 192
  • "request_tag_id" = 2
  • "request_tag_id_ready" = 130
  • "request_tag_id_error" = 194
  • "authenticating_mifare_classic_page" = 3
  • "authenticating_mifare_classic_page_ready" = 131
  • "authenticating_mifare_classic_page_error" = 195
  • "write_page" = 4
  • "write_page_ready" = 132
  • "write_page_error" = 196
  • "request_page" = 5
  • "request_page_ready" = 133
  • "request_page_error" = 197