   }
   // you're connected now, so print out the data:
   Serial.println("DEBUG :: You're connected to the network");
-  printWifiData();
+  //printWifiData();
 }
 
 // handles message arrived on subscribed topic(s)
-void callback(char* topic, byte* payload, unsigned int length) {
+void callbackFunction(char* topic, byte* payload, unsigned int length) {
+  Serial.println("---------------BOOM!!!!!!!!!!!!!!!!!!--------------------- : ");
   //  arduino-rfid-validation
   String resp = String((char*)payload);
   Serial.println("Payload: " + resp);
-  Serial.print("---------------BOOM!!!!!!!!!!!!!!!!!!--------------------- : ");
+  
   if (resp == "Valid") {
     digitalWrite(2, HIGH);
   } else {
     digitalWrite(3, HIGH);
   }
-  delay(1000);
+  delay(3000);
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
 }
 
+void ShowReaderDetails() {
+	// Get the MFRC522 software version
+	byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
+	Serial.print("MFRC522 Software Version: 0x");
+	Serial.print(v, HEX);
+	if (v == 0x91)
+		Serial.print(" = v1.0");
+	else if (v == 0x92)
+		Serial.print(" = v2.0");
+	else
+		Serial.print(" (unknown)");
+	Serial.println("");
+	// When 0x00 or 0xFF is returned, communication probably failed
+	if ((v == 0x00) || (v == 0xFF)) {
+		Serial.println("WARNING: Communication failure, is the MFRC522 properly connected?");
+	}
+}
+
+//void listWifiNetworks() {
+//  // scan for nearby networks:
+//  Serial.println("** Scan Networks **");
+//  byte numSsid = WiFi.scanNetworks();
+//
+//  // print the list of networks seen:
+//  Serial.print("number of available networks:");
+//  Serial.println(numSsid);
+//
+//  // print the network number and name for each network found:
+//  for (int thisNet = 0; thisNet<numSsid; thisNet++) {
+//    Serial.print(thisNet);
+//    Serial.print(") ");
+//    Serial.print(WiFi.SSID(thisNet));
+//    Serial.print("\tSignal: ");
+//    Serial.print(WiFi.RSSI(thisNet));
+//    Serial.print(" dBm");
+//    Serial.print("\tEncryption: ");
+//    Serial.println(WiFi.encryptionType(thisNet));
+//  }
+//}
+
+
+//void printWifiData() {
+//  // print your WiFi shield's IP address:
+//  IPAddress ip = WiFi.localIP();
+//  Serial.print("DEBUG :: IP Address: ");
+//  Serial.println(ip);
+//  // print MAC address:
+//  byte mac[6];
+//  WiFi.macAddress(mac);
+//  Serial.print("DEBUG :: MAC address: ");
+//  Serial.print(mac[5], HEX);
+//  Serial.print(":");
+//  Serial.print(mac[4], HEX);
+//  Serial.print(":");
+//  Serial.print(mac[3], HEX);
+//  Serial.print(":");
+//  Serial.print(mac[2], HEX);
+//  Serial.print(":");
+//  Serial.print(mac[1], HEX);
+//  Serial.print(":");
+//  Serial.println(mac[0], HEX);
+//    // print the SSID of the network you're attached to:
+//  Serial.print("DEBUG :: SSID: ");
+//  Serial.println(WiFi.SSID());
+//  // print the received signal strength:
+//  long rssi = WiFi.RSSI();
+//  Serial.print("DEBUG :: Signal strength (RSSI):");
+//  Serial.println(rssi);
+//  // print the encryption type:
+//  byte encryption = WiFi.encryptionType();
+//  Serial.print("DEBUG :: Encryption Type:");
+//  Serial.println(encryption, HEX);
+//}

}

void loop() {
  Serial.println("DEBUG :: loop()");
  float tempVar = readTemperature();
  float humVar = getDHT11HumidityReadings();
  char temp[10];
  char humidity[10];
  dtostrf(tempVar, 2, 2, temp);
  dtostrf(humVar, 2, 2, humidity);
  Serial.print("DEBUG :: Temp value return is : ");
  Serial.println(temp);
  Serial.print("DEBUG :: Humidity value return is : ");
  Serial.println(humidity);
  publishTempHumMsg(temp, humidity);
  checkTiltState();
  String rfid = getRfidUID();
  if (rfid != "") {
    Serial.print("DEBUG :: RFID value return is : ");
    Serial.println(rfid);
    publishRFIDMsg(rfid);
  }
  client.subscribe("arduino-rfid-validation");
  // MQTT client loop processing
  client.loop();
}

String getRfidUID() {
   // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
     Serial.println("PICC_IsNewCardPresent loop...");
     return "";
  }

    // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
     Serial.println("PICC_ReadCardSerial loop...");
     return "";
  }
    // Show some details of the PICC (that is: the tag/card)
  
  String rfidUid = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
    rfidUid += String(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.print("Card UID:");
  Serial.println(rfidUid);
  mfrc522.PICC_HaltA();       // Halt PICC
  return rfidUid;
}

void checkTiltState() {
  tiltState = digitalRead(tiltSensorPin);
  Serial.print("INFO :: [1 -> On, 0 -> Off] :: Tilt State is : ");
  Serial.println(tiltState);
    String tiltMsg = getSensorId();
    tiltMsg.concat(",");
    tiltMsg.concat(tiltState);
//    tiltMsg.concat(",");
//    tiltMsg.concat(time);
    char message_buff[30];
    tiltMsg.toCharArray(message_buff, tiltMsg.length()+1);
    //send tilt state to the tilt queue
    client.publish("arduino-tilt-exchange", message_buff);
}

void connectToMQTT() {

      Serial.println("INFO :: Connected to RabbitMQ");
    } else {
      Serial.println("ERROR :: Failed to Connect to RabbitMQ");
      connectToMQTT();
    }
//    client.subscribe("arduino-rfid-validation");
}

void publishRFIDMsg(String rfid) {
  //client.subscribe("inTopic");
  Serial.print("DEBUG :: Sending RFID event : ");
  Serial.println(rfid);
  //Format for the message to the following SensorId,temperature,humidity
  String rfidMsg = getSensorId();
  rfidMsg.concat(",");
  rfidMsg.concat(rfid);
  char message_buff[rfidMsg.length()+1];
  rfidMsg.toCharArray(message_buff, rfidMsg.length()+1);
//  if (!client.connected()) {
//     Serial.print("INFO :: We lost mqtt connection, reconnecting.... ");
//     connectToMQTT(); 
//  }
  boolean result = client.publish("arduino-rfid", message_buff);
   Serial.print("DEBUG :: arduino-rfid publish outcome : ");
   Serial.println(result);
   if (!result) {
    Serial.print("ERROR :: Retry : ");
    delay(1000);
    result = client.publish("arduino-rfid", message_buff);
    Serial.print("DEBUG :: arduino-rfid publish outcome 2 : ");
    Serial.println(result);
  }
}

void publishTempHumMsg(String temp, String humidity) {
  //Format for the message to the following SensorId,temperature,humidity
  String tempHumMsg = getSensorId();
  tempHumMsg.concat(",");
  tempHumMsg.concat(temp);
  tempHumMsg.concat(",");
  tempHumMsg.concat(humidity);
  char message_buff[tempHumMsg.length()+1];
  tempHumMsg.toCharArray(message_buff, tempHumMsg.length()+1);
  boolean result = client.publish("arduino-weather", message_buff);
  Serial.print("DEBUG :: arduino-weather publish outcome : ");
  Serial.println(result);
  while (!result) {
    Serial.print("ERROR :: Retry : ");
    delay(1000);
    result = client.publish("arduino-weather", message_buff);
    Serial.print("DEBUG :: arduino-weather publish outcome 2 : ");
    Serial.println(result);
  }
}

String getSensorId() {
  return "need-a-sensor-id";
}


float getDHT11HumidityReadings() {
  float h = dht.readHumidity();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h)){// || isnan(t) || isnan(f)) {
    Serial.println("mmmmh!!! should put a retry count of 3 here !!! Failed to read from DHT sensor! looping!!");
    getDHT11HumidityReadings();
  }
  return h;
}

float getTemperature() {
  return dht.readTemperature();
}

void connectToWifi() {
