yue caoRAKwirelessHarold Duarte Rojas
Published © GPL3+

How to build an NFC reader with WisBlock and RUI3

Make a NFC reader and send tag data through LoRaWAN

IntermediateFull instructions provided2 hours1,357
How to build an NFC reader with WisBlock and RUI3

Things used in this project

Hardware components

WisBlock Base Board RAK5005-O
RAKwireless WisBlock Base Board RAK5005-O
×1
WisBlock LPWAN Module RAK4631
RAKwireless WisBlock LPWAN Module RAK4631
×1
WisBlock Buzzer Module RAK18001
RAKwireless WisBlock Buzzer Module RAK18001
×1
NFC RFID NXP PN532 RAK13600
RAKwireless NFC RFID NXP PN532 RAK13600
×1
WisGate Edge Lite 2 RAK7268\RAK7268C  Gateway for LoRaWAN
RAKwireless WisGate Edge Lite 2 RAK7268\RAK7268C Gateway for LoRaWAN
×1

Software apps and online services

Arduino IDE
Arduino IDE
RAKwireless Unified Interface V3 (RUI3)
RAKwireless Unified Interface V3 (RUI3)

Story

Read more

Schematics

Schematic

Code

NFC reader with RUI3

Arduino
/**
   @file iso14443a_uid.ino
   @author rakwireless.com
   @brief This example will attempt to connect to an ISO14443A card and read card UID
   @version 0.1
   @date 2021-10-14
   @copyright Copyright (c) 2021
**/
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <RAK13600-PN532.h> // Click here to get the library: http://librarymanager/All#RAK13600-PN532

/*************************************

   LoRaWAN band setting:
     RAK_REGION_EU433
     RAK_REGION_CN470
     RAK_REGION_RU864
     RAK_REGION_IN865
     RAK_REGION_EU868
     RAK_REGION_US915
     RAK_REGION_AU915
     RAK_REGION_KR920
     RAK_REGION_AS923

 *************************************/

#define OTAA_BAND     (RAK_REGION_AS923) 
#define OTAA_DEVEUI   {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
#define OTAA_APPEUI   {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
#define OTAA_APPKEY   {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88} 


// If using the breakout or shield with I2C, define just the pins connected
#define PN532_IRQ   (WB_IO6)
#define PN532_RESET (WB_IO5)  // Not connected by default on the NFC Shield
#define BUZZER_CONTROL  WB_IO1

uint8_t ledPin1 = LED_GREEN;
uint8_t ledPin2 = LED_BLUE;

// Or use this line for a breakout or shield with an I2C connection:
NFC_PN532 nfc(PN532_IRQ, PN532_RESET);

void lora_init();

void setup(void) {
  Serial.begin(115200);
  pinMode(WB_IO2, OUTPUT);
  digitalWrite(WB_IO2, HIGH);

  pinMode(BUZZER_CONTROL,OUTPUT);
  noTone(BUZZER_CONTROL);

  // initialize the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  lora_init(); 
  
  delay(300);
  while (!Serial) delay(10);
  Serial.println("Hello!");
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0xFF);

  //configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A card");
}

void loop(void) {
  boolean success;
  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)

  // Wait for an ISO14443B 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 = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

  if (success) {
    tone(BUZZER_CONTROL,4000);
    delay(150);
    noTone(BUZZER_CONTROL);
    Serial.println("Found a card!");
    Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i = 0; i < uidLength; i++)
    {
      Serial.print(" 0x"); Serial.print(uid[i], HEX);
    }
    Serial.println("");

   digitalWrite(ledPin1, HIGH); // LED turn on when input pin value is HIGH
   delay(150);
   digitalWrite(ledPin1, LOW); // 

  /** Send the data package */
  if (api.lorawan.send(uidLength, (uint8_t *) & uid, 2, true, 1)) 
  {
    Serial.println("Sending is requested");
  } 
  else 
  {
    Serial.println("Sending failed");
  }
    
    // Wait 1 second before continuing
    delay(1000);
  }
  else
  {
    // PN532 probably timed out waiting for a card
    Serial.println("Timed out waiting for a card");
  }
}


void lora_init()
{
   // OTAA Device EUI MSB first
  uint8_t node_device_eui[8] = OTAA_DEVEUI;
  // OTAA Application EUI MSB first
  uint8_t node_app_eui[8] = OTAA_APPEUI;
  // OTAA Application Key MSB first
  uint8_t node_app_key[16] = OTAA_APPKEY;

  if (!api.lorawan.appeui.set(node_app_eui, 8)) {
    Serial.printf("LoRaWan OTAA - set application EUI is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.appkey.set(node_app_key, 16)) {
    Serial.printf("LoRaWan OTAA - set application key is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.deui.set(node_device_eui, 8)) {
    Serial.printf("LoRaWan OTAA - set device EUI is incorrect! \r\n");
    return;
  }

  if (!api.lorawan.band.set(OTAA_BAND)) {
    Serial.printf("LoRaWan OTAA - set band is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)) {
    Serial.printf("LoRaWan OTAA - set device class is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.njm.set(RAK_LORA_OTAA))  // Set the network join mode to OTAA
  {
    Serial.
  printf("LoRaWan OTAA - set network join mode is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.join())  // Join to Gateway
  {
    Serial.printf("LoRaWan OTAA - join fail! \r\n");
    return;
  }

  /** Wait for Join success */
  while (api.lorawan.njs.get() == 0) {
    Serial.print("Wait for LoRaWAN join...");
    api.lorawan.join();
    delay(10000);
  }

  if (!api.lorawan.adr.set(true)) {
    Serial.printf
  ("LoRaWan OTAA - set adaptive data rate is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.rety.set(1)) {
    Serial.printf("LoRaWan OTAA - set retry times is incorrect! \r\n");
    return;
  }
  if (!api.lorawan.cfm.set(1)) {
    Serial.printf("LoRaWan OTAA - set confirm mode is incorrect! \r\n");
    return;
  }

  /** Check LoRaWan Status*/
  Serial.printf("Duty cycle is %s\r\n", api.lorawan.dcs.get()? "ON" : "OFF"); // Check Duty Cycle status
  Serial.printf("Packet is %s\r\n", api.lorawan.cfm.get()? "CONFIRMED" : "UNCONFIRMED");  // Check Confirm status
  uint8_t assigned_dev_addr[4] = { 0 };
  api.lorawan.daddr.get(assigned_dev_addr, 4);
  Serial.printf("Device Address is %02X%02X%02X%02X\r\n", assigned_dev_addr[0], assigned_dev_addr[1], assigned_dev_addr[2], assigned_dev_addr[3]);  // Check Device Address
  Serial.println("");
}

Credits

yue cao

yue cao

1 project • 0 followers
RAKwireless

RAKwireless

27 projects • 53 followers
A pioneer in providing innovated and diverse LPWA connectivity solutions for IoT edge devices.
Harold Duarte Rojas

Harold Duarte Rojas

9 projects • 10 followers
Electronics engineer, Content creator at RAKwireless.

Comments