Jump to content
43oh

[Energia Library] NFC card reading with Energia - MF RC 522


Recommended Posts

  • 2 weeks later...
  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Hi everybody,   >>> UPDATE - Made "proper" Energia Library out of it. Now with proper named methods/class members<<<   >>> UPDATE 2 - Added "keywords.txt" file for synta

Hi! I got my RC522 up and running this morning and just thought I'd share what worked for me..   I tried running three different Energia-versions of the library, but none of them worked. I tried pri

Okay guys,   Did some work this morning to transform the test code into a proper library. It should be more usable and generic. I haven't made a "keywords.txt". The methods should be self explanator

Posted Images

OK, spent a bit of time on this this evening.

Tried knocking together some code to allow hard coding of tag IDs which works OK. Might need to change the for loop though as anything in else just gets repeated, a switch might do the trick but it's late.

For anyone interested, here's my code, would appreciate any improvements that could be made, including an scenario where the tag's not recognized as valid.

/*
	Example file for communicating with the NFRC522. The program prints the card data.
	Created by Eelco Rouw - Originally adapted by Grant Gibson. 
*/

// Pinout
// SDA  - 2.2
// SCK  - 1.5
// Mosi - 1.7
// Miso - 1.6
// IRQ  - NC
// GND  - GND
// RST  - 1.3
// VCC  - VCC

#include <Mfrc522.h>

// the sensor communicates using SPI, so include the library:
#include <SPI.h>

int chipSelectPin = 10;
int NRSTDP = 5;
int ledpin1 = 3;
int ledpin2 = 6;
int cardint = 5;

Mfrc522 Mfrc522(chipSelectPin,NRSTDP);
unsigned char serNum[5];
unsigned char sectorKey[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char readData[16];

void setup() 
{                
	Serial.begin(9600);                       // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
	// Start the SPI library:
	SPI.begin();
	// Initialize the Card Reader
	digitalWrite(chipSelectPin, LOW);
	pinMode(RED_LED, OUTPUT);
        pinMode(ledpin1, OUTPUT);
        pinMode(ledpin2, OUTPUT);
	Mfrc522.Init();  
}

void loop()
{
	unsigned char i,tmp;
	unsigned char status;
	unsigned char str[MAX_LEN];
	unsigned char RC_size;
	unsigned char blockAddr;
	//String mynum = "";
        String mycardno;
        char* myarray[2][5]={  //Array of tags and related names, tag numbers copied from serial console after 1s
          {"205928944228","1251291514471","7748944415","157172894468","611918944247"},
          {"Dad","Mum","Baby","Dog","Cat"}
          };
  	
	status = Mfrc522.Request(PICC_REQIDL, str);	
	if (status == MI_OK)
	{
		Serial.println("Card detected");
		Serial.print(str[0],BIN);
		Serial.print(" , ");
		Serial.print(str[1],BIN);
		Serial.println(" ");
	}

	status = Mfrc522.Anticoll(str);
	memcpy(serNum, str, 5);
	if (status == MI_OK)
	{
		Serial.println("The card's number is  : ");
		Serial.print(serNum[0], DEC);
		Serial.print(" , ");
		Serial.print(serNum[1], DEC);
		Serial.print(" , ");
		Serial.print(serNum[2], DEC);
		Serial.print(" , ");
		Serial.print(serNum[3], DEC);
		Serial.print(" , ");
		Serial.print(serNum[4], DEC);
		Serial.println(" ");
                mycardno = String(serNum[0]) += String(serNum[1]) += String(serNum[2]) += String(serNum[3]) += String(serNum[4]); // Appends the content of the serNum array to give a unique card no
                Serial.println(mycardno);
                Mfrc522.SelectTag(serNum);
                status = Mfrc522.Auth(PICC_AUTHENT1A,1,sectorKey,serNum);
                if (status == MI_OK)
                {
                  Serial.println("Authenticated...\r\n");
                } else
                {
                  Serial.println("Error authenticating...\r\n");
                }
                
                status = Mfrc522.ReadBlock(1, readData);
                if (status == MI_OK)
                {
                  for(i=0; i<16; i++)
                  {                  
                    Serial.write(readData[i]);
                    delay(10);
                  }                  
                } else {
                  Serial.println("Error reading.");
                }
                int i;
                for(i = 0; i < 5; i = i + 1)
                {
                  if (mycardno == myarray[0][i])
                  {
                  Serial.print("Hello ");
                  Serial.println(myarray[1][i]);        // Should Print out name matching tag
                }
                /*else {
                  Serial.println("Bugger Off");
                }*/
                }
  
		delay(1000);
                Mfrc522.Init();	
	}
	//Serial.println(" ");
	Mfrc522.Halt();	                        
}
Link to post
Share on other sites

I think that would be a great idea. You should check the possibilities with the available smart cards, you can do some really neat stuff with it!

 

I could envision you should add a small serial eeprom to the board as you can store an access table and do something with challenge/handshake.

 

I haven't tried the above but it would help to make a solid door mechanism.

 

Kind regards, Eelco

Link to post
Share on other sites

Today I got RF522 module, works fine with Energia library - THANKS.

What do You think about small pcb module RFID-Door Control (MSP430G2+Relay+Buzzer+LED) connected directly to RC522 board?

Sounds similar to what I'm working on, adding a uln2003 to help with the mixed power of the leds and relays.

Serial eeprom sounds like a good idea too, I was going to try with an SD card but it would be overkill!

Link to post
Share on other sites

That looks good Traxman

What's the idea behind your MOSI/MISO setup, I've not seen it done like that before?

The ULN with relay combo is working fine at the minute, are there any equivalents arrays with FETs?

Just migrated my setup to a breadboard and it works perfectly, ready to stick it onto protoboard now.

post-33474-0-24152400-1377848558_thumb.jpg

Link to post
Share on other sites
  • 3 months later...
  • 3 weeks later...
  • 4 weeks later...

I have a whole bunch of problems getting my Stellaris to play nice with my ebay 522 reader...

 

EDIT: got it ^_^here

So far I cant get the serial monitor to send anything back to the computer.

 

I've tried changing the baud rate - 9600 and 2400.

 

also, I'm confused at to the pin connection, I've tried:

// SDA   on Stellaris pinmap SDA(0) is PB_3
// SCK    on Stellaris pinmap SCK(0) is PA_2
// Mosi   on Stellaris pinmap MOSI(0) is PA_5
// Miso   on Stellaris pinmap MISO(0) is PA_4
// IRQ  - not connected
// GND  - GND
// RST  - 1.3  on 430 refers to PUSH2; on Stellaris = PF_0... not sure bout this
// VCC  - 3.3v

 

I tried:
SPI.setModule(0);
in the setup

 

and this returned nothing in the serial monitor..

 

from here I modified the code to send *something* back to the computer over serial..
 

    status = Mfrc522.Request(PICC_REQIDL, str);    
    if (status == MI_OK)
    {
        Serial.println("Card detected");
        Serial.print(str[0],BIN);
        Serial.print(" , ");
        Serial.print(str[1],BIN);
        Serial.println(" ");
    }
else{                                             //added this
  Serial.println("nope");
}

and still I'm getting NOTHING back over serial. which confuses me, I thought Id be getting 'nope' on a new line constantly *confused*

 

in the 'working' code, I'm not rly sure what the line

 

int NRSTDP = 5;

 

refers to, seemingly PUSH2, so I tried changing that to 17 to correspond with the stellaris, not sure if this makes any difference.

 

After this I tried changing the pin wiring around to suit the  "default" for SPI http://forum.43oh.com/topic/3387-spi-pin-map-for-stellaris-launchpad-lm4f120/

 

// Pinout
// SDA  - 2.2  (430 launchpad) -> 10 (stellaris launchpad)
// SCK  - 1.5  (430) -> PB_4 (stellaris)
// Mosi - 1.7  (430) -> PB_7 (stellaris)
// Miso - 1.6  (430) -> PB_6 (stellaris)
// IRQ  - Not connected
// GND  - GND
// RST  - 1.3  (430) -> 1.3 refers to PUSH2; PF_0... not sure bout this
// VCC  - VCC

 

and leaving

 

SPI.setModule(0);

 

out of the code... nothing. I tried putting

 

SPI.setModule(2);

 

in the code..nothing.

 

this is the most recent code, which is basically an amalgamation of the above problems

/*
	Example file for communicating with the NFRC522. The program prints the card data.
	Created by Eelco Rouw - Originally adapted by Grant Gibson. 
*/


// Pinout
// SDA  - 2.2  on pinmap SDA(0) is PB_3.. default - PA_7 or maybe 10
// SCK  - 1.5  on pinmap SCK(0) is PA_2.. default - PB_4
// Mosi - 1.7  on pinmap MOSI(0) is PA_5.. default (no SPI.setModule) - PB_7
// Miso - 1.6  on pinmap MISO(0) is PA_4.. default - PB_6
// IRQ  - NC
// GND  - GND 
// RST  - 1.3  on pinmap... 1.3 refers to PUSH2; PF_0... not sure bout this
// VCC  - VCC

#include <Mfrc522.h>

// the sensor communicates using SPI, so include the library:
#include <SPI.h>

int chipSelectPin = 10;  //SDA on reader (pin 10 is PA_7 on stellaris) although try PB_3 as above instead of 10
int NRSTDP = 17;
Mfrc522 Mfrc522(chipSelectPin,NRSTDP);
unsigned char serNum[5];

void setup() 
{                
	Serial.begin(9600);                       // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
	// Start the SPI library:
        SPI.setModule(2); // taken from http://forum.43oh.com/topic/3387-spi-pin-map-for-stellaris-launchpad-lm4f120/
	SPI.begin();
	// Initialize the Card Reader
	digitalWrite(chipSelectPin, LOW);
	pinMode(RED_LED, OUTPUT);
	Mfrc522.Init();  
}

void loop()
{
	unsigned char i,tmp;
	unsigned char status;
	unsigned char str[MAX_LEN];
	unsigned char RC_size;
	unsigned char blockAddr;
	String mynum = "";
  	
	status = Mfrc522.Request(PICC_REQIDL, str);	
	if (status == MI_OK)
	{
		Serial.println("Card detected");
		Serial.print(str[0],BIN);
		Serial.print(" , ");
		Serial.print(str[1],BIN);
		Serial.println(" ");
	}
else{
  Serial.println("Card detected");
}

	status = Mfrc522.Anticoll(str);
	memcpy(serNum, str, 5);
	if (status == MI_OK)
	{
		Serial.println("The card's number is  : ");
		Serial.print(serNum[0]);
		Serial.print(" , ");
		Serial.print(serNum[1],BIN);
		Serial.print(" , ");
		Serial.print(serNum[2],BIN);
		Serial.print(" , ");
		Serial.print(serNum[3],BIN);
		Serial.print(" , ");
		Serial.print(serNum[4],BIN);
		Serial.println(" ");

		// Should really check all pairs, but for now we'll just use the first

		if(serNum[0] == 29) 
		{
			Serial.println("Hello Eelco");
			digitalWrite(RED_LED, HIGH);
			delay(1000);
			digitalWrite(RED_LED, LOW);
		}     
		delay(1000);	
	}
	//Serial.println(" ");
	Mfrc522.Halt();	                        
}

So far, no dice. Any help with this would be appreciated! thanks!

Link to post
Share on other sites
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...