Go Down

Topic: Best way search SD Card file for authorized RDIF tag EPC number (Read 219 times) previous topic - next topic

MikeOrlando02


Hello,

I have a sketch that currently is up and running where I have a list array of RFID EPC tab numbers  of chars.  Each tag (key) in the array is 24 chars + null.  The code loops though the array checking the serial input with the array of keys to determine if valid.  My issue is that the number of users I will need to have will exceed the Arduino's memory so I will need to move the list of keys to a txt/CSV on an SD card.  Unless there is a better way to go around this that someone would be willing to share.

My question is how to most efficiently search, loop, etc. though the file on the SD card to find a match.  I have searched through the forum and some examples read each line of the file into the Arduino and then look for a comparison using similar logic that I have now. 

What is the best method of doing this?  readBytesUntil()?  Do I have any benefits of knowing that each key will be 25 chars every time?  Is there a different function I should consider learning and implementing?


Any tips would be appreciated.
Mike

PaulRB

Hi Mike.

I suggest the following:

On the PC, sort the keys into alpha order, using Excel or similar, before saving in text format to the SD card.

On the Arduino, when you need to search for a key, use a binary search. Remember playing a game, when you were a kid, where someone thought of a number between 1 and 100 and you had to guess the number with the least number of questions? You started by asking if the number was over 50. If it was not, you asked if it was over 25. If it was, you asked if it was over 37 and so on. That's a binary search. To guess a number between 1 and 100, you only need to ask 7 questions at most.

It may be better to open the file in setup() and keep it open. Also in setup(), calculate the number of records in the file using size() and the known length of each record (25 plus 1 or 2 bytes at the end of the line) and record this in a variable.

When searching for a key, start at the record that is half way through the file. Read it using seek() and read() into a 25 byte buffer and compare to the key to be checked using strcmp(). If it matches, then great. Otherwise, if it is greater, focus on the second half of the file, otherwise the first half. Keep reading and checking the record half way between the upper and lower search limits, until you zero in on the matching key (or not).

This way, even if there are 1000 keys to be searched, you can find it (or not) in 10 or 11 reads of the file.

MikeOrlando02


Thanks for taking the time to help me out.  I should have mentioned, the EPC keys are in HEX so I cannot do a less than or greater than comparison.  Unless I am not following.

PaulRB

the EPC keys are in HEX
Hex is fine. No problem, it will work as well with hex as with text, numbers...

MikeOrlando02

I chickened out on this part of my project, but now it is time to move forward as I would like to be able to add new authorized EPC tags/keys without reprogramming.  Say I have 50-75 keys stored on my SD card.  If I use Paul's higher/lower logic to find the key on the SD card... is this noticeably slower than me looping through the array I have on-board the uno now? 

If so, would the best solution would be to upgrade to a Mega and then store keys on the SD card so they can be easily updated, but when the uno boots to move them all into the array at setup?  Best of both worlds??  or bad idea?

wildbill

I don't think looping through 75 records is going to cause a noticeable delay from a user perspective. Try it with your simple version and see before you try to optimize.

Obviously, getting more capable hardware will let you store the keys in RAM, but again, prove to yourself that you need to do it.

Slumpert

A alternative method of this to create individual text files named for each valid tags last 9 digits. Inside each file have the full ID that is valid, if for some reason you end up having two keys with matching last 9 digits then have two entries in the file with each full ID.

Now when your sketch receives a ID code, you can create a string based on the last 9 and then try to open that string name on in your SD.

If the file exists, read and compare.

MikeOrlando02

I don't think looping through 75 records is going to cause a noticeable delay from a user perspective. Try it with your simple version and see before you try to optimize.

Obviously, getting more capable hardware will let you store the keys in RAM, but again, prove to yourself that you need to do it.
Thanks!

Do you think it wouldn't be noticeable to loop through 75 in RAM or even if it was on SD?

Paul_KD7HB

A alternative method of this to create individual text files named for each valid tags last 9 digits. Inside each file have the full ID that is valid, if for some reason you end up having two keys with matching last 9 digits then have two entries in the file with each full ID.

Now when your sketch receives a ID code, you can create a string based on the last 9 and then try to open that string name on in your SD.

If the file exists, read and compare.
I suggest there is a problem with this option. The SD uses logic from the old floppy disk days. EACH FILE will take 512 bytes of space from your SD card. If you go this way, be sure to use a large capacity SD card.

Paul

MikeOrlando02

I suggest there is a problem with this option. The SD uses logic from the old floppy disk days. EACH FILE will take 512 bytes of space from your SD card. If you go this way, be sure to use a large capacity SD card.

Paul
Thanks.  I think I want to try the binary search and have the sketch:

1) Load the array from the SD card at setup
2) Sort the array at setup
3) Run a binary search of the array when code is checked

Romonaga

A alternative method of this to create individual text files named for each valid tags last 9 digits. Inside each file have the full ID that is valid, if for some reason you end up having two keys with matching last 9 digits then have two entries in the file with each full ID.

Now when your sketch receives a ID code, you can create a string based on the last 9 and then try to open that string name on in your SD.

If the file exists, read and compare.
This is a very valid suggestion and was the same one I was going to make.  If the file exists, open compare, if the file does not exist well security please  this person out of here.
The universe exists only because we are aware of it.
We want a few mad people now. See where the sane ones have landed us!

Go Up