NFC door lock help

Hey ladies and gents,

I'll start this thread by saying, YES I have Googled it! Spent hours on Google and YouTube over the past 3 days trying to make heads or tails of this problem with no luck.

So, here's my project. It's a simple (Ha!) NFC controlled garage door locking/unlocking system. All I want to do is scan an NFC card/NFC device and toggle an actuator... Scan once for unlock, scan again to lock. Seems pretty simple to me...

I have my head around the mechanics and installation of it, my problem is this though, I suck at the finer points of writing/getting sketches to work. I have an elechouse PN532 NFC/RFID module (http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2242) hooked up to an Arduino Nano, and I have no idea how to write the sketch so the Arduino accepts certain NFC chips (authorized ID's) and not others (unauthorized ID's). In other words, I'm stuck trying to figure out the authentication protocol.

Please please help!

So you've got the library working on your Arduino and you're able to run their example sketch? That's the first step - whatever demo they provide should work, to test the wiring.

Then you will need to get the Arduino to remember which tags are authorised. Don't put the tag codes into your Arduino sketch because then you will have to plug it into your computer and recompile each time you change a tag. Use the EEPROM to save the authorised tags. Perhaps add another button to the Arduino that can be the 'program' button. (This button is inside the locked box.) Scan a tag while holding the 'program button' and save that tag into the next available slot. Maybe have an 'erase' button so you can remove tags from memory if the tag is lost.

Thanks MorganS. Yeah, I have the library working, and the module scanning tags and printing their ID to the serial monitor. Where do I go from there though. How do I write my sketch though so that it compares cards being scanned to it's list of known/authorised cards? That's where I'm having difficulty. And plugging in and recompiling to add cards doesn't really bother me, particularly if it's easier to write the sketch for it.

Just point me toward any similar project that spells it out and I'm sure I'll fumble through. I just don't know where to get started.

Thanks again for your help.

I saw this concept on shark tank a few months ago. It's called UniKey if you are interested in purchasing a working model and trying to reverse engineer it to use NFC instead of Bluetooth. I've seen these guys at a few technology trade shows. Pretty neat concept, but there's no way i'd currently pay $200 for a lock when I could buy one that worked only with a key for $12. You also have to pay extra for "wireless keys" which is basically a confirmation app (encryption pass thing) that you can give to a babysitter, family members or whatever. I remember seeing this on amazon a while back and people were very upset only two family members could put the key thing on their phone without paying a lot extra. I seem to think they might have addressed that issue and started giving up to 5 keys for free, but don't quote me on that.

------------------------------edit------------------------------------
Wait you want to use it for a garage door........ NFC stands for NEAR Frequency Control. I don't know what range they consider "Near" but this might not be the ideal technology to use for what you are trying to accomplish. I'd look into the range of NFC first

Hey, thanks for the input Thomas499. I am fully aware of the limitations with NFC (near field communication) and its exactly what I want. I'm using it exactly like any other security system does with a tap and go card or fob. Reason I want to use this method is simply because I always have my phone on me and sometimes walk outside without my garage keys. I just want to be able to unlock the garage with a tap of my phone rather than having to go back inside to get my keys.

Like I said, I imagine the sketch is actually quite simple, something along the lines of putting a number of known cards into the setup as integers and comparing the serial read to those integers? Am I on the right track people?

I haven't seen a NFC shield for arduino personally, but if they do make them then it probably wouldn't be the hardest thing in the world to rig.

People usually keep their garage keys on their key chain with their car keys, and house keys. Without having car keys with you.... besides the point though. Out of curiosity, what is the range of NFC? Is it a few inches, a few feet, or more than 7 feet? I'm just curious, where i'm from NFC isn't popular yet.

Standards NFC has a range of 10 cm max. The tags are powered by inductive power transfer, which is why you can't easily read them at distance without an awfully powerful coil since the power drops off at the inverse cube of the distance. For a reliable read the card has to nearly touch the reader.

Just point me toward any similar project that spells it out and I'm sure I'll fumble through. I just don't know where to get started.

How about here:-

https://code.google.com/p/open-access-control/

I will start by saying that I am not a proficent code writer nor do I know any details about NFC systems. Now, based on things said in this thread I will make the following general statements: I assume that every card, fob, phone etc has a unique ID number. You said that you have the arduino printing the ID number of a scanned device to the serial monitor. If the above is true then I think the simple way to do what you want is to simply capture the ID of the scanned device to a variable and then compair that to a list of authorized ID numbers. If there is a match toggle the output for the lock. There are some details that you will have to work out as you design the code, for example, you will only want to scan the list once with each ID scanned or else you will effectivly be locking and unlocking the door with each scan of the code loop. I hope this makes sence and gives you a direction from another noob.......

R

OK, so am I on the right track if I were to do something like;

int incomingByte = 0
int LockPin 13
int Buzzer 14
const KnownCard = 12345678

Void setup {

Serial.begin(9600);
PinMode (LockPin, OUTPUT);
PinMode (Buzzer, OUTPUT);
digitalWrite (LockPin, HIGH);

}

Void loop{

if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

if incomingByte == KnownCard
digitalWrite (LockPin, LOW);

Else if incomingByte != KnownCard;
digitalWrite (Buzzer, HIGH);
delay(1000);
digitalWrite (Buzzer, LOW);
}

Like I said, I sort of understand the theory behind it, that is read whats coming in through the serial and check it against known ID's, I just don't know exactly how to put it into a working sketch. Also, how do I get it to work the other way for known cards to lock the door again? Basically, How do I toggle between HIGH and LOW with the same value when it is entered a second time.

How about posting code that compiles?

Also const is not a variable type.
Look and see how big the largest number you can get into each variable type and see what you need to define that as.

Cheers for the help Grumpy... Stupid me, putting code up that doesn't compile and then asking for those who know more about Arduino than I for help.

Point taken on the variable type issue. Thanks for the help there, however I have no idea what you mean by your last point

Grumpy_Mike:
Look and see how big the largest number you can get into each variable type and see what you need to define that as.

Look at what, the number I'm trying to identify, or the largest number the variable type can theoretically deal with?

Look at the data types of variables, byte, int, long and long long. Each has a limit on the biggest value it will hold, what is the biggest value you want to hold as a token? You need to make your know card variable that type.

If you post code that does not compile then how are you going to make any progress? If you know so little that you can not make it compile then that is your immediate problem and you should be asking about that and not the function of your code. You have to learn to stand before you can walk. Successful learning is always one step at a time.

Do you know why it would not compile? Hint ;

Ah, now I follow you. I'll have to scan a card when I get home to determine the value and hence correct variable. As for the code, I knew without even trying that the original code I typed out wouldn't work, I typed it on my phone whilst at work and will admit it wasn't anywhere near a working sketch, note however that I did preface it with "something like".

Anyway, so now I'm at the laptop with the IDE and I've come up with the following. It compiles but I don't know if it will work, and to be honest, I very much doubt it will work as I'm hoping.

Here it is;

#include "nfc.h"

NFC_Module nfc;

int incomingByte = 0;
int LockPin = 13;
int Buzzer = 14;
int KnownCard;
int LockState = 0

void setup (void)
{
Serial.begin(9600);
nfc.begin();
Serial.println("NFC car door lock project. Booting...");

uint32_t versiondata = nfc.get_version();
if (! versiondata) {
Serial.println("Didn't find NFC board");
while (1); }// halt

pinMode (LockPin, OUTPUT);
pinMode (Buzzer, OUTPUT);
digitalWrite (LockPin, HIGH);
KnownCard = 12345678;
}

void loop ()
{

{if (Serial.available() > 0)
// read the incoming byte:
incomingByte = Serial.read();}

{if (incomingByte == KnownCard)
digitalWrite (LockPin, LOW);

else if (incomingByte != KnownCard)
digitalWrite (Buzzer, HIGH);
delay(1000);
digitalWrite (Buzzer, LOW);}
}

Now three questions with what I have here so far. Firstly, are there any glaring issues in what I have so far? Secondly, can anyone tell me how to write my LockState statement in the loop so that it toggles between locked and unlocked with each swipe of a card? Lastly, should there be a delay between the read of the card and the subsequent incoming serial data, and the beginning of the comparison to the KnownCard int?

OK so read what I said again, you are not understanding this.

Read this, and you will see an int can hold numbers between -32,768 to 32,767.
You set the variable KnownCard = 12345678 which is a number that is 376 times larger than the largest number an int can hold.
Basically an int is just two bytes or 16 bits.

Will the code work? Well no way it is wrong in all sorts of ways you might as well start again.
I can't reproduce it because you have not posted a link to the library you have used.

There is no way that just reading a single byte from the serial port will give you the card number, the card number is sent over several bytes. Is there not a call in the library to get the data? Is the reader even connected to the Arduino's serial port, I would have thought not because that would interfere with uploading. What you have to do is to assemble these bytes into a single number before you can then go on to do a compare.

Start off with the sample code that comes with that library. Get it to print out the code. Then get that number into a single variable either a long or a long long. Only then can you do a compare.

This reader can be wired up in a number of different ways how have you wired it up?

Hi everyone, i'm trying to make a project using arduino, NFC Shield, fingerprint, and solenoid 12 V to make a door lock.

From outside, solenoid will be unlocked when your uid from NFC tag and your finger was match, and the door can be open, and solenoid will be locked again when you press lockButton.

But i have some problems when i'am adding unlockButton to unlocked the door from inside,
when i press unlockButton, the solenoid can be unlocked, then i press lockButton to lock the Solenoid, but the program only loop in unlockButton, not loop from begining

sorry, if my english not fluently :slight_smile:
please help me, i also attach my program,

smartdoor.ino (3.75 KB)