collapse

* Posts Recentes

Amplificador - Rockboard HA 1 In-Ear por almamater
[Ontem às 19:13]


O que é isto ? por KammutierSpule
[26 de Março de 2024, 19:35]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Emulador NES em ESP32 por dropes
[13 de Março de 2024, 21:19]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]


Meu novo robô por josecarlos
[06 de Janeiro de 2024, 16:46]


Laser Engraver - Alguém tem? por almamater
[16 de Dezembro de 2023, 14:23]

Autor Tópico: Encravado com RFID RC522  (Lida 2843 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline helderjsd

  • Mini Robot
  • *
  • Mensagens: 149
Encravado com RFID RC522
« em: 20 de Abril de 2016, 23:02 »
Olá a todos pessoal,
Ando aqui há uma semana as cabeçadas com os modulos RFID RC522 e nao consigo sair da estaca zero.
Ja testei 2 arduinos UNO e um Mega, 2 receptores RC522, cera de 10 cartoes/coins, e o resultado é sempre o mesmo.

Testei N exemplos de codigos tirados da net, e refazendo a sligações (regra geral so muda o pin do reset), nao consigo por esta treta a funcionar.
Com um dos exemplso (e apenas com um) consigo que funcione durante alguns segundos. Mas cerca de 30 segundos depois, deixa de funcionar, e mesmo desligando o arduino e voltando a ligar continua a nao fucionar. Tem de estar desligado cerca de 5 minutos para depois voltar a dar.
O exemplo que funciona é este da Addicore:
Código: [Seleccione]
// Example sketch to read the ID from an Addicore 13.56MHz RFID tag
// as found in the RFID AddiKit found at:
// http://www.addicore.com/RFID-AddiKit-with-RC522-MIFARE-Module-RFID-Cards-p/126.htm

#include <AddicoreRFID.h>
#include <SPI.h>

#define uchar unsigned char
#define uint unsigned int

//4 bytes tag serial number, the first 5 bytes for the checksum byte
uchar serNumA[5];

uchar fifobytes;
uchar fifoValue;

AddicoreRFID myRFID; // create AddicoreRFID object to control the RFID module

/////////////////////////////////////////////////////////////////////
//set the pins
/////////////////////////////////////////////////////////////////////
const int chipSelectPin = 10;
const int NRSTPD = 5;

//Maximum length of the array
#define MAX_LEN 16

void setup() {               
   Serial.begin(9600);                        // RFID reader SOUT pin connected to Serial RX pin at 9600bps
 
  // start the SPI library:
  SPI.begin();
 
  pinMode(chipSelectPin,OUTPUT);              // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
    digitalWrite(chipSelectPin, LOW);         // Activate the RFID reader
  pinMode(NRSTPD,OUTPUT);                     // Set digital pin 10 , Not Reset and Power-down
    digitalWrite(NRSTPD, HIGH);

  myRFID.AddicoreRFID_Init(); 
  Serial.println("RFID tag detected");
}

void loop()
{
 
  uchar i, tmp, checksum1;
uchar status;
        uchar str[MAX_LEN];
        uchar RC_size;
        uchar blockAddr; //Selection operation block address 0 to 63
        String mynum = "";

        str[1] = 0x4400;
//Find tags, return tag type
status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);
if (status == MI_OK)
{
            Serial.println("RFID tag detected");
        Serial.print(str[0],BIN);
            Serial.print(" , ");
        Serial.print(str[1],BIN);
            Serial.println(" ");
}

//Anti-collision, return tag serial number 4 bytes
status = myRFID.AddicoreRFID_Anticoll(str);
if (status == MI_OK)
{
            checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
            Serial.println("The tag's number is  : ");
        //Serial.print(2);
        Serial.print(str[0]);
            Serial.print(" , ");
        Serial.print(str[1],BIN);
            Serial.print(" , ");
        Serial.print(str[2],BIN);
            Serial.print(" , ");
        Serial.print(str[3],BIN);
            Serial.print(" , ");
        Serial.print(str[4],BIN);
            Serial.print(" , ");
            Serial.println(checksum1,BIN);
           
            // Should really check all pairs, but for now we'll just use the first
            if(str[0] == 156)                      //You can change this to the first byte of your tag by finding the card's ID through the Serial Monitor
            {
                Serial.print("Hello Craig!\n");
            } else if(str[0] == 244) {             //You can change this to the first byte of your tag by finding the card's ID through the Serial Monitor
                Serial.print("Hello Erin!\n");
            }
            Serial.println();
            delay(1000);
}

        myRFID.AddicoreRFID_Halt();    //Command tag into hibernation             

}

Com outros exemplos (e testei imensos nada funciona nem le cartao nenuum.
Por exemplo este, nao dá nada de nada:

Código: [Seleccione]
/*
 * MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
 * Signal     Pin              Pin               Pin
 *            Arduino Uno      Arduino Mega      MFRC522 board
 * ------------------------------------------------------------
 * Reset      8                5                 RST
 * SPI SS     10               53                SDA
 * SPI MOSI   11               52                MOSI
 * SPI MISO   12               51                MISO
 * SPI SCK    13               50                SCK
 *
 */

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 8
MFRC522 mfrc522(SS_PIN, RST_PIN);        // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
byte status;
byte valueBlockA    = 4;
byte trailerBlock   = 7;
byte buffer[18];
byte size = sizeof(buffer);
byte escrever[]={ 1,2,3,4,  5,6,8,8, 9,10,255,12,  13,14,15,120};
byte card1[]={0x44,0x51,0x18,0xC9};
void setup() {
        Serial.begin(9600);        // Initialize serial communications with the PC
        SPI.begin();                // Init SPI bus
        mfrc522.PCD_Init();        // Init MFRC522 card
        //Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
        pinMode(14, OUTPUT);
        pinMode(15, OUTPUT);
}

void loop() {
        Serial.println("Card OK");
        // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
        //MFRC522::MIFARE_Key key;
        for (byte i = 0; i < 6; i++) {
                key.keyByte[i] = 0xFF;
        }
        // Look for new cards
        if ( ! mfrc522.PICC_IsNewCardPresent()) {
                return;
        }

        // Select one of the cards
        if ( ! mfrc522.PICC_ReadCardSerial()) {
                return;
        }
        // Now a card is selected. The UID and SAK is in mfrc522.uid.
       
       dumpid();

       if(mfrc522.uid.uidByte[0] == card1[0] && mfrc522.uid.uidByte[1] == card1[1] && mfrc522.uid.uidByte[2] == card1[2] && mfrc522.uid.uidByte[3] == card1[3])
      {
        Serial.println("Card OK");
        digitalWrite(15, HIGH);
        delay(1500);
        digitalWrite(15, LOW);
      }
       else
      {
       Serial.println("Card NOK");
        digitalWrite(14, HIGH);
        delay(1500);
        digitalWrite(14, LOW);
      }
      // dump();
      // autenticar();

     //  write();
               
      // read();
 
        // Halt PICC
        mfrc522.PICC_HaltA();

        // Stop encryption on PCD
        mfrc522.PCD_StopCrypto1();
       
       
}

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

void dumpid()
{
          // Dump UID
        Serial.print("Card UID:");
        for (byte i = 0; i < mfrc522.uid.size; i++) {
                Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
                Serial.print(mfrc522.uid.uidByte[i], HEX);
        }
        Serial.println();
       
}
void dump()
{
                // Dump PICC type
        byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
        Serial.print("PICC type: ");
        Serial.println(mfrc522.PICC_GetTypeName(piccType));
        if (        piccType != MFRC522::PICC_TYPE_MIFARE_MINI
                &&        piccType != MFRC522::PICC_TYPE_MIFARE_1K
                &&        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
                //Serial.println("This sample only works with MIFARE Classic cards.");
                return;
        }
}

void autenticar()
{
         
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
        Serial.print("PCD_Authenticate() failed: ");
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
        }
       
}

void write()
{
          Serial.println("Writing new value block A(4) : the first of the sector TWO ");
                status = mfrc522.MIFARE_Write(valueBlockA, escrever, 16);
                if (status != MFRC522::STATUS_OK) {
                        Serial.print("MIFARE_Write() failed: ");
                        Serial.println(mfrc522.GetStatusCodeName(status));
                }
               
}

void read()
{
 
        Serial.println("Read block A(4) : the first of the sector TWO");
        status = mfrc522.MIFARE_Read(valueBlockA, buffer, &size);
        Serial.print("Settore : 0 Valore :");
        Serial.println(buffer[0]);
        Serial.print("Settore : 1 Valore :");
        Serial.println(buffer[1]);
        Serial.print("Settore : 2 Valore :");
        Serial.println(buffer[2]);
        Serial.print("Settore : 3 Valore :");
        Serial.println(buffer[3]);
        Serial.print("Settore : 4 Valore :");
        Serial.println(buffer[4]);
        Serial.print("Settore : 5 Valore :");
        Serial.println(buffer[5]);
        Serial.print("Settore : 6 Valore :");
        Serial.println(buffer[6]);
        Serial.print("Settore : 7 Valore :");
        Serial.println(buffer[7]);
        Serial.print("Settore : 8 Valore :");
        Serial.println(buffer[8]);
        Serial.print("Settore : 9 Valore :");
        Serial.println(buffer[9]);
        Serial.print("Settore :10 Valore :");
        Serial.println(buffer[10]);
        Serial.print("Settore :11 Valore :");
        Serial.println(buffer[11]);
        Serial.print("Settore :12 Valore :");
        Serial.println(buffer[12]);
        Serial.print("Settore :13 Valore :");
        Serial.println(buffer[13]);
        Serial.print("Settore :14 Valore :");
        Serial.println(buffer[14]);
        Serial.print("Settore :15 Valore :");
        Serial.println(buffer[15]);
       
}

Alguem ja teve situações identicas, que me possa ajudar?

Offline ricard0g0mes

  • Mini Robot
  • *
  • Mensagens: 146
Re: Encravado com RFID RC522
« Responder #1 em: 21 de Abril de 2016, 12:02 »
Quando passas cá?

Tratamos disso. Tenho um meu a funcionar

Offline helderjsd

  • Mini Robot
  • *
  • Mensagens: 149
Re: Encravado com RFID RC522
« Responder #2 em: 21 de Abril de 2016, 14:16 »
Boas camarada,
É quando quiseres/puderes :p