Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Arduino] [NFC] [PN532] [Erreur]

Je n'arrive pas à compiler le code d'example de la librairie PN532

    21 juillet 2018 à 8:59:42

    Bonjour,

    Je n'arrive pas à compiler le code d'example de la librairie PN532 :

    Voicie le code :

    /**************************************************************************/
    /*! 
        This example will wait for any ISO14443A card or tag, and
        depending on the size of the UID will attempt to read from it.
       
        If the card has a 4-byte UID it is probably a Mifare
        Classic card, and the following steps are taken:
       
        - Authenticate block 4 (the first block of Sector 1) using
          the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
        - If authentication succeeds, we can then read any of the
          4 blocks in that sector (though only block 4 is read here)
    	 
        If the card has a 7-byte UID it is probably a Mifare
        Ultralight card, and the 4 byte pages can be read directly.
        Page 4 is read by default since this is the first 'general-
        purpose' page on the tags.
    
        To enable debug message, define DEBUG in PN532/PN532_debug.h
    */
    /**************************************************************************/
    
    #if 0
      #include <SPI.h>
      #include <PN532_SPI.h>
      #include "PN532.h"
    
      PN532_SPI pn532spi(SPI, 10);
      PN532 nfc(pn532spi);
    #elif 1
      #include <PN532_HSU.h>
      #include <PN532.h>
          
      PN532_HSU pn532hsu(Serial1);
      PN532 nfc(pn532hsu);
    #else 
      #include <Wire.h>
      #include <PN532_I2C.h>
      #include <PN532.h>
    #endif
    void setup(void) {
      Serial.begin(115200);
      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);
      
      // configure board to read RFID tags
      nfc.SAMConfig();
      
      Serial.println("Waiting for an ISO14443A Card ...");
    }
    
    
    void loop(void) {
      uint8_t 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 ISO14443A 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, &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("");
        
        if (uidLength == 4)
        {
          // We probably have a Mifare Classic card ... 
          Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
    	  
          // Now we need to try to authenticate it for read/write access
          // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
          Serial.println("Trying to authenticate block 4 with default KEYA value");
          uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    	  
    	  // Start with block 4 (the first block of sector 1) since sector 0
    	  // contains the manufacturer data and it's probably better just
    	  // to leave it alone unless you know what you're doing
          success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    	  
          if (success)
          {
            Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
            uint8_t data[16];
    		
            // If you want to write something to block 4 to test with, uncomment
    		// the following line and this text should be read back in a minute
            // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
            // success = nfc.mifareclassic_WriteDataBlock (4, data);
    
            // Try to read the contents of block 4
            success = nfc.mifareclassic_ReadDataBlock(4, data);
    		
            if (success)
            {
              // Data seems to have been read ... spit it out
              Serial.println("Reading Block 4:");
              nfc.PrintHexChar(data, 16);
              Serial.println("");
    		  
              // Wait a bit before reading the card again
              delay(1000);
            }
            else
            {
              Serial.println("Ooops ... unable to read the requested block.  Try another key?");
            }
          }
          else
          {
            Serial.println("Ooops ... authentication failed: Try another key?");
          }
        }
        
        if (uidLength == 7)
        {
          // We probably have a Mifare Ultralight card ...
          Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
    	  
          // Try to read the first general-purpose user page (#4)
          Serial.println("Reading page 4");
          uint8_t data[32];
          success = nfc.mifareultralight_ReadPage (4, data);
          if (success)
          {
            // Data seems to have been read ... spit it out
            nfc.PrintHexChar(data, 4);
            Serial.println("");
    		
            // Wait a bit before reading the card again
            delay(1000);
          }
          else
          {
            Serial.println("Ooops ... unable to read the requested page!?");
          }
        }
      }
    }
    
    

    Voicie mon montage (même si je pense qu'il n'y est pour rien étant donné que le code ne compile pas :

    (J'utilise une arduino nano)

    Voyez-vous l'erreur ?

    Merci d'avance

    Jérémy

    • Partager sur Facebook
    • Partager sur Twitter
      21 juillet 2018 à 16:42:21

      Bonjour,

      Comme tu l'as connecté et configuré en i2c, tu peux effacer toutes les lignes entre #if 0 et #endif pour ne garder que:

      #include <Wire.h>
      #include <PN532_I2C.h>
      #include <PN532.h>

      Et apparemment, il manque juste après ces lignes l'initialisation de la variable nfc:

      PN532_I2C pn532i2c;
      PN532 nfc(pn532i2c);



      • Partager sur Facebook
      • Partager sur Twitter
        21 juillet 2018 à 18:08:32

        Merci beaucoup. Par contre, ça ne compile toujours pas :

        Voicie le code :

        #include <Wire.h>
        #include <PN532_I2C.h>
        #include <PN532.h>
        
        PN532_I2C pn532i2c;
        PN532 nfc(pn532i2c);
        
        
        void setup(void) {
          Serial.begin(115200);
          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);
          
          // configure board to read RFID tags
          nfc.SAMConfig();
          
          Serial.println("Waiting for an ISO14443A Card ...");
        }
        
        
        void loop(void) {
          uint8_t 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 ISO14443A 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, &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("");
            
            if (uidLength == 4)
            {
              // We probably have a Mifare Classic card ... 
              Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
        	  
              // Now we need to try to authenticate it for read/write access
              // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
              Serial.println("Trying to authenticate block 4 with default KEYA value");
              uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
        	  
        	  // Start with block 4 (the first block of sector 1) since sector 0
        	  // contains the manufacturer data and it's probably better just
        	  // to leave it alone unless you know what you're doing
              success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
        	  
              if (success)
              {
                Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
                uint8_t data[16];
        		
                // If you want to write something to block 4 to test with, uncomment
        		// the following line and this text should be read back in a minute
                // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
                // success = nfc.mifareclassic_WriteDataBlock (4, data);
        
                // Try to read the contents of block 4
                success = nfc.mifareclassic_ReadDataBlock(4, data);
        		
                if (success)
                {
                  // Data seems to have been read ... spit it out
                  Serial.println("Reading Block 4:");
                  nfc.PrintHexChar(data, 16);
                  Serial.println("");
        		  
                  // Wait a bit before reading the card again
                  delay(1000);
                }
                else
                {
                  Serial.println("Ooops ... unable to read the requested block.  Try another key?");
                }
              }
              else
              {
                Serial.println("Ooops ... authentication failed: Try another key?");
              }
            }
            
            if (uidLength == 7)
            {
              // We probably have a Mifare Ultralight card ...
              Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
        	  
              // Try to read the first general-purpose user page (#4)
              Serial.println("Reading page 4");
              uint8_t data[32];
              success = nfc.mifareultralight_ReadPage (4, data);
              if (success)
              {
                // Data seems to have been read ... spit it out
                nfc.PrintHexChar(data, 4);
                Serial.println("");
        		
                // Wait a bit before reading the card again
                delay(1000);
              }
              else
              {
                Serial.println("Ooops ... unable to read the requested page!?");
              }
            }
          }
        }
        
        

        Merci beaucoup et merci d'avance.

        Jérémy

        • Partager sur Facebook
        • Partager sur Twitter
          21 juillet 2018 à 19:07:34

          Effectivement, je n'avais pas vu qu'on devait spécifier le port i2c utilisé. Remplace la ligne 5 par:

          PN532_I2C pn532i2c(Wire);



          • Partager sur Facebook
          • Partager sur Twitter
            21 juillet 2018 à 19:18:47

            Merci beaucoup. Ca marche, mais par contre, savez-vous "décoder" un tag nfc, Voici ces specs : (je les utilise pour ouvrir une page web sur mobile):

            Merci et merci d'avance

            Jérémy

            -
            Edité par JérémyChamboultou 21 juillet 2018 à 21:54:46

            • Partager sur Facebook
            • Partager sur Twitter

            [Arduino] [NFC] [PN532] [Erreur]

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown