-->
Page 1 of 2

RFID lock example

PostPosted: Mon Sep 12, 2016 8:57 am
by Ecoli-557
OK ESPers, here for your information is a short, but effective RFID lock that will fire a lock based on a library of up to 10 ID tags. The number of tags is easily modified.
Continued Thanks go out to Mike who is slowly beating into my head what the ESP is doing while it is 'waiting' but this program works and it will help those who want to fire an interrupt via a serial input.
Mike really is busting his butt to help others.....
Those of us who work at a lower level will appreciate how to get hex bytes from the reader <grin>.

The reader is a Seeedstudio mini which outputs only 5 bytes - no header. I chose it for this learning task because it is 3.3v compliant. Picture below.

Code below:
Code: Select allmemclear
print "Select operation mode"         'Prints option for selection; Read (normal) or Add which adds new tags
mode = "read"                     'Default
dropdown mode, "add,read"
maxtags = 10                     'Max user ID tags used
dim idtags(10)                     'Tag ID array
dim h$(5)                        'Hex array
dim d(5)                        'Decimal array

'Read in added tags from flash
for x = 1 to maxtags
    idtags(x) = read("idtag" & str(x))
    print idtags(x)                  'Prints on screen for verification
next x

'activate serial branch to catch rfid tag data
serialbranch [check.rfid]            'Works well, Thanks Mike!
wait

[check.rfid]                     'Magic happens here
ID = ""
do                              'Reads the data from the tag, this reader spits out 5 hex bytes
    d(1) = str(serial.read.int())
    d(2) = str(serial.read.int())
    d(3) = str(serial.read.int())
    d(4) = str(serial.read.int())
    d(5) = str(serial.read.int())
Loop while Serial.available()          'loop through and put each byte in its own var

for x=1 to 5                     'Converts dec to hex in bytes
        h$(x)=hex(d(x))       
next x

'for x=1 to 5                     'This confirms by printing that it is working
'        print "x=" & x & "  " & "decimal=" & d(x) & "  " & "Hex=" & (h$(x))
'next x   
    ID = hextoint(h$(1)&h$(2)&h$(3)&h$(4))      'Equates ID in decimal

if mode == "add" then
    for x = 1 to maxtags
        'check if id is already in system
        if idtags(x) == ID then
           
           print "tag already in system"
           return
        end if

        'Add tag to next empty slot
        if idtags(x) == 0 then
           idtags(x) = ID
           'add tag to flash storage
           write("idtag" & str(x), ID)
           print "Tag Added"
           return
        end if
    next x
 else
    'Look up tag and exicute branch for if tag is in system
    for x = 1 to maxtags
        'check if id is already in system
        if idtags(x) == ID then
           x = 10
          Print "Valid tag detected"

          'Activate pin to open door
          io(po,d0,0)
          'Give 10 seconds for user to enter
          timer 10000, [lock.the.door]
          return
        end if
    next x
    print "Invalid tag detected"
end if
return

[lock.the.door]
timer 0
io(po,d0,1)
wait

Re: RFID lock example

PostPosted: Tue Sep 13, 2016 10:40 am
by Mmiscool
Nice. Thanks for posting.

Re: RFID lock example

PostPosted: Tue Sep 13, 2016 3:29 pm
by viscomjim
This is very cool. Nice job Ecoli-557!!!! Thanks for posting and keep them coming!!!!

Re: RFID lock example

PostPosted: Tue Sep 13, 2016 5:04 pm
by Electroguard
Well done Ecoli, again.
It's something I had planned for my ex-ambulance camper conversion eventually, so hopefully it will save me a heap of time".
I looked up the reader which seems to be 125KHz with a 35mm range, is that about right?