NFCrx in puckjs

Posted on
  • Hi,

    I'm trying to write the NFC tag in puck. so i m using NFCrx method.
    i want to receive data and print to console?
    also how can i erase this content on puck side, any api is there ?
    please help me fixing this.

  • There's a library to handle it here: https://www.espruino.com/NFCTag

    Will that work for you?

  • can you please help me how to use this api's.
    NFCTag.prototype._write = function (rx) { ... }

    i want to receive the bytes and store in array, print to console or modify those as next step?

  • i m trying very simple case. i want to make the puck as tag and receive contents from reader and print to the log ?

  • Can you please provide me the sample code snippet or guide to work with NFC usecase. I mean to read and write operation of this puckjs as tag?

  • Looks like you have to modify the module. Copy the uncompressed to your sandbox modules folder and modify the ._write() method (NFCTag.prototype._write(...){...). You can then do analog to the ._write() method. I assume you want to debug / monitor what 'your' tag reader/writer does...

  • Personally I would just write a handler for the NRF.on("NFCoff", event that checks the contents of the card each time the NFC reader turns off the field - after some playing that seems to be the most sensible.

    Here's some code that works without using a module:

    var data = new Uint8Array(10+64);
    var header = NRF.nfcStart();
    var written = false;
    data.set(header,0); // NFC device header
    data.set([0,0,0xE1,0x10,(data.length-10)­/8,0,0,3,0,0xFe], 0x0A); // NDEF tag header
    // 0,0,e1
    NRF.on('NFCrx', function(rx) {
      var idx = rx[1]*4;
      switch(rx[0]) {
        case 0x30: //command: READ
          NRF.nfcSend(new Uint8Array(data.buffer, idx, 16));
          break;
        case 0xa2: //command: WRITE
          written = true;
          if(idx > data.length) {
            NRF.nfcSend(0x0);
          } else {
            data.set(new Uint8Array(rx, 2, 4), idx);
            NRF.nfcSend(0xA);
          }
          break;
        default:   //just, re-enable rx
          NRF.nfcSend();
          break;
        }
    });
    NRF.on("NFCoff",function() {
      if (written) 
        onWritten(E.toString(new Uint8Array(data.buffer,26,data[21]-3)));­
      written = false;
    });
    
    function onWritten(data) {
      console.log("NFC written", data);
      var colors = {
        red : 1,
        green : 2,
        blue : 4,
      };
      if (colors[data]) {
        digitalWrite([LED3,LED2,LED1], colors[data]); //  onwards
        setTimeout(function() {
          digitalWrite([LED3,LED2,LED1], 0);
        },1000);
      }
    }
    

    You can even control it using a webpage!

    <html>
    <body>
    <script>
    /* Starting a scan at startup stops Android from
    moving away from the Chrome window when a tag is found*/
    const reader = new NDEFReader();
    const writer = new NDEFWriter();
    reader.scan();
    
    function send(msg) {
      writer.write(msg).then(_=>console.log("W­ritten ",msg));
    }
    </script>
    <button onclick="send('red')">Red</button>
    <button onclick="send('green')">Green</button>
    <button onclick="send('blue')">Blue</button>
    </body>
    </html>
    
  • Just to add, you can see that the NFCrx event handles the write, but NFC has a really odd way of showing the end of the write (it writes something at the beginning and then re-writes something at the end) - it's much easier to just look at the full contents of the data when you know the writing has finished, rather than trying to handle every single 4 byte write that occurs.

  • i have burned the above code in puck,t i m not able to read /write(getting nfc error) the tag using Iphone ( NFC TagWriter by NXP app).

    i m trying very simple thing, make this puck as tag, receive the contents from the app and print the data to log. can you please help me in achieving that?

    can you little bit explain about the "NFCrx". what exactly ?

  • @Gordon, great, simple AND complete example - comparable to a (http) request/response filter - and with no dependency what so ever - on the NFC bare bones.

  • Thanks @allObjects!

    @afrid are you running with up to date Puck.js firmware? Could you read the Puck using 'NFC TagWriter' using the previous code I posted up or even when using NRF.nfcURL()?

    I'm afraid I can't help you out more though - I've given you tested code that exactly what you want and works great on Android, but I don't have an NFC-enabled iPhone to try

    can you little bit explain about the "NFCrx". what exactly ?

    It's called when a packet of NFC data is received from the NFC device, and then you have to respond to it. If you need more info about the protocol you need to search for the NFC Forum Type 2 spec: http://apps4android.org/nfc-specificatio­ns/NFCForum-TS-Type-2-Tag_1.1.pdf

    Honestly though it sounds like you might be better off using NFC as a simple readable tag and then using Bluetooth to set any data you want.

  • Thanks @Gordon for reply.

    Yes i have latest firmware
    I m actually loading the code into puck. I have NFC enabled iphone using tag writer by NXP and also NFC tools by wakdev.

    What actually i need to write to get that LED blink on puck.?
    Can you please explain the code what actually trying for?

    What is the use of case 0xa2 in NFCrx event.?

    From html i understood that,need to send 'red' or equivalent button that turn on LED on puck,is my understanding is correct?

  • Yes, you just need to send the text red - but probably best to try writing something and see what response you get on the console.

    What is the use of case 0xa2 in NFCrx event.?

    Read the PDF I posted above.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

NFCrx in puckjs

Posted by Avatar for afrid @afrid

Actions