Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • Getting the NFC reader
  • Scan and read tags
  • Accessing the data
  • What to do when a tag was read?
  • Tweak: Wait a second before scanning again

Was this helpful?

  1. Appendix
  2. Other Devices

NFC Reader

PreviousSound Pressure SensorNextMotion Detector

Last updated 4 years ago

Was this helpful?

The can be thought of as a sensor, but it has some particularities that we need to address. In this article, we assume you have successfully initialized the devices with the , and you stored all connected devices on a global variable devices. We also declared a global variable called nfcReader.

Getting the NFC reader

Next, we need to know the , which 286:

// Get the NFC reader via its device identifer
nfcReader = devices.getDeviceByIdentifier(286);

Scan and read tags

Once we have a reference to the NFC reader, we can use the scan() function to tell the reader to start scanning. When an NFC tag is placed on the reader, it will detect it and read the information that is stored on it:

// We want to be informed when a new sensor value arrives
nfcReader.scan(readingDone, readingFailed);

function readingDone(val) {
    // Do something with the data from the NFC tag
}

function readingFailed(error) {
    // Handle reading errors
}

When calling the scan() function, you must provide two callback functions as arguments: The first is called when the NFC reader has found and read a tag successfully, the second if the reader has found a tag, but there was an error reading it.

Accessing the data

In the function readingDone() we have access to the data stored on the NFC tag via the parameter val. In the current version of the Tinkerforge Device Manager, the tag's information must be a simple JSON object with two fields. For example, this could be stored on a NFC sticker:

{
    "id": 1,
    "type": 2
}

We can obtain this JSON object from the val parameter:

function readingDone(val) {
    var id = val.id;
    var type = val.type;
    
    log("Read tag with ID " + id + " and type " + type;
}

Writing data to an NFC tag is currently not supported by the Tinkerforge Device Manager. You can only work with the NFC tags and their current data stored on them.

What to do when a tag was read?

When a tag was read, whether that was successful or not, the NFC reader goes into an idle status. To keep the reader in a state where it can detect and read tags, we can take the following measurements:

// We want to be informed when a new sensor value arrives
nfcReader.scan(readingDone, readingFailed);

function readingDone(val) {
    // Do something with the data...
    
    // Set the reader back to scan mode
    nfcReader.scan(readingDone, readingFailed);
}

function readingFailed(error) {
    // Handle reading errors...
    
    // In case of an error, also go back to scanning mode
    nfcReader.scan(readingDone, readingFailed);
}

In the callback function readingDone(), we simply use nfcReader.scan() directly after we processed the data from the current tag. This puts the NFC reader back to scan mode. We must do the same in the readingFailed() function if we want to make sure the reader will always stay in scan mode.

The readingFailed() callback is called when the NFC reader times out. This happens a few seconds after the scan() function was called and no tag was found. Therefore, in the above code example, it is quite normal that the readingFailed() function is called every so many seconds.

Tweak: Wait a second before scanning again

In the example above, we put the NFC reader back to scan mode directly after we received the data from the current tag. As a consequence, we will see the same tag and its data many times because we can't remove the tag fast enough (and the reader is really fast). To prevent this issue, we can set a timeout of 1 second before we start scanning again:

// We want to be informed when a new sensor value arrives
nfcReader.scan(readingDone, readingFailed);

function readingDone(val) {
    // Do something with the data...
    
    // Set the reader back to scan mode after 1000 ms
    setTimeout(setScanning, 1000);
}

function readingFailed(error) {
    // Handle reading errors...
    
    // In case of an error, also go back to scanning mode
    setTimeout(setScanning, 1000);
}

function setScanning() {
    nfcReader.scan(readingDone, readingFailed);
}

The Tinkerforge Device Manager currently only support NFC Forum Typ 2 tags. These include the NFC stickers and the NFC cards that are available on the .

NFC Reader
Tinkerforge Device Manager
device identifier
Tinkerforge shop