Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of how to write on NTAG213, if possible #5

Closed
txgruppi opened this issue May 9, 2018 · 3 comments
Closed

Example of how to write on NTAG213, if possible #5

txgruppi opened this issue May 9, 2018 · 3 comments

Comments

@txgruppi
Copy link

txgruppi commented May 9, 2018

Hi,

I'm learning about the mfrc522 and I have a project that can use it with Ntag213, but it would work better if I can write on the tags.

I'm not sure mfrc522 can do this, if it don't could you just comment saying it is not possible? But if it can could you please provide a example of how to write to a NTAG213?

I've spent the last few hours trying to make it work but no success, no Google search that helped.

Thanks.

@musdom
Copy link

musdom commented Sep 27, 2018

Hello this is an example that can write to NTAG21x:

"use strict";
const mfrc522 = require("mfrc522-rpi");

mfrc522.initWiringPi(0);
let continueReading = true;

//# This loop keeps checking for chips. If one is near it will get the UID and authenticate
console.log("scanning...");
console.log("Please put chip or keycard in the antenna inductive zone!");
console.log("Press Ctrl-C to stop.");

const defaultUrl = "github.com";
let payload = process.argv[2] ? process.argv[2] : defaultUrl;

while (continueReading) {
  //# reset card
  mfrc522.reset();

  //# Scan for cards
  let response = mfrc522.findCard();
  if (!response.status) {
      continue;
  }

  //# Get the UID of the card
  response = mfrc522.getUid();
  if (!response.status) {
      console.log("UID Scan Error");
      continue;
  }
  //# If we have the UID, continue
  const uid = response.data;

  // add 0xFE to end of message
  payload = payload + "þ";
  // character to be inserted after NDEF header
  let firstChar = payload.slice(0, 1).charCodeAt(0);
  let stringArray = chunkSubstr(payload.slice(1), 4);

  // NDEF message headers
  let data = [0x03, 0x44, 0xD1, 0x01];
  console.log(mfrc522.getDataForBlock(4));
  mfrc522.writeDataToBlock(4, data);
  // 0x04 = https://
  data = [0x40, 0x55, 0x04, firstChar];
  console.log(mfrc522.getDataForBlock(5));
  mfrc522.writeDataToBlock(5, data);

  const startBlock = 6;
  for (var i = startBlock; i < (startBlock + stringArray.length); i++){
    // must read page before writing
    mfrc522.getDataForBlock(i);
    let binPayload = string2Bin(stringArray[i-startBlock]);
    console.log(`Block ${i}`);
    console.log(binPayload);
    mfrc522.writeDataToBlock(i, binPayload);
    console.log(mfrc522.getDataForBlock(i).splice(0, 4));
  }
  continueReading = false;
  console.log("finished successfully!");
}

function string2Bin(str) {
  var result = [];
  for (var i = 0; i < str.length; i++) {
    // result.push(str.charCodeAt(i).toString(10));
    result.push(str.charCodeAt(i));
  }
  return result;
}

function chunkSubstr(str, size) {
  var numChunks = Math.ceil(str.length / size),
      chunks = new Array(numChunks);

  for(var i = 0, o = 0; i < numChunks; ++i, o += size) {
    chunks[i] = str.substr(o, size);
  }

  return chunks;
}

@firsttris
Copy link
Owner

thanks @musdom

@firsttris
Copy link
Owner

closing the issue, and linking this in the readme as example for NTAG213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants