I am building an RFID reader that reads tags, notes the time, and sends a message when it happens.
Here are the symptoms of my problem. Whenever I read a tag for the first time, the code works correctly and outputs:
Message Contents:
Tag ID Is: 39fl7412
Read At: Apr 01 2012 12:10:20
However, when I read it again I get:
Tag ID Is: 39fl7412
Read At: uy"null" 01 2012 12:10:20 (Where "null" is the null character, not the text)
And the third time:
Tag ID Is: 39fl7412
Read At: uy followed by 5 null characters, then part of the tag Id "fl7412" then 6 null characters, then 01.
After the third read, it's the same as above.
I have tried all kinds of stuff and haven't figured it out. Any help is greatly appreciated.
Here's my code:
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
SoftwareSerial rfid(12,13);
// Create an RTC instance, using the chip select pin it's connected to
RTC_DS3234 RTC(8);
//RTC time buffer variables
const int len = 32;
static char buf[len];
//RFID variables
int flag = 0;
int Tag[11];
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);
void setup()
{
Serial.begin(9600);
Serial.println("Starting...");
//Initialize RTC
SPI.begin();
RTC.begin();
//Initialize RFID
rfid.begin(19200);
delay(10);
}
void loop()
{
read_serial();
}
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 parse()
{
while(rfid.available())
{
if(rfid.read() == 255)
{
for(int i=1;i<11;i++)
{
Tag[i]= rfid.read();
}
}
}
}
void set_flag()
{
if(Tag[2] == 6){
flag++;
}
if(Tag[2] == 2){
flag = 0;
}
}
void print_serial()
{
if(flag == 1)
{
DateTime now = RTC.now();
String time = now.toString(buf,len);
delay(100);
String TagID = "";
char buffer[3];
for(int index = 8; index>4; index--)
{
itoa(Tag[index], buffer,16);
TagID += buffer;
}
send_message(TagID, time);
}
}
void send_message(String ID, String timestamp)
{
Serial.println("Message Contents:");
Serial.print("Tag ID Is: ");
Serial.println(ID);
Serial.print("Tag Read At: ");
Serial.println(timestamp);
}