Skip to content

Instantly share code, notes, and snippets.

@andrewn
Last active August 29, 2015 14:08
Show Gist options
  • Save andrewn/b1f1977b54ba76e07be2 to your computer and use it in GitHub Desktop.
Save andrewn/b1f1977b54ba76e07be2 to your computer and use it in GitHub Desktop.
RFID reader

Espruino javascript code to poll for serial number of RFID card. Since the reader communicates over UART, should work with other controllers that have UART, such as the Raspberry Pi.

Works with MFRC500-based RFID Reader/Writer - 13.56Mhz

Pinout

   .-----------------------------.
   | [1][2]      [5][4][3][2][1] |
   |  J3              J1         |
   |                             |
   |           RDM880 (top view) |
   |                             |
   |                  J2         |
   |             [5][4][3][2][1] |
   .-----------------------------.
RFID Breakout pin Espruino pin
J2 5 Bat
J2 4 GND
J2 3 C11
J2 2 C10
Serial4.setup(9600);
var Cmd = {
start: 0xAA
, ok : 0x00
, end : 0xBB
};
var debug = false;
var query = [Cmd.start, 0x00, 0x03, 0x25, 0x26, 0x00, 0x00, Cmd.end];
function parseData(string) {
return string
.split('')
.map (
function (c) { return c.charCodeAt (0); }
);
}
function hex(num) {
return (num < 10 ) ? '0' + num : num.toString(16);
}
function hexString(array) {
return array
.map(hex)
.join('');
}
var buffer = [];
var lastTagId = null;
function done(id) {
if (id && lastTagId != id) {
lastTagId = id;
print(id);
} else if (id == null && lastTagId != null) {
lastTagId = null;
print('Removed');
//print('no tags readable');
}
setTimeout(doQuery, 0);
}
function parseResponse(b) {
if (debug) { print(b); }
var status = b[3],
numTags = b[4],
id = [];
if (status == Cmd.ok) {
if (debug) { print('read: ok'); }
if (debug) { print(numTags); }
if (debug) {
switch(numTags) {
case 0x00: print('one tag readable');
break;
case 0x01: print('at least 2 tags readable');
break;
}
}
id = b.slice(5, 9);
if (debug) { print('ID: ' + id); }
id = hexString(id);
done(id);
} else {
done();
}
}
function receive(data) {
var parsed = parseData(data);
buffer = buffer.concat(parsed);
if (debug) { print(parsed); }
if (buffer.indexOf(Cmd.end) > -1) {
parseResponse(buffer);
buffer = [];
}
}
Serial4.on('data', receive);
function doQuery () {
Serial4.write(query);
}
// Start polling
done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment