Arduino UNO and PN532 RFID/NFC Shield Project

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
zid
 
Posts: 4
Joined: Sun May 18, 2014 8:26 pm

Arduino UNO and PN532 RFID/NFC Shield Project

Post by zid »

Hi Support,

Let me tell you first what I want to do with my project. I am setting up a system that has this arduino and adafruit shield that will read a mifare rfid tag. I am newbie in programming so what I am doing now is to setup arduino and adafruit to read the tag and connected to my laptop which I will pull the data read by the shield. The problem is I am not able to pull the data and store it in my MySQL server. Here's my sample sketch program and my python script that will pull the data whenever there is tag read and update my SQL server.

1. Sketch code:

Code: Select all

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>

#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield

Adafruit_NFCShield_I2C nfc(IRQ, RESET);

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for an ISO14443A Card ...");
}


void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("rfid: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
  }
  delay(1000);
}
2. Laptop Python script:

Code: Select all

#!/usr/bin/python

#from time import sleep
import serial 
import MySQLdb

#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","admin","1234","rfid") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()

device = "/dev/tty.usbmodem1d11" #this will have to be changed to the serial port you are using
try:
  print "Trying...",device 
  arduino = serial.Serial(device, 115200) 

except: 
  print "Failed to connect on",device    

try: 
  data = arduino.readline()  #read the data from the arduino 
  print data
  pieces = data.split("\t")  #split the data by the tab
  #Here we are going to insert the data into the Database
    
  sql = """INSERT INTO arduino(uid,uidlength) VALUES (%s,%s), (pieces[0],piecess[1])"""

  try:
    cursor.execute(sql) 
    dbConn.commit() #commit the insert
    cursor.close()  #close the cursor
  except MySQLdb.IntegrityError:
    print "failed to insert data"
  finally:
    cursor.close()  #close just incase it failed
except:
    print "Failed to get data from Arduino!"

#disconnect to the server
dbConn.close()
3. When I read the tags I am getting this result on my python script.

Trying... /dev/tty.usbmodem1d11
udD

Failed to get data from Arduino!



Please tell me what do I need to add. Appreciate your help!
Last edited by adafruit_support_bill on Mon Jun 23, 2014 6:38 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88160
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino UNO and PN532 RFID/NFC Shield Project

Post by adafruit_support_bill »

Have you verified via the Serial Monitor that the Arduino is sending the data as expected?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”