5 Replies Latest reply on Nov 30, 2015 1:45 PM by PabloM_Intel

    Adafruit PN532 RFID/NFC shield and javascript

    CHerbst

      As the title shows I am using an adafruit PN532 shield and I am coding in javascript in the Intel XDK IoT Edition environment and on a Galileo Gen 2.  The sample I code I am using I got from github. I will display it also below.

       

      Now I feel this code should work, but if it did I wouldn't be typing this.  I did have to make adjustments to the github code, like adding a missing semicolon and thinking it would probably be a good idea to tie the IRQ pin to pin 2 as that's what it is in hardware.  I have the latest upm and mraa, and as of a few minutes ago I now have the latest XDK IoT Edition.

       

      My error is thus:

       

      ERROR: sendCommandCheckAck: Not ready, timeout                                                                                                                                 

      Could not identify PN532                                                                                                                                                       

      Exiting

       

      I'm wondering if my issue is a bad shield, bad connection, or I need to somehow update the firmware on the shield?

       

      MODIFIED CODE from Github:

       

      /*jslint node:true, vars:true, bitwise:true, unparam:true */

      /*jshint unused:true */

       

       

      /*

      * Author: Zion Orent <zorent@ics.com>

      * Copyright (c) 2015 Intel Corporation.

      *

      * Permission is hereby granted, free of charge, to any person obtaining

      * a copy of this software and associated documentation files (the

      * "Software"), to deal in the Software without restriction, including

      * without limitation the rights to use, copy, modify, merge, publish,

      * distribute, sublicense, and/or sell copies of the Software, and to

      * permit persons to whom the Software is furnished to do so, subject to

      * the following conditions:

      *

      * The above copyright notice and this permission notice shall be

      * included in all copies or substantial portions of the Software.

      *

      * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

      * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

      * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

      * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE

      * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

      * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION

      * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

      */

       

       

      // Load PN532 module

      var pn532 = require('jsupm_pn532');

       

       

      // Instantiate an PN532 on I2C bus 0 (default) using gpio 2 for the

      // IRQ, and gpio 3 for the reset pin.

      var myNFCObj = new pn532.PN532(2, 3);

       

       

      if (!myNFCObj.init())

        console.log("init() failed");

       

       

      var vers = myNFCObj.getFirmwareVersion();

       

       

      if (vers)

        console.log("Got firmware version: " + toHex(vers, 8));

      else

      {

        console.log("Could not identify PN532");

        exit();

      }

       

       

      // Now scan and identify any cards that come in range (1 for now)

       

       

      // Retry forever

      myNFCObj.setPassiveActivationRetries(0xff);

       

       

      myNFCObj.SAMConfig();

       

       

      var uidSize = new pn532.uint8Array(0);

      var uid = new pn532.uint8Array(7);

       

       

      var myInterval = setInterval(function()

      {

        for (var x = 0; x < 7; x++)

        uid.setitem(x, 0);

        if (myNFCObj.readPassiveTargetID(pn532.PN532.BAUD_MIFARE_ISO14443A,

                                uid, uidSize, 2000))

        {

        // found a card

        console.log("Found a card: UID len " + uidSize.getitem(0));

        process.stdout.write("UID: ");

        for (var i = 0; i < uidSize.getitem(0); i++)

                      {

                              var byteVal = uid.getitem(i);

        process.stdout.write(toHex(byteVal, 2) + " ");

                      }

        process.stdout.write("\n");

        console.log("SAK: " + toHex(myNFCObj.getSAK(), 2));

        console.log("ATQA: " + toHex(myNFCObj.getATQA(), 4));

        console.log(" ");

        }

        else

        console.log("Waiting for a card...");

      }, 1000);

       

       

      function toHex(d, pad)

      {

          // pad should be between 1 and 8

          return  ("00000000"+(Number(d).toString(16))).slice(-pad);

      }

       

       

      function exit()

      {

        clearInterval(myInterval);

        myNFCObj = null;

        pn532.cleanUp();

        pn532 = null;

        console.log("Exiting");

        process.exit(0);

      }

       

       

      // When exiting: clear interval, and print message

      process.on('SIGINT', function()

      {

        exit();

      });

        • 1. Re: Adafruit PN532 RFID/NFC shield and javascript
          PabloM_Intel

          Hi CHerbst,

           

          Can you tell me which upm and mraa version you are using? Also, which image are you using? Enter uname –a into your board’s terminal to get this information.

          Do you have the jsupm_pn532 module as one of the node modules in your board?

           

          Regards,

          PabloM_Intel

          • 2. Re: Adafruit PN532 RFID/NFC shield and javascript
            CMata_Intel

            Hi CHerbst,

             

            Have you been able to use upm example in your Galileo board?

             

            Regards,

            Charlie

            • 3. Re: Adafruit PN532 RFID/NFC shield and javascript
              JadeDragon

              I have the exact same problem running Intels UPM example program for PN532, just the C++ version of the code from Intel IoT Eclipse IDE on Galileo Gen2.

               

              “sendCommandCheckAck: Not ready, timeout

              Could not identify RFid PN532

              logout”


              I then tried the Arduino 1.6.4 IDE and run read/write/format example programs for the PN532 for this environment(rebooting with and without a SDcard inserted on the Galileo board, but it would not upload the code to the board in either case, it never finish uploading ,went into a livelock state. I then just tried the basic on-board LED blink sketch but that is also producing the


              C:\Users\pihlungpang\AppData\Roaming\Arduino15\packages\Intel\tools\sketchUploader\1.6.2+1.0/clupload/cluploadGalileo_win.sh: line 56: /dev/ttyS2: Permission denied


              So I have a few questions:
              1.Is it possible to easily switch between programming the Galileo board with all three types of IDEs? Arduino, XDK and Eclipse?

              or should I choose between Eclipse/XDK or Arduino?


              2. I can only run Arduino sketches if a run a special linux image from on-board flash and have removed the SDcard?

              3. What do I need to do to come back to the Eclipse IDE(XDK), and what is likely the reason for the "Could not identify RFid PN532" message.


              Thanks


              • 4. Re: Adafruit PN532 RFID/NFC shield and javascript
                JadeDragon

                After long time in PC device manager an extra “Galileo(com4)” appeared, beside USB serial port(com3) that I used earlier and got the “permission denied” reply. Using Com4 in Arduino make it possible to run the on board blink sketch, and download the RFid example sketch. When open the Monitor it said “Didn’t find PN53x board”

                Do I have to change any of the i2c/SPI defines, so it fit the Galileo board? Or is Galileo full compatible with regular Arduino pin outs?

                Thanks.


                #include <Wire.h>

                #include <SPI.h>

                #include <Adafruit_PN532.h>

                 

                 

                // If using the breakout with SPI, define the pins for SPI communication.

                #define PN532_SCK  (2)

                #define PN532_MOSI (3)

                #define PN532_SS   (4)

                #define PN532_MISO (5)

                 

                 

                // If using the breakout or shield with I2C, define just the pins connected

                // to the IRQ and reset lines.  Use the values below (2, 3) for the shield!

                #define PN532_IRQ   (2)

                #define PN532_RESET (3)  // Not connected by default on the NFC Shield

                 

                 

                // Uncomment just _one_ line below depending on how your breakout or shield

                // is connected to the Arduino:

                 

                 

                // Use this line for a breakout with a SPI connection:

                //Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

                 

                 

                // Use this line for a breakout with a hardware SPI connection.  Note that

                // the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's

                // hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are

                // SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.

                //Adafruit_PN532 nfc(PN532_SS);

                 

                 

                // Or use this line for a breakout or shield with an I2C connection:

                Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

                 

                 

                /*

                    We can encode many different kinds of pointers to the card,

                    from a URL, to an Email address, to a phone number, and many more

                    check the library header .h file to see the large # of supported

                    prefixes!

                */

                // For a http://www. url:

                const char * url = "adafruit.com";

                uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;

                 

                 

                // for an email address

                //const char * url = "mail@example.com";

                //uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;

                 

                 

                // for a phone number

                //const char * url = "+1 212 555 1212";

                //uint8_t ndefprefix = NDEF_URIPREFIX_TEL;

                 

                 

                 

                 

                void setup(void) {

                  Serial.begin(115200);

                  Serial.println("Looking for PN532...");

                 

                 

                  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);

                 

                 

                  // configure board to read RFID tags

                  nfc.SAMConfig();

                }

                 

                 

                void loop(void) {

                  uint8_t success;                          // Flag to check if there was an error with the PN532

                  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)

                  bool authenticated = false;               // Flag to indicate if the sector is authenticated

                 

                 

                  // Use the default key

                  uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

                 

                 

                  Serial.println("");

                  Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the");

                  Serial.println("authentication keys.  To reformat your NDEF tag as a clean Mifare");

                  Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!");

                  Serial.println("");

                  Serial.println("Place your Mifare Classic card on the reader to format with NDEF");

                  Serial.println("and press any key to continue ...");

                  // Wait for user input before proceeding

                  while (!Serial.available());

                  // a key was pressed1

                  while (Serial.available()) Serial.read();

                 

                 

                  // Wait for an ISO14443A type card (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, &uidLength);

                 

                 

                  if (success)

                  {

                    // Display some basic information about the card

                    Serial.println("Found an ISO14443A card");

                    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");

                    Serial.print("  UID Value: ");

                    nfc.PrintHex(uid, uidLength);

                    Serial.println("");

                 

                 

                    // Make sure this is a Mifare Classic card

                    if (uidLength != 4)

                    {

                      Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");

                      return;

                    }

                 

                 

                    // We probably have a Mifare Classic card ...

                    Serial.println("Seems to be a Mifare Classic card (4 byte UID)");

                 

                 

                    // Try to format the card for NDEF data

                    success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya);

                    if (!success)

                    {

                      Serial.println("Unable to authenticate block 0 to enable card formatting!");

                      return;

                    }

                    success = nfc.mifareclassic_FormatNDEF();

                    if (!success)

                    {

                      Serial.println("Unable to format the card for NDEF");

                      return;

                    }

                 

                 

                    Serial.println("Card has been formatted for NDEF data using MAD1");

                 

                 

                    // Try to authenticate block 4 (first block of sector 1) using our key

                    success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya);

                 

                 

                    // Make sure the authentification process didn't fail

                    if (!success)

                    {

                      Serial.println("Authentication failed.");

                      return;

                    }

                 

                 

                    // Try to write a URL

                    Serial.println("Writing URI to sector 1 as an NDEF Message");

                 

                 

                    // Authenticated seems to have worked

                    // Try to write an NDEF record to sector 1

                    // Use 0x01 for the URI Identifier Code to prepend "http://www."

                    // to the url (and save some space).  For information on URI ID Codes

                    // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef

                    if (strlen(url) > 38)

                    {

                      // The length is also checked in the WriteNDEFURI function, but lets

                      // warn users here just in case they change the value and it's bigger

                      // than it should be

                      Serial.println("URI is too long ... must be less than 38 characters long");

                      return;

                    }

                 

                 

                    // URI is within size limits ... write it to the card and report success/failure

                    success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);

                    if (success)

                    {

                      Serial.println("NDEF URI Record written to sector 1");

                    }

                    else

                    {

                      Serial.println("NDEF Record creation failed! :(");

                    }

                  }

                 

                 

                  // Wait a bit before trying again

                  Serial.println("\n\nDone!");

                  delay(1000);

                  Serial.flush();

                  while(Serial.available()) Serial.read();

                }



                • 5. Re: Adafruit PN532 RFID/NFC shield and javascript
                  PabloM_Intel

                  Hi JadeDragon,

                   

                  Have you checked the Shield Testing Report for Intel Galileo? I would suggest you to take a look at that document so you can check if the device that you’re using has been tested with the Galileo board (which I believe is the case for the PN352). You’ll also find pin usage for the board. In this case A4 and A5 is for I2C’s SDA and SCL, respectively. Have you tested it using these pins?

                   

                  Regards,

                  PabloM_Intel