Jump to content
43oh

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


Recommended Posts

  • 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

  • 6 months later...

I'm not detecting either of the two tags supplied with my RFID-RC522 from Banggood (http://www.banggood.com/MIFAIR-RC522-Chip-IC-Card-Induction-Module-RFID-Reader-p-81067.html)

 

I've worked out how to put the library in the right place (under my Energia sketchbook directory Engia/libraries/Mfrc522).

I've checked that spirilis's fix is in usci_isr_handler.c (it was already in place in Energia v14)

I think I've got the right pinouts (I also tried swapping 1.3 and 2.2, which was the setup in the OP):

 

// SDA  - 2.2
// SCK  - 1.5
// Mosi - 1.7
// Miso - 1.6
// IRQ  - NC
// GND  - GND
// RST  - 1.3
// VCC  - VCC
 
I can add debugging serial out so I know the app is starting and looping... but it never says it detects a card. In fact Mfrc522.Request(PICC_REQIDL, str); is returning 2 (which is MI_ERR).
 
I've tried two separate readers, so unless they're both faulty, what am I doing wrong? Is it the pinout? Do I have to change something on the Launchpad itself? Is the Banggood card different in some way?
 
I'm stuck, so any help would be appreciated, thanks.
 
Thanks
Link to post
Share on other sites

 

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();	                        
}

hello i tried your code, but i can not get it to work, i connected it exactly as the one you in your pictures, right now im getting both red and green leds on and nothing changes when i place the cards near the reader, could you help me please!?

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

There is code with very detailed documentation and many examples for these RFID modules here: https://github.com/miguelbalboa/rfid

 

The examples are more complicated than the example here but if you want to see just about everything the module can do I recommend it.  The examples include reading a card, writing to a card, dumping detailed information, changing the card user ID, etc.  The examples and code run without modification except for pin connections with MSP430 LaunchPads.  The pin connections I used are:

 
Pin Connections
==================================       
RFID Module       LaunchPad        
--------------    ----------------
Pin 1  (SDA)      Pin 8  (CS)
Pin 2  (SCK)      Pin 7  (SCK)
Pin 3  (MOSI)     Pin 15 (MOSI)
Pin 4  (MISO)     Pin 14 (MISO)
Pin 5  (IRQ)      Not connected
Pin 6  (GND)      GND
Pin 7  (RST)      Pin 10
Pin 8  (3V3)      3V3

 

Change these lines in the example code if you use my pin mapping above and they should run:

#define RST_PIN       10        
#define SS_PIN        8

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

hello i tried your code, but i can not get it to work, i connected it exactly as the one you in your pictures, right now im getting both red and green leds on and nothing changes when i place the cards near the reader, could you help me please!?

 

Same here :(

 

For some reason these lines crash the MSP430. ledpin1&2 aren't referenced anywhere, so I'm not sure (a) what they're for and (B) why they should crash the MSP430!

        pinMode(ledpin1, OUTPUT);
        pinMode(ledpin2, OUTPUT);
 

If I take them out, and try to print the status returned by status = Mfrc522.Request(PICC_REQIDL, str); I get a space. What's going on?!

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

I've just tested this code again and it works fine on energia 12. On 14 and 16 the char-string conversion creates something odd! If you're not getting a card, but serial is working, then it's either the reader wiring or the reader isn't seeing a compatible card.

Unfortunately there's no way to see if the reader is reading.

I only have this working on a G2335

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

Hi @@redkid,

 

I just checked the version that I am using with Energia v17 and it worked with no problem on the F5529.  I've used it with a number of different LaunchPads and several versions of Energia without a problem.  There is a wiring  schematic in the wiki as well as a description in the code.

 

I suppose there could be differences in the NFC readers, or maybe you have a bad reader, or cards.  But the first thing to do is to check and double check the way you have wired it up.

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

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 printing the status and it was always 2 (MI_ERR), just like tobydjones reported earlier.

 

What worked was grabbing the latest Arduino version as suggested by Fmilburn.

  1. Downloaded the latest version here: https://github.com/miguelbalboa/rfid. Placed that folder in my libraries folder.
  2. When trying to compile, Energia complains that "beginTransaction" and "endTransaction" does not exist in the SPI class. Just open MFRC522.cpp (it's in that folder) and comment out every line with either (I found 8 in total). Now the examples should compile fine.
  3. I changed to the following because I already had those pins hooked up from using the other examples:
    #define SS_PIN 8   // Same as CS pin in the other examples
    #define RST_PIN 10 // Same as NRSTPD pin in the other examples
    

 

Not sure why none of the others were working.. Took me quite a while to figure out! Oh and I am using Energia 16 on OS X and MSP-EXP430G2553 LaunchPad.

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

 

What worked was grabbing the latest Arduino version as suggested by Fmilburn.

  1. Downloaded the latest version here: https://github.com/miguelbalboa/rfid. Placed that folder in my libraries folder.
  2. When trying to compile, Energia complains that "beginTransaction" and "endTransaction" does not exist in the SPI class. Just open MFRC522.cpp (it's in that folder) and comment out every line with either (I found 8 in total). Now the examples should compile fine.

 

beginTransaction() and endTransaction have been added to Arduino with the 1.0.6 release. These functions make sure that only one instance of SPI is able to talk to the same SPI-bus at a time, which otherwise can happen for example from within interrupt handlers. So if you have only one SPI instance then it's certainly no problem to remove the beginTransaction/endTransaction lines from the library code. The better solution (imho) would be to update the Energia SPI library to the latest Arduino standard.

Link to post
Share on other sites

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...