I can not pass the same card more than once using the RDM6300

Hello.

I am doing an RFID project with the RDM6300 and the Ethernet Shield W5100 to apply an electronic lock with authorization from a MySql database.

The project is going well, but I encountered a small problem and, try as I might, I could not solve it.

The problem I found is that when I try to read the same card a second time then the code does not work. If I pass the cards alternately, everything happens OK, but if I pass a card and then pass it back, I get no response from my code.

Could someone explain to me why this is happening? I suspect I should do some sort of cleanup in the buffer, but I do not know exactly how to do this.

#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include <RDM6300.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress server(192,168,0,10);
 
EthernetClient client;

//Inicializa a serial nos pinos 2 (RX) e 3 (TX)
SoftwareSerial RFID(2, 3);

//Caracteres para leitura no RDM6300
int Led = 4;
uint8_t Payload[6]; // used for read comparisons
String rfid_in = "";

RDM6300 RDM6300(Payload);
  
String codigo;        
String rfid_servidor;        
boolean fim = false;  
boolean pregunta = true;

String readString;
String identificador;  
 
 
void setup() {

  pinMode(Led, OUTPUT);
  //Inicializa a serial para o leitor RDM6300
  RFID.begin(9600);
  
  //Inicializa a serial para comunicacao com o PC
  Serial.begin(9600);
  delay(1000);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac);
  }

  delay(1000);
   
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() {
    inicial();
}

void inicial (){
      if (pregunta == true) {
      Serial.print("Encoste o cartão RFID... ");
      RFID.begin(9600);
      }
      
    pregunta = false;

    while (RFID.available() > 0){

        digitalWrite(Led, HIGH);
        uint8_t c = RFID.read();
        if (RDM6300.decode(c)){chamada();}
        
    }
     delay(1000);
     digitalWrite(Led, LOW);
          
}

void chamada(){
  //MOSTRA A TAG CONVERTIDA PARA DECIMAL NA PORTA SERIAL
          unsigned long result = 
          ((unsigned long int)Payload[1]<<24) + 
          ((unsigned long int)Payload[2]<<16) + 
          ((unsigned long int)Payload[3]<<8) + 
          Payload[4];              
        Serial.print("ID: ");
        Serial.println(result);
        identificador = result;
        requisicao();
        pregunta = true;
    
}
 
int requisicao() {
 
  if (client.connect(server, 8090)) {
    Serial.println("Conectado");
    client.print("GET /arduinodb/rfid.php?rfid=");
    client.print(identificador);
    client.println(" HTTP/1.0");
    client.println("Host: 192.168.0.10");
    client.println("Connection: close");
    client.println();

  }
  else {

    Serial.println("Conexão falhou");
    Serial.println("Desconectando");
   
  }
 
  delay(1000);
  
  while (client.available()) {
    char c = client.read();
    codigo += c;

    fim = true;
   
 
  }
  
  if (fim)  {
   //Serial.println(codigo);
    int comprimento = codigo.length();
    int posicion = codigo.indexOf("*");
    rfid_servidor = "";
 
    for (int i = posicion + 1; i < comprimento; i ++){
      if (codigo[i] == '^') i = comprimento;
      else rfid_servidor += codigo[i];
    }

    fim = false;
    Serial.println("O dono desse RFID é: " + rfid_servidor);
    Serial.println("Desconectando");
    client.stop();
  }
  codigo="";
  return 1;
}

Not sure…

But I would start by getting rid of

      if (pregunta == true) {
      Serial.print("Encoste o cartão RFID... ");
      RFID.begin(9600);
      }

since you have already done it in setup()

  //Inicializa a serial para o leitor RDM6300
  RFID.begin(9600);

and at the end of chamada() - just in case - reset your payload buffer to empty

memset(Payload, 0, sizeof(Payload)); // http://www.cplusplus.com/reference/cstring/memset/

Also - can you try to see if this works (set your serial console at 115200 bauds on your PC/Mac)

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); //Inicializa a serial nos pinos 2 (RX) e 3 (TX)

#include <RDM6300.h>
uint8_t Payload[6];
RDM6300 RDM6300(Payload);

void setup()
{
  Serial.begin(115200);  // USE SERIAL CONSOLE AT 115200 BAUDS
  RFID.begin(9600);    // start serial to RFID reader
}

void loop()
{
  while (RFID.available() > 0) 
  {
    uint8_t c = RFID.read();
    if (RDM6300.decode(c)) {
      for (int i=0; i < 4; i++){
        Serial.print(Payload[i], HEX);
        Serial.print('.');
      } 
      Serial.println(Payload[5], HEX);
    }  
  }
  delay(100);
}

Thanks for the answer,

As for RFID.begin (), you're right, it was duplicate and I deleted the inside of the loop function.

about the command
memset (Payload, 0, sizeof (Payload));
did not work perfectly. This allowed me to be able to read the same card again, but repeated the RFID reading myself, as you can see below on Serial.

Let's just say that was almost the solution.

IP: 192.168.0.11
Encoste o cartão RFID... ID: 5815646
Conectado
O dono desse RFID é: Neto 

Desconectando
ID: 5815646
Conectado
O dono desse RFID é: Neto 

Desconectando
ID: 5815646
Conectado
O dono desse RFID é: Neto 

Desconectando
Encoste o cartão RFID...

“Also - can you try to see if this works (set your serial console at 115200 bauds on your PC/Mac)”

it’s definitly not work.

still need help. because although it allows me to read again with the same card, there are several automatic re-readings of the ID. After these automatic readings, there seems to be no bytes stored and only then can I do a different reading on the RFID antenna.

here is how I put the “memset”

void chamada(){
  //MOSTRA A TAG CONVERTIDA PARA DECIMAL NA PORTA SERIAL
          unsigned long result = 
          ((unsigned long int)Payload[1]<<24) + 
          ((unsigned long int)Payload[2]<<16) + 
          ((unsigned long int)Payload[3]<<8) + 
          Payload[4];              
        Serial.print("ID: ");
        Serial.println(result);
        identificador = result;
        requisicao();
        pregunta = true;

        memset(Payload, 0, sizeof(Payload));
        
}

Well maybe the library you are using is crap...

because although it allows me to read again with the same card, there are several automatic re-readings of the ID. After these automatic readings, there seems to be no bytes stored and only then can I do a different reading on the RFID antenna.

i've not checked your code in details, but you need to save whatever was read before the next call to RDM6300.decode() because it might change the content of the payload. My memset() is definitely emptying it... so if you need the data after exiting chamada() then don't do the memset()

try replacing the memset() command by RDM6300.decode(2); this might reset the reading of a new card without emptying the payload buffer

J-M-L:
try replacing the memset() command by RDM6300.decode(2); this might reset the reading of a new card without emptying the payload buffer

this not work. i think the memset is a good way to go, but the only problem is the repeating actions of reading the ID number. there is any way to stop this?

#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include <RDM6300.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress server(192,168,0,10);
 
EthernetClient client;

//Inicializa a serial nos pinos 2 (RX) e 3 (TX)
SoftwareSerial RFID(2, 3);

//Caracteres para leitura no RDM6300
int Led_amarelo = 4;
int Led_verde = 5;
int Led_vermelho = 6;
int Rele_porta = 7;
uint8_t Payload[6]; // used for read comparisons
String rfid_in = "";


RDM6300 RDM6300(Payload);
  
String codigo;        
String rfid_servidor;        
boolean fim = false;  
boolean pregunta = true;

String readString;
String identificador;  
 
 
void setup() {

  pinMode(Led_amarelo, OUTPUT);
  pinMode(Led_verde, OUTPUT);
  pinMode(Led_vermelho, OUTPUT);
  pinMode(Rele_porta, OUTPUT);
  //Inicializa a serial para o leitor RDM6300
  RFID.begin(9600);
  
  //Inicializa a serial para comunicacao com o PC
  Serial.begin(9600);
  delay(1000);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac);
  }

  delay(1000);
   
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() {
    inicial();
}

void inicial (){
      if (pregunta == true) {
      Serial.print("Encoste o cartão RFID... ");
      }
      
    pregunta = false;

    while (RFID.available() > 0){

        digitalWrite(Led_amarelo, HIGH);
        uint8_t c = RFID.read();
        if (RDM6300.decode(c)){chamada();}
                       
        }
        if (RFID.available() == 0){
          digitalWrite(Led_amarelo, LOW);  
        }
          
}

void chamada(){
  //MOSTRA A TAG CONVERTIDA PARA DECIMAL NA PORTA SERIAL
          unsigned long result = 
          ((unsigned long int)Payload[1]<<24) + 
          ((unsigned long int)Payload[2]<<16) + 
          ((unsigned long int)Payload[3]<<8) + 
          Payload[4];              
        Serial.print("ID: ");
        Serial.println(result);
        identificador = result;
        requisicao();
        pregunta = true;
        memset(Payload, 0, sizeof(Payload));

        
}


int requisicao() {
 
  if (client.connect(server, 8090)) {
    Serial.println("Conectado");
    client.print("GET /arduinodb/rfid.php?rfid=");
    client.print(identificador);
    client.println(" HTTP/1.0");
    client.println("Host: 192.168.0.10");
    client.println("Connection: close");
    client.println();

  }
  else {

    Serial.println("Conexão falhou");
    Serial.println("Desconectando");
   
  }
 
  delay(1000);
  
  while (client.available()) {
    char c = client.read();
    codigo += c;

    fim = true;
   
 
  }
  
  if (fim)  {
   //Serial.println(codigo);
    int comprimento = codigo.length();
    int posicion = codigo.indexOf("*");
    rfid_servidor = "";
 
    for (int i = posicion + 1; i < comprimento; i ++){
      if (codigo[i] == '^') i = comprimento;
      else rfid_servidor += codigo[i];
    }

    fim = false;

    // verifica se o usuário está permitido
    if(rfid_servidor.length() == 0){
      Serial.println("Acesso negado: Usuário não encontrado.");
      client.stop(); 
      digitalWrite(Led_vermelho, HIGH);
      delay(1000);
      digitalWrite(Led_vermelho, LOW);
      
    } else {
      Serial.println("Acesso liberado: " + rfid_servidor);
      Serial.println("Desconectando");
      client.stop();
      digitalWrite(Led_verde, HIGH);
      digitalWrite(Rele_porta, HIGH);
      delay(1000);
      digitalWrite(Led_verde, LOW);
      digitalWrite(Rele_porta, LOW);
      
    }
    
  }
  codigo="";
  return 1;
}

try with both...

memset(Payload, 0, sizeof(Payload));
RDM6300.decode(2);