#include <SPI.h>
#include <MFRC522.h>
#include <Ethernet.h>
#define RST_PIN         9         
#define SS_1_PIN        7         
#define SS_2_PIN        8            // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
#define NR_OF_READERS   2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();        // Init SPI bus
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
  }
  Ethernet.begin(mac, ip);
  server.begin();
}
void loop() {
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
   EthernetClient client = server.available();
    if (client) {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) {
         client.println("<!DOCTYPE HTML><html><head>");
         client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");    
         client.println("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "); 
         //client.println("<meta http-equiv=\"refresh\" content=\"3\">");
         client.print(F("Reader "));
         client.print(reader);
         client.print(F(": Card UID:"));
         dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
         mfrc522[reader].PICC_HaltA();
         mfrc522[reader].PCD_StopCrypto1();
         client.println("</body></html>");
         break;
        }
        if (c == '\n') {
         currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }   
    delay(1); 
    client.stop();
    Serial.println("client disconnected");
  } 
  }
}
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}
