Hello, I am implementing an RFID reader that receives a tag ID via text message and compares it to a tag that enters its field.
I have been successful in reading the tag and printing the ID to the serial window. Additionally, because I am comparing an incoming string and the tag id is numeric I have converted the ID into a string of chars using itoa(). When I try to use string.equals(string2) on this new string, I'm not getting the results. Even though when I user Serial.print() on the new string it's identical to the incoming "message".
One more thing: the length of the new string (called IDofTag in my code) is 8. The string is: "39fldf22" which implies that there is a null character at the end. How do I remove it or can I do: string.equals("39fldf22\0")? Everything I'm talking about is in the print serial function.
Thanks! My code:
#include <SoftwareSerial.h>
#include <SPI.h>
SoftwareSerial rfid(12,13);
//RFID variables
int flag = 0;
int Tag[11];
//String TagID = "";
String IDofTag = ""; //Global version of TagID
String message = "39fldf22";
//void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);
//Time Variables
const int cs=8; //chip select
void setup()
{
Serial.begin(9600);
Serial.println("Starting...");
//Initialize Time
RTC_init();
//day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
SetTimeDate(28,3,12,8,57,0);
//Initialize RFID
rfid.begin(19200);
delay(10);
}
void loop()
{
read_serial();
}
int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
//set control register
digitalWrite(cs, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(cs, HIGH);
delay(10);
}
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);
digitalWrite(cs, HIGH);
}
}
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(cs, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);
digitalWrite(cs, HIGH);
int a=n & B00001111;
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}
void halt()
{
//Halt tag
//rfid.print(255, BYTE);
rfid.write(255);
//rfid.print(0, BYTE);
int i = 0;
rfid.write(i);
//rfid.print(1, BYTE);
rfid.write(1);
//rfid.print(147, BYTE);
rfid.write(147);
//rfid.print(148, BYTE);
rfid.write(148);
}
void parse()
{
while(rfid.available())
{
if(rfid.read() == 255)
{
for(int i=1;i<11;i++)
{
Tag[i]= rfid.read();
}
}
}
}
void print_serial()
{
if(flag == 1)
{
//print to serial port
Serial.print(Tag[8], HEX);
Serial.print(Tag[7], HEX);
Serial.print(Tag[6], HEX);
Serial.print(Tag[5], HEX);
delay(10);
Serial.println(ReadTimeDate());
String TagID = "";
char buffer[1];
for(int index = 8; index>4; index--)
{
itoa(Tag[index], buffer,16);
TagID = TagID += buffer;
}
IDofTag = TagID; //Globalize TagID
Serial.println(IDofTag.length());
if(IDofTag.equals(message))
{
Serial.println("Entered IF loop");
}
Serial.println(IDofTag);
}
}
void read_serial()
{
seek();
delay(10);
parse();
set_flag();
print_serial();
delay(100);
}
void seek()
{
rfid.write(255);
int i = 0;
rfid.write(i);
rfid.write(1);
rfid.write(130);
rfid.write(131);
delay(10);
}
void set_flag()
{
if(Tag[2] == 6){
flag++;
}
if(Tag[2] == 2){
flag = 0;
}
}