Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#rc522-rfid
node.js module to access a rfid reader with rc522 chipset which is connected a raspberry pi

## Warning
The library work with RPI v1 only. It is based on bcm2835 module. The RIPv2 has different addresses.
```
On RPI 2, the peripheral addresses are different and the bcm2835 library gets them from reading /proc/device-tree/soc/ranges. This is only availble with recent versions of the kernel on RPI 2.
```
Source: http://www.airspayce.com/mikem/bcm2835/
Issue: https://github.com/sbrinkmann/rc522-rfid/issues/2

## Purpose
This node module is to access RFID reader with a rc522 chipset (e.g. http://amzn.com/B00GYR1KJ8) via GPIO interface of the raspberry pi.

Expand Down Expand Up @@ -33,4 +41,4 @@ var rc522 = require("rc522-rfid");
rc522(function(rfidSerialNumber){
console.log(rfidSerialNumber);
});
```
```
154 changes: 77 additions & 77 deletions src/accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,100 +10,100 @@
uint8_t initRfidReader(void);

char statusRfidReader;
uint16_t CType=0;
uint16_t CType = 0;
uint8_t serialNumber[10];
uint8_t serialNumberLength=0;
uint8_t noTagFoundCount=0;
uint8_t serialNumberLength = 0;
uint8_t noTagFoundCount = 0;
char rfidChipSerialNumber[23];
char rfidChipSerialNumberRecentlyDetected[23];
char *p;
int loopCounter;

using namespace v8;

Handle<Value> RunCallback(const Arguments& args) {
HandleScope scope;

Local<Function> callback = Local<Function>::Cast(args[0]);
const unsigned argc = 1;

InitRc522();

for (;;) {
statusRfidReader = find_tag(&CType);
if (statusRfidReader == TAG_NOTAG) {

// The status that no tag is found is sometimes set even when a tag is within reach of the tag reader
// to prevent that the reset is performed the no tag event has to take place multiple times (ger: entrprellen)
if (noTagFoundCount > 2) {
// Sets the content of the array 'rfidChipSerialNumberRecentlyDetected' back to zero
memset(&rfidChipSerialNumberRecentlyDetected[0], 0, sizeof(rfidChipSerialNumberRecentlyDetected));
noTagFoundCount = 0;
}
else {
noTagFoundCount++;
}

usleep(200000);
continue;
} else if (statusRfidReader != TAG_OK && statusRfidReader != TAG_COLLISION) {
continue;
}

if (select_tag_sn(serialNumber,&serialNumberLength) != TAG_OK) {
continue;
}

// Is a successful detected, the counter will be set to zero
noTagFoundCount = 0;

p=rfidChipSerialNumber;
for (loopCounter = 0; loopCounter < serialNumberLength; loopCounter++) {
sprintf(p,"%02x", serialNumber[loopCounter]);
p+=2;
}

// Only when the serial number of the currently detected tag differs from the
// recently detected tag the callback will be executed with the serial number
if(strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0)
{
Local<Value> argv[argc] = { Local<Value>::New(String::New(&rfidChipSerialNumber[1])) };
callback->Call(Context::GetCurrent()->Global(), argc, argv);
}

// Preserves the current detected serial number, so that it can be used
// for future evaluations
void RunCallback(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

Local<Function> callback = Local<Function>::Cast(args[0]);
const unsigned argc = 1;

InitRc522();

for (;;) {
statusRfidReader = find_tag(&CType);
if (statusRfidReader == TAG_NOTAG) {

// The status that no tag is found is sometimes set even when a tag is within reach of the tag reader
// to prevent that the reset is performed the no tag event has to take place multiple times (ger: entrprellen)
if (noTagFoundCount > 2) {
// Sets the content of the array 'rfidChipSerialNumberRecentlyDetected' back to zero
memset(&rfidChipSerialNumberRecentlyDetected[0], 0, sizeof (rfidChipSerialNumberRecentlyDetected));
noTagFoundCount = 0;
} else {
noTagFoundCount++;
}

usleep(200000);
continue;
} else if (statusRfidReader != TAG_OK && statusRfidReader != TAG_COLLISION) {
continue;
}

if (select_tag_sn(serialNumber, &serialNumberLength) != TAG_OK) {
continue;
}

// Is a successful detected, the counter will be set to zero
noTagFoundCount = 0;

p = rfidChipSerialNumber;
for (loopCounter = 0; loopCounter < serialNumberLength; loopCounter++) {
sprintf(p, "%02x", serialNumber[loopCounter]);
p += 2;
}

// Only when the serial number of the currently detected tag differs from the
// recently detected tag the callback will be executed with the serial number
if (strcmp(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber) != 0) {
Local<Value> argv[argc] = {
String::NewFromUtf8(isolate, &rfidChipSerialNumber[0])
};

callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

// Preserves the current detected serial number, so that it can be used
// for future evaluations
strcpy(rfidChipSerialNumberRecentlyDetected, rfidChipSerialNumber);

*(p++)=0;
}
*(p++) = 0;
}

bcm2835_spi_end();
bcm2835_close();

return scope.Close(Undefined());
bcm2835_spi_end();
bcm2835_close();
}

void Init(Handle<Object> exports, Handle<Object> module) {
initRfidReader();
module->Set(String::NewSymbol("exports"), FunctionTemplate::New(RunCallback)->GetFunction());
initRfidReader();
NODE_SET_METHOD(module, "exports", RunCallback);
}

uint8_t initRfidReader(void) {
uint16_t sp;

sp=(uint16_t)(250000L / DEFAULT_SPI_SPEED);
if (!bcm2835_init()) {
return 1;
}

bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(sp); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
return 0;
uint16_t sp;

sp = (uint16_t) (250000L / DEFAULT_SPI_SPEED);
if (!bcm2835_init()) {
return 1;
}

bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(sp); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
return 0;
}

NODE_MODULE(rc522, Init)