RFID Door Lock Shield trouble

Control electric door locks and strike plates using your Arduino and an RFID module. Serial interface for an external RFID reader, and a switched output for door strike control. [Product page]
Post Reply
jagerp
Posts: 4
Joined: Wed Dec 18, 2013 9:41 pm

RFID Door Lock Shield trouble

Post by jagerp » Wed Dec 18, 2013 10:01 pm

Hi, As this is my first post apologies for any misconceptions or errors.

I am using the Door lock shield, Uno R3 and RDM630 rfid reader in a small project so I can open my garage door without the need of a key. My reason for building this is that I do allot of bike riding road and mountain and would rather not take a key.

I am stuck somewhat; I have constructed the circuit and am using the practical arduino example but when reading the tag with the reader I always get the following results.
if((val = rfid.read()) == 2) { // Check for header
bytesRead = 0;
while (bytesRead < 12) { // Read 10 digit code + 2 digit checksum
val = rfid.read();
RFID reader starting up
Tag read: FFFFFFFFFF
Checksum: FF -- passed.
Tag not authorized

Now I know that the five different tags I have are all unique tag numbers and don't match the above FFFFFFFFFF .

By getting a result I know that software serial is working but not the result obvioulsy that I am after.

I did manage to get it working on a mega using the below after scanning the web for info. obviously a mega is an overkill for what I need hence my search for more info.
if(Serial1.available() > 0) {
//if((val = Serial1.read()) == 2) { // Check for header
//bytesRead = 0;
//while (bytesRead < 12) {
//if( Serial1.available() > 0) { // Read 10 digit code + 2 digit checksum
// val = Serial1.read();
the above code works using serial1 on a mega, so I am thinking that it has something to do with software serial or the code relating to the rfid.read section of the practical arduino example .

Appreciate any help or pointers hopefully I have given enough info.

Thanks Paul

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: RFID Door Lock Shield trouble

Post by andrew » Thu Dec 19, 2013 9:13 am

Serial1 is pins D18 and D19 on the Mega - are you manually connecting the RFID reader straight to the Mega?

Anyhow, back to the Uno and the shield for a moment. Could you please connect the RFID reader to the shield, and the shield to the Uno, and upload the following sketch:

Code: Select all

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()  
{
  // Open connection to host at 38400bps
  Serial.begin(38400);

  // Open connection to RFID module at 9600bps
  mySerial.begin(9600);
}
void loop()
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}
Then wave some tags at the reader, and let me know what appears in the serial monitor?
Thanks
john

jagerp
Posts: 4
Joined: Wed Dec 18, 2013 9:41 pm

Re: RFID Door Lock Shield trouble

Post by jagerp » Fri Dec 20, 2013 5:35 am

Hi John, here are the results with uploaded code you posted.


Serial Monitor Results, using each card/fob
08000AC075B7
08000AD6F226
280015E6FF24
03000B014B42
0300019DE07F

Regards Paul

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: RFID Door Lock Shield trouble

Post by andrew » Fri Dec 20, 2013 11:18 am

Thanks for doing that. By seeing those different tag numbers it tells us that the hardware is working as expected.

So I've just had another look at the code for the project out of the PA book and it looks as if some of the pin numbers are incorrect for use with the DLOCK shield. Give the following code a try:

Code: Select all

/**
 * RFID Access Control Single
 *
 * This project implements a single stand-alone RFID access control
 * system that can operate independently of a host computer or any
 * other device. It uses either an ID-12 RFID reader module from ID
 * Innovations or an RDM630 RFID reader module from Seeed Studio to
 * scan for 125KHz RFID tags, and when a recognised tag is identified
 * it toggles an output for a configurable duration, typically 2
 * seconds. The output can then be used to control a relay to trip an
 * electric striker plate to release a door lock.
 *
 * Because this project is intended to provide a minimal working system
 * it does not have any provision for database updates to be managed
 * externally from a host, so updates to the accepted cards must be
 * made by changing the values in the code, recompiling the program,
 * and re-uploading it to the Arduino. It does however report card
 * readings (both successful and unsuccessful) via the serial
 * connection so you can monitor the system using a connected computer.
 *
 * Some of this code was inspired by Tom Igoe's excellent RFID tutorial
 * which is detailed on his blog at:
 *   http://www.tigoe.net/pcomp/code/category/PHP/347
 * And also from the ID-12 example code on the Arduino Playground at:
 *   http://www.arduino.cc/playground/Code/ID12
 *
 * Copyright Jonathan Oxer <jon@oxer.com.au>
 * http://www.practicalarduino.com/projects/medium/rfid-access-control
 */

// Set up the serial connection to the RFID reader module. In order to
// keep the Arduino TX and RX pins free for communication with a host,
// the sketch uses the SoftwareSerial library to implement serial
// communications on other pins.
#include <SoftwareSerial.h>

// The RFID module's TX pin needs to be connected to the Arduino. Module
// RX doesn't need to be connected to anything since we won't send
// commands to it, but SoftwareSerial requires us to define a pin for
// TX anyway so you can either connect module RX to Arduino TX or just
// leave them disconnected.

// If you have built the circuit exactly as described in Practical
// Arduino, use pins D2 / D3:
#define rxPin 2
#define txPin 3
// If you are using the Freetronics RFID Lock Shield, use pins D4 / D5:
/* #define rxPin 4 */
/* #define txPin 5 */

// Create a software serial object for the connection to the RFID module
SoftwareSerial rfid = SoftwareSerial( rxPin, txPin );

// Set up outputs for the strike plate and status LEDs.
// If you have built the circuit exactly as described in Practical
// Arduino, use pins D12 and D13:
#define strikePlate 9
#define ledPin 8
// If you are using the Freetronics RFID Lock Shield, use pins D6 / D7:
/* #define strikePlate 6 */
/* #define ledPin 7 */

// Specify how long the strike plate should be held open.
#define unlockSeconds 2

// The tag database consists of two parts. The first part is an array of
// tag values with each tag taking up 5 bytes. The second is a list of
// names with one name for each tag (ie: group of 5 bytes).
char* allowedTags[] = {
  "0104F5B523",         // Tag 1
  "04146E8BDD",         // Tag 2
  "0413BBBF23",         // Tag 3
};

// List of names to associate with the matching tag IDs
char* tagName[] = {
  "Jonathan Oxer",      // Tag 1
  "Hugh Blemings",      // Tag 2
  "Dexter D Dog",       // Tag 3
};

// Check the number of tags defined
int numberOfTags = sizeof(allowedTags)/sizeof(allowedTags[0]);

int incomingByte = 0;    // To store incoming serial data

/**
 * Setup
 */
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(strikePlate, OUTPUT);
  digitalWrite(strikePlate, LOW);

  Serial.begin(38400);   // Serial port for connection to host
  rfid.begin(9600);      // Serial port for connection to RFID module

  Serial.println("RFID reader starting up");
}

/**
 * Loop
 */
void loop() {
  byte i         = 0;
  byte val       = 0;
  byte checksum  = 0;
  byte bytesRead = 0;
  byte tempByte  = 0;
  byte tagBytes[6];    // "Unique" tags are only 5 bytes but we need an extra byte for the checksum
  char tagValue[10];

  // Read from the RFID module. Because this connection uses SoftwareSerial
  // there is no equivalent to the Serial.available() function, so at this
  // point the program blocks while waiting for a value from the module
  if((val = rfid.read()) == 2) {        // Check for header
    bytesRead = 0;
    while (bytesRead < 12) {            // Read 10 digit code + 2 digit checksum
      val = rfid.read();

      // Append the first 10 bytes (0 to 9) to the raw tag value
      if (bytesRead < 10)
      {
        tagValue[bytesRead] = val;
      }

      // Check if this is a header or stop byte before the 10 digit reading is complete
      if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) {
        break;                          // Stop reading
      }

      // Ascii/Hex conversion:
      if ((val >= '0') && (val <= '9')) {
        val = val - '0';
      }
      else if ((val >= 'A') && (val <= 'F')) {
        val = 10 + val - 'A';
      }

      // Every two hex-digits, add a byte to the code:
      if (bytesRead & 1 == 1) {
        // Make space for this hex-digit by shifting the previous digit 4 bits to the left
        tagBytes[bytesRead >> 1] = (val | (tempByte << 4));

        if (bytesRead >> 1 != 5) {                // If we're at the checksum byte,
          checksum ^= tagBytes[bytesRead >> 1];   // Calculate the checksum... (XOR)
        };
      } else {
        tempByte = val;                           // Store the first hex digit first
      };

      bytesRead++;                                // Ready to read next digit
    }

    // Send the result to the host connected via USB
    if (bytesRead == 12) {                        // 12 digit read is complete
      tagValue[10] = '\0';                        // Null-terminate the string

      Serial.print("Tag read: ");
      for (i=0; i<5; i++) {
        // Add a leading 0 to pad out values below 16
        if (tagBytes[i] < 16) {
          Serial.print("0");
        }
        Serial.print(tagBytes[i], HEX);
      }
      Serial.println();

      Serial.print("Checksum: ");
      Serial.print(tagBytes[5], HEX);
      Serial.println(tagBytes[5] == checksum ? " -- passed." : " -- error.");

      // Show the raw tag value
      //Serial.print("VALUE: ");
      //Serial.println(tagValue);

      // Search the tag database for this particular tag
      int tagId = findTag( tagValue );

      // Only fire the strike plate if this tag was found in the database
      if( tagId > 0 )
      {
        Serial.print("Authorized tag ID ");
        Serial.print(tagId);
        Serial.print(": unlocking for ");
        Serial.println(tagName[tagId - 1]);   // Get the name for this tag from the database
        unlock();                             // Fire the strike plate to open the lock
      } else {
        Serial.println("Tag not authorized");
      }
      Serial.println();     // Blank separator line in output
    }

    bytesRead = 0;
  }
}

/**
 * Fire the relay to activate the strike plate for the configured
 * number of seconds.
 */
void unlock() {
  digitalWrite(ledPin, HIGH);
  digitalWrite(strikePlate, HIGH);
  delay(unlockSeconds * 1000);
  digitalWrite(strikePlate, LOW);
  digitalWrite(ledPin, LOW);
}

/**
 * Search for a specific tag in the database
 */
int findTag( char tagValue[10] ) {
  for (int thisCard = 0; thisCard < numberOfTags; thisCard++) {
    // Check if the tag value matches this row in the tag database
    if(strcmp(tagValue, allowedTags[thisCard]) == 0)
    {
      // The row in the database starts at 0, so add 1 to the result so
      // that the card ID starts from 1 instead (0 represents "no match")
      return(thisCard + 1);
    }
  }
  // If we don't find the tag return a tag ID of 0 to show there was no match
  return(0);
}
Finally there is a different way of reading the tags, but with the same final result in another tutorial over at tronixstuff:

http://tronixstuff.com/2013/11/19/ardui ... r-15-rfid/

If you work through that tutorial the last example sketch will do what you want, just trigger D9 when you get a successful match in the final example.

Let me know how you get on.
cheers
john

jagerp
Posts: 4
Joined: Wed Dec 18, 2013 9:41 pm

Re: RFID Door Lock Shield trouble

Post by jagerp » Fri Dec 20, 2013 11:31 pm

Hi John,


Still no luck with the PA project code. But I did try the tronixstuff scketch and I had success, so as you said I just need to adjust pin numbers to suit the shield.

Appreciate your help, still my curious nature wonders where the other sketch fails. But I will leave that for another day, I can now take my project off the breadboard and put in to operation

Cheers Paul

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: RFID Door Lock Shield trouble

Post by andrew » Sun Dec 22, 2013 8:34 am

jagerp wrote:Hi John,


Still no luck with the PA project code. But I did try the tronixstuff scketch and I had success, so as you said I just need to adjust pin numbers to suit the shield.

Appreciate your help, still my curious nature wonders where the other sketch fails. But I will leave that for another day, I can now take my project off the breadboard and put in to operation

Cheers Paul
Hi Paul
Apologies for the delay in my reply. Good to hear you have it working. PA was written a few years back so I will have a word with the author about updating the code. In the meanwhile if you have any more questions about the content in the book etc., let us know in the forum:
viewforum.php?f=23
or email support@freetronics.com
John

Post Reply