How to combine RFID and Ethernet?

Hi there,

I'd like to combine an ethernet shield and a rfid-shield (that work separately).

The Ethernet Code is:

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char serverName[] = "web0.vps6950.alfahosting-vps.de";

EthernetClient client;

int result;

void setup ()
{
  Serial.begin(9600);

  if(Ethernet.begin(mac) ==0)
  {
    Serial.println("DHCP-Konfiguration fehlgeschlagen");

    while(true)
      ;
  }
  delay(1000);

  Serial.println("Verbindung wird hergestellt...");
}

void loop()

{
  if(client.connect(serverName, 80)>0)  {
    Serial.println(F("Die Verbindung wurde erfolgreich hergestellt."));

    // send request    
    client.print(F("GET /index.php?test=hallo HTTP/1.1\r\nHost: "));
    client.println(serverName);
    client.println(F("Connection: close\r\n"));
  }
  else   {
    Serial.println(F("That's an error. That's all we know."));
  }

  // read and display server response
  // the next loop needs a timeout  or it can become an infinite loop
  boolean myValue = false;
  char myString[32];
  int myCount = 0;

  while(client.connected()) {
    while(client.available()) {
      char ch = client.read();

      // if myValue is false and ch is the '<', it is the start of your string
      if(myValue == false && ch == '<') {
        myValue = true;
        // Serial.println(F("myValue start"));
      }

      //Serial.write(ch);

      if(myValue == true && myCount < 31) {
        myString[myCount] = ch;
        myCount++;
        myString[myCount] = 0;
      }

      // if myValue is true and ch is a line feed, that is the end of your string
      if(myValue == true &&  ch == '\n') {
        myValue = false;
        //   Serial.println(F("myValue end"));
        //  Serial.print("myString: [");
        Serial.print(myString);
        //  Serial.print("]");
      }
    }
  }
  // close after the server does.  
  client.stop();

  delay(1000);
}

Here comes the RFID-Code:

/*
RFID Eval 13.56MHz Shield example sketch v10

Aaron Weiss, aaron at sparkfun dot com
OSHW license: http://freedomdefined.org/OSHW

works with 13.56MHz MiFare 1k tags

Based on hardware v13:
D7 -> RFID RX
D8 -> RFID TX
D9 -> XBee TX
D10 -> XBee RX

Note: RFID Reset attached to D13 (aka status LED)

Note: be sure include the SoftwareSerial lib, http://arduiniana.org/libraries/newsoftserial/

Usage: Sketch prints 'Start' and waits for a tag. When a tag is in range, the shield reads the tag,
blinks the 'Found' LED and prints the serial number of the tag to the serial port
and the XBee port.
06/04/2013 - Modified for compatibility with Arudino 1.0. Seb Madgwick.

*/
#include <SoftwareSerial.h>

SoftwareSerial rfid(7, 8);
SoftwareSerial xbee(10, 9);

//Prototypes
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);

//Global var
int flag = 0;
int Str1[11];
tagid= Str1

//INIT
void setup()
{
  Serial.begin(9600);
  Serial.println("Start");

  // set the data rate for the SoftwareSerial ports
  xbee.begin(9600);
  rfid.begin(19200);
  delay(10);
  halt();
}

//MAIN
void loop()
{
  read_serial();
}

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
 //Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    
    delay(100);
    //check_for_notag();
  }
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

And now I have combined the two codes to:

//Ethernet Initialisierung
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char serverName[] = "web0.vps6950.alfahosting-vps.de";
EthernetClient client;

//Ethernet Ende

//RFID Initialisierung

#include <SoftwareSerial.h>
SoftwareSerial rfid(7, 8);
void check_for_notag(void);
void halt(void);
void pare(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);

//RFID Ende

//Globale Variablen
int result; //Ethernet
int flag = 0; //RFID
int Str1[11]; //RFID

//Endde Variablen

void setup ()
{//Setup Ethernet
  Serial.begin(9600);
  if(Ethernet.begin(mac) ==0)
  {
    Serial.println("DHCP-Konfiguration fehlgeschlagen");
    while(true)
      ;
  }
  delay(1000);
  Serial.println("Verbindung wird hergestellt...");
  //Ende Setup Ethernet

  //Setup RFID
  Serial.println("Start");

  // set the data rate for the SoftwareSerial ports
  rfid.begin(19200);
  delay(10);
  halt();

}

void loop()

{
  //RFID-Tag auslesen

  read_serial();

  //Ethernet Abfrage

  //Erst starten, wenn Tag erkannt

  if(rfid.available())
  {
    if(client.connect(serverName, 80)>0)  {
      Serial.println(F("Die Verbindung wurde erfolgreich hergestellt."));

      // send request    
      client.print(F("GET /index.php?test="));
      client.print(Str1[8], HEX);
      client.print(Str1[7], HEX);
      client.print(Str1[6], HEX);
      client.print(Str1[8], HEX);
      client.println(serverName);
      client.println(F("Connection: close\r\n"));
    }
    else   {
      Serial.println(F("That's an error. That's all we know."));
    }

    // read and display server response
    // the next loop needs a timeout  or it can become an infinite loop
    boolean myValue = false;
    char myString[32];
    int myCount = 0;

    while(client.connected()) {
      while(client.available()) {
        char ch = client.read();

        // if myValue is false and ch is the '<', it is the start of your string
        if(myValue == false && ch == '<') {
          myValue = true;
          // Serial.println(F("myValue start"));
        }

        //Serial.write(ch);

        if(myValue == true && myCount < 31) {
          myString[myCount] = ch;
          myCount++;
          myString[myCount] = 0;
        }

        // if myValue is true and ch is a line feed, that is the end of your string
        if(myValue == true &&  ch == '\n') {
          myValue = false;
          //   Serial.println(F("myValue end"));
          //  Serial.print("myString: [");
          Serial.print(myString);
          //  Serial.print("]");
        }
      }
    }
    // close after the server does.  
    client.stop();

    delay(1000);

    //Ende Ethernet Abfrage
  }
}

//Benötigte Zusatzfunktionen für RFID

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
  //Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    delay(100);
    //check_for_notag();
  }
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

The thing I want to do is to first read a RFID-Tag and then send the id to the server.

What's wrong here?

Thank you in advance
arduino2013

What's wrong here?

What does it do that it is not supposed to do? Or, what isn't it doing that it's supposed to do?

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
}

If there is at least one byte available to read, and that byte is 255, read 10 more that probably haven't arrived yet. Yeah, right. NOT!

  read_serial();
  if(rfid.available())

Read all the data that has arrived, and some that hasn't. The, if there is still more, send the data already read to the server. Does that make any sense?

The RFID-Sketch for itself works well and displays me the ID-numbers of the tags on the Serial Monitor. The thing I want to do now is that the ID should be sent via ethernet.

Sorry PaulS I didn't understand what you meant in your answer.

The thing it should do is to first read the RFID-Tag and then send it to the server via ethernet. This is what the Serial Monitor gives me:

Verbindung wird hergestellt...
Start

But unfortunately it doesn't recognize any tag and doesn't send the ID to the server as well.

Where's the problem here?

Thanks

arduino2013:
Sorry PaulS I didn't understand what you meant in your answer.

I think PaulS meant that the logic you're using to read from the rfid input stream is wrong. You can't read a byte before it is available, and your code reads without checking that the data is available.

and your code reads without checking that the data is available.

On top of that, it checks for more RFID data to be available before sending the stuff it already read to the server. That will never happen, because you've already read everything.

So how to fix this?

So how to fix this?

Only read data that has already arrived. Typically, that means storing data in an array until either an end-of-packet marker arrives or until the required number of bytes has arrived (and hoping nothing gets lost).

Then, send data to the server when the end of packet marker arrives, not when there is more data after that.

You can't read a byte before it is available, and your code reads without checking that the data is available.

Does that mean that the problem has something to do with this?:

if(rfid.available())}
  {