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).
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
|
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/
nfc_rfid_bricklet/
<UID>/
request_tag_id
¶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:
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:
request/
nfc_rfid_bricklet/
<UID>/
get_tag_id
¶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:
request/nfc_rfid_bricklet/<UID>/request_tag_id
request/nfc_rfid_bricklet/<UID>/get_state
or
register/nfc_rfid_bricklet/<UID>/state_changed
callback)request/nfc_rfid_bricklet/<UID>/get_tag_id
The following symbols are available for this function:
For tag_type:
request/
nfc_rfid_bricklet/
<UID>/
get_state
¶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:
request/
nfc_rfid_bricklet/
<UID>/
authenticate_mifare_classic_page
¶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:
request/nfc_rfid_bricklet/<UID>/request_tag_id
request/nfc_rfid_bricklet/<UID>/get_state
or register/nfc_rfid_bricklet/<UID>/state_changed
callback)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 1request/nfc_rfid_bricklet/<UID>/authenticate_mifare_classic_page
with page and key for the pagerequest/nfc_rfid_bricklet/<UID>/get_state
or register/nfc_rfid_bricklet/<UID>/state_changed
callback)request/nfc_rfid_bricklet/<UID>/request_page
or request/nfc_rfid_bricklet/<UID>/write_page
to read/write pageThe following symbols are available for this function:
For key_number:
request/
nfc_rfid_bricklet/
<UID>/
write_page
¶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:
The general approach for writing to a tag is as follows:
request/nfc_rfid_bricklet/<UID>/request_tag_id
request/nfc_rfid_bricklet/<UID>/get_state
or
register/nfc_rfid_bricklet/<UID>/state_changed
callback)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 1request/nfc_rfid_bricklet/<UID>/write_page
with page number and datarequest/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: |
|
---|---|
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:
The general approach for reading a tag is as follows:
request/nfc_rfid_bricklet/<UID>/request_tag_id
request/nfc_rfid_bricklet/<UID>/get_state
or register/nfc_rfid_bricklet/<UID>/state_changed
callback)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 1request/nfc_rfid_bricklet/<UID>/request_page
with page numberrequest/nfc_rfid_bricklet/<UID>/get_state
or register/nfc_rfid_bricklet/<UID>/state_changed
callback)request/nfc_rfid_bricklet/<UID>/get_page
to retrieve the page from the bufferIf 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: |
|
---|---|
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/
nfc_rfid_bricklet/
<UID>/
get_identity
¶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/
nfc_rfid_bricklet/
<UID>/
state_changed
¶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: