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.
The example code below is Public Domain (CC0 1.0).
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/....
| Request: |
|
|---|---|
| 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: |
|
|---|---|
| Response: |
|
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:
Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state() or
register/nfc_rfid_bricklet/<UID>/state_changed() callback)
The following symbols are available for this function:
For tag_type:
"mifare_classic" = 0
"type1" = 1
"type2" = 2
| Request: |
|
|---|---|
| Response: |
|
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: |
|
|---|---|
| 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:
Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state()
or register/nfc_rfid_bricklet/<UID>/state_changed() callback)
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
Call request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page() with page and key for the page
Wait for state to change to AuthenticatingMifareClassicPageReady (see
request/nfc_rfid_bricklet/<UID>/get_state() or register/nfc_rfid_bricklet/<UID>/state_changed() callback)
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: |
|
|---|---|
| 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:
Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state() or
register/nfc_rfid_bricklet/<UID>/state_changed() callback)
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
Call request/nfc_rfid_bricklet/<UID>/write_page() with page number and data
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: |
|
|---|---|
| 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:
Wait for state to change to RequestTagIDReady (see request/nfc_rfid_bricklet/<UID>/get_state()
or register/nfc_rfid_bricklet/<UID>/state_changed() callback)
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
Call request/nfc_rfid_bricklet/<UID>/request_page() with page number
Wait for state to change to RequestPageReady (see request/nfc_rfid_bricklet/<UID>/get_state()
or register/nfc_rfid_bricklet/<UID>/state_changed() callback)
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: |
|
|---|---|
| Response: |
|
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.
| Request: |
|
|---|---|
| Response: |
|
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 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 Request: |
|
|---|---|
| Callback Response: |
|
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