RFID Reader program randomly sends tag data twice.

Hello,

I have a program that I want it to read a RFID tag and send the data to Carriots. I have the program set up so after a successful card read that it turns off the reader sends some data and delays for 10 seconds before reactivating the reader. The problem is the program seems to often run the while loop a second time sending the RFID data a second time sending a second stream of information to Carriots. I am kind of lost on why this is happening. Here is the code.

#include <SPI.h>
#include <Ethernet.h>
// include the SoftwareSerial library so we can use it to talk to the RFID Reader
#include <SoftwareSerial.h>

#define enablePin  5   // Connects to the RFID's ENABLE pin
#define rxPin      6  // Serial input (connects to the RFID's SOUT pin)
#define txPin      7  // Serial output (unused)

#define BUFSIZE    11  // Size of receive buffer (in bytes) (10-byte unique ID + null character)

#define RFID_START  0x0A  // RFID Reader Start and Stop bytes
#define RFID_STOP   0x0D

// set up a new serial port
SoftwareSerial rfidSerial =  SoftwareSerial(rxPin, txPin);

const int ON = 1;    // Constant to indicate that lights are on
const int OFF = 2;  // Constant to indicate that lights are off 
const String APIKEY = "1475"; // Replace with your Carriots apikey
const String DEVICE = "device key"; // Replace with the id_developer of your device

String passId;
String inVar;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0x34, 0x71, 0x6D, 0x08, 0xA5, 0xE9 };

IPAddress ip(192,168,1,73);       // Your IP Address
IPAddress server(82,223,244,60);  // api.carriots.com IP Address

EthernetClient client; // Initialize the library instance

int outledPin = 3;        // Led pin number
int inledPin = 2; 
int rfread = OFF;       // Current status
int newrfread = OFF;    // New status


// The setup routine runs once when you press reset
void setup() {
 // define pin modes
  pinMode(enablePin, OUTPUT);
  pinMode(rxPin, INPUT);

  digitalWrite(enablePin, HIGH);  // disable RFID Reader
  
  pinMode(outledPin, OUTPUT);        // Initialize the digital pin as an output
    pinMode(inledPin, OUTPUT);        // Initialize the digital pin as an output

  digitalWrite(inledPin, HIGH);  // disable RFID Reader


  Serial.begin(9600);             // Start serial port
    while (!Serial);   // wait until ready
  Serial.println("\n\nParallax RFID Card Reader");
  Serial.println(F("Starting"));
  // set the baud rate for the SoftwareSerial port
  rfidSerial.begin(2400);
  Ethernet.begin(mac);         // Start the Ethernet connection
  
  delay(1000);                    // Give the Ethernet shield a second to initialize
}

// The loop routine runs over and over again forever
void loop() {
 /* 
    When the RFID Reader is active and a valid RFID tag is placed with range of the reader,
    the tag's unique ID will be transmitted as a 12-byte printable ASCII string to the host
    (start byte + ID + stop byte)
    
    For example, for a tag with a valid ID of 0F0184F07A, the following bytes would be sent:
    0x0A, 0x30, 0x46, 0x30, 0x31, 0x38, 0x34, 0x46, 0x30, 0x37, 0x41, 0x0D
    
    We'll receive the ID and convert it to a null-terminated string with no start or stop byte. 
  */   
 digitalWrite(enablePin, LOW);   // enable the RFID Reader
  
  // Wait for a response from the RFID Reader
  // See Arduino readBytesUntil() as an alternative solution to read data from the reader
  char rfidData[BUFSIZE];  // Buffer for incoming data
  char offset = 0;         // Offset into buffer
  rfidData[0] = 0;         // Clear the buffer    
  while(1)
  {
    if (rfidSerial.available() > 0) // If there are any bytes available to read, then the RFID Reader has probably seen a valid tag
    {
      rfidData[offset] = rfidSerial.read();  // Get the byte and store it in our buffer
      if (rfidData[offset] == RFID_START)    // If we receive the start byte from the RFID Reader, then get ready to receive the tag's unique ID
      {
        offset = -1;     // Clear offset (will be incremented back to 0 at the end of the loop)
      }
      else if (rfidData[offset] == RFID_STOP)  // If we receive the stop byte from the RFID Reader, then the tag's entire unique ID has been sent
      {
        rfidData[offset] = 0; // Null terminate the string of bytes we just received
        newrfread = ON; 
 digitalWrite(enablePin, HIGH);   // enable the RFID Reader
        break;                // Break out of the loop
      }
          
      offset++;  // Increment offset into array
      if (offset >= BUFSIZE) offset = 0; // If the incoming data string is longer than our buffer, wrap around to avoid going out-of-bounds
    }
  }

  Serial.println(rfidData);       // The rfidData string should now contain the tag's unique ID with a null termination, so display it on the Serial Monitor
  Serial.flush();                 // Wait for all bytes to be transmitted to the Serial Monitor

if (newrfread == ON) {
  if (strcmp(rfidData,"0400121C69")==0){
    passId = "Pass1"; 
  }
  else if (rfidData == "35021DD169") {
    passId = "Pass2"; 
  }
  
  if (digitalRead(inledPin) == HIGH){
    digitalWrite(inledPin, LOW); 
      digitalWrite(outledPin, HIGH); 
            inVar = "Out"; 
      delay(500);
}
else {
    digitalWrite(inledPin, HIGH); 
      digitalWrite(outledPin, LOW);  
      inVar = "In";
      delay(500);
}
  newrfread = OFF; 
  //Serial.println("check variable"); 
  //Serial.println(passId); 
  delay(10000);
  sendStream();
  
}

  //if (weight != newWeight) {        // Check if we have a change in status
    //Serial.println(F("Send Stream"));
    //weight = newWeight;             // Status update and send stream
    //sendStream();
 // }

  

  // If there's incoming data from the net connection, send it out the serial port
  // This is for debugging purposes only
  //while (client.available()) {
    //char c = client.read();
    //Serial.print(c);
  //}

  //if (!client.connected()) {
    //  client.stop();
  //}
}

// Send stream to Carriots
void sendStream()
{
  String txt = passId;          // Text to send
  //String inVar = "In";

  //Serial.println(txt);      // For debugging purpose only

  if (client.connect(server, 80)) {   // If there's a successful connection
    Serial.println(F("connected"));
    // Build the data field
    String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"Pass\":\""+txt+"\",\"IN\":\""+inVar+"\"}}";

    // Make a HTTP request
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.carriots.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-Carriots");
    client.println("Content-Type: application/json");
    client.print("carriots.apikey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");
    int thisLength = json.length();
    client.println(thisLength);
    client.println("Connection: close");
    client.println();

    client.println(json);
        //Serial.println(json);

  }
  else {
    // If you didn't get a connection to the server:
    Serial.println(F("connection failed"));
  }


}

Ok I figured it out, the RFID reader is continuing so send serial data no matter how long I wait from a single card read. I added a little loop to wait till the reader stops sending data before continuing on with my program like this.

while (rfidSerial.available() > 0){
  //Sit and do nothing and wait for reader to be empyty
  rfidSerial.read();
  Serial.println(i);
  i++;
  delay(100); 
}

That was completely unnecessary. You read a tag. You might have read a new tag - this tag is not the same as the last tag - or you might have read the same tag again - this tag is the same as the last tag. It isn't THAT difficult to do something only when this tag not equal that tag.