Hello,
First of all, I don't want anyone to just do the program for me, I enjoy the learning curve, and also I don't want anyone to think that I'm just asking you to write it all for me.
The problem:
I have an application of writing RFID Tags for different objects (sticky tags 13.56 MHz). I'm using an RFID NFC Module I bought on Ebay: http://www.ebay.com/itm/PN532-NFC-RFID-Reader-Writer-Module-Arduino-Compatible-/261278168864?pt=Vintage_Electronics_R2&hash=item3cd5647720
This module connected to an Arduino Nano 168, connects to a PC through a USB connection and sends/receives serial data from an executable file I just finished.
A simple Arduino sketch validated the communications and data transfer between Arduino and PC.
int ledPin = 13;
int ledState = LOW;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
}
void loop() {
String pName = "";
String pModel = ""; // variables not used in this sample sketch but used in the original
String pSerial = ""; // variables not used in this sample sketch but used in the original
String pDestination = ""; // variables not used in this sample sketch but used in the original
if (Serial.available() > 0) {
while(Serial.available() > 0) {
// The length of the data is not random the NFC Module has 4 blocks to write at (for any Classic Mifare card) each block
// can write up to 16 characters of information... 4 blocks * 16 characters = 64 characters in total this example only reads
// the first 16 (just copy paste the while loop after this until the delay and change variable name to read 16 more.
while(pName.length() <= 16) {
pName += char(Serial.read());
// This part just blinks the LED to acknowledge serial data transfer
if (ledState == Low) {
ledState = HIGH;
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
delay (75);
}
// This outputs the received string
Serial.print(pName);
}
}
}
As seen in the comments of this program, the Mifare Classic cards can store up to 4 blocks of information each consisting of up to 16 characters each. 16 characters * 4 blocks, 64 letters in total.
My program was able to start the serial transfer so that 16 characters per block are sent in order and the simple sketch returns the values in the same order.
So data transfer and receiving/acknowledgement is already done, all I want to do now is include my sketch or combine it with the provided (by manufacturer) nfc sketch to write via serial the values I'm going to be writing instead of having to upload a new sketch every time I want to change the values in the RFID cards.
The manufacturer's sketch is the following:
#include "Wire.h"
#include "nfc.h"
NFC_Module nfc;
void setup(void)
{
Serial.begin(9600);
nfc.begin();
Serial.println("MF1S50 Reader Demo From Elechouse!");
uint32_t versiondata = nfc.get_version();
if(!versiondata) {
//Serial.print("Didn't find PN53x board");
while(1);
}
//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);
nfc.SAMConfiguration();
}
void loop(void)
{
u8 buf[32],sta;
sta = nfc.InListPassiveTarget(buf);
if(sta&&buf[0] ==4) {
Serial.print("UUID length" );
Serial.print(buf[0], DEC);
Serial.println();
Serial.print("UUID: ");
nfc.puthex(buf+1,buf[0]);
Serial.println();
u8 key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
u8 blocknum = 4;
sta = nfc.MifareAuthentication(0, blocknum, buf+1, buf[0], key);
if(sta) {
u8 block[16];
Serial.println("Authentication success");
strcpy((char*)block, "Value Block 4");
sta = nfc.MifareWriteBlock(blocknum, block);
if(sta) {
Serial.println("Write block 4 successfully");
}
delay(500);
}
}
}
I thought I had combined it, however I have a few errors I will be putting up soon... The combined program is the following:
#include "Wire.h"
#include "nfc.h"
// Creating an instance of the NFC module to use
NFC_Module nfc;
int ledPin = 13;
int ledState = LOW;
// Getting Comms Ready
void setup() {
Serial.begin(115200); // Baud Rate higher = faster
nfc.begin(); // Startup the nfc module
pinMode(ledPin, OUTPUT);
uint32_t versiondata = nfc.get_version();
if(!versiondata) {
// Didn't find a module
while(1);
// Debugging options (not needed for real serial communications
// 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);
nfc.SAMConfiguration();
}
}
// Main Program
void loop(void) {
// Setting our initial string variables
String pName = "";
String pModel = "";
String pSerial = "";
String pDestination = "";
String pBuffer = "";
u8 buf[32], sta;
// Checking for communications availability
if (Serial.available() > 0) {
// While communications are still active
while(Serial.available() > 0) {
sta = nfc.InListPassiveTarget(buf);
if(sta&&buf[0] == 4) {
// Debugging purposes again
// Serial.print("UUID length");
// Serial.print(bug[0], DEC);
// Serial.println();
// Serial.print("UUID: ");
nfc.puthex(buf+1, buf[0]);
// Serial.println();
u8 key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
u8 blocknum = 4;
sta = nfc.MifareAuthentication(0, blocknum, buf+1, buf[0], key);
char charBuff[16];
if(sta) {
char block[16];
// Debugging
// Serial.println("Authentication success");
while(pName.length() <= 16) {
// Take info and put it into variable
pName += char(Serial.read());
strcpy((char*)block, pName);
sta = nfc.MifareWriteBlock(blocknum, block);
if (sta) {
// Debugging purposes
if (ledState == LOW) {
ledState = HIGH;
// Serial.println("Writting block 4 successful");
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
// Give it a time break
delay(75);
}
}
while(pModel.length() <= 16) {
// Take info and put it in variable
pModel += char(Serial.read());
strcpy((char*)block, pModel);
sta = nfc.MifareWriteBlock(blocknum, block);
if (sta) {
// Debugging purposes
if (ledState == LOW) {
ledState = HIGH;
// Serial.println("Writting block 4 successful");
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
// Give it a time break
delay(75);
}
}
while(pSerial.length() <= 16) {
// Take info and put it in variable
pSerial += char(Serial.read());
strcpy((char*)block, pSerial);
sta = nfc.MifareWriteBlock(blocknum, block);
if (sta) {
// Debugging purposes
if (ledState == LOW) {
ledState = HIGH;
// Serial.println("Writting block 4 successful");
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
// Give it a time break
delay(75);
}
}
while(pDestination.length() <= 16) {
// Take info and put it in variable
pDestination += char(Serial.read());
strcpy((char*)block, pDestination);
sta = nfc.MifareWriteBlock(blocknum, block);
if (sta) {
// Debugging purposes
if (ledState == LOW) {
ledState = HIGH;
// Serial.println("Writting block 4 successful");
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
// Give it a time break
delay(75);
}
}
}
}
}
//Serial.print("pName is: ");
Serial.print(pName);
//Serial.print("pModel is: ");
Serial.print(pModel);
//Serial.print("pSerial is: ");
Serial.print(pSerial);
//Serial.print("pDestination is: ");
Serial.print(pDestination);
}
}