If the function ReadRFID() worked (in the previous example/test), the the code could look something like the code below.
We setup all the Ethernet and RFID stuff, and keep calling the ReafRFID() function (a delay in between might be desired)
(I noticed another mistake in the previous code: remove the first line with "char server" - it's in there twice, which is wrong)
#include <UIPEthernet.h> // Used for Ethernet
#include <SoftwareSerial.h> // Used for RFID
// ETHERNET
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
// RFID
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
boolean ReadRFID() {
if (rfidSerial.available()) {
char incoming = (char)rfidSerial.read();
if(incoming == 3){
Serial.println("ReadRFID: Incoming Tag");
myTagClean = myTag.substring(1, 13);
myTag = "";
Serial.println(myTagClean);
return true;
}
else{
myTag = String (myTag + incoming);
return false;
}
}
}
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
rfidSerial.begin(9600); // communication speed of ID-12 reader
rfidSerial.println("Starting Staff Attendance Using RFID...");
}
void loop() {
// if RFID read an ID ...
if(ReadRFID()) {
// ... and if we get a connection, send the RFID code to the server:
if (client.connect(server, 80)) {
Serial.println("-> Connected");
// Make a HTTP request:
client.print( "GET /test/add_data.php?");
client.print("myTagClean= ");
client.print(myTagClean); // <--- Maybe let the server decide to do with that number?
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}
else {
// you didn't get a connection to the server:
Serial.println("--> connection failed/n");
}
}
}
Note that I moved the ReadRFID() function to the front, seems required so the compiler knows the function definition properly. This means that it should have failed in the previous example as well,... Like I said: I cannot really test any of this ...
Also note that gateway definition is probably needed for Ethernet to work properly.