 #define MODE_OFF    0  // not sensing light, LED off
 #define MODE_ON     1  // not sensing light, LED on
 #define MODE_SENSE  2  // sensing light, LED controlled by software
-int senseMode = 0;
-char message_buff[100];
 //setup MQTT
 WiFiClient wifiClient;
 //MQTT
-PubSubClient client(serverRabbit, port, callback, wifiClient);
+PubSubClient client(serverRabbit, port, callbackFunction, wifiClient);
 MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
-// Number of known default keys (hard-coded)
-// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
-#define NR_KNOWN_KEYS   8
-// Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
-byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] =  {
-    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
-    {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
-    {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
-    {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
-    {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
-    {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
-    {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
-    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}  // 00 00 00 00 00 00
-};
   
 void setup() {
   // initialize serial and wait for the port to open:
   Serial.begin(9600);
   while (!Serial);
-    
+//    listWifiNetworks();
   // set the LED pins as outputs
   // the for() loop saves some extra coding
-  for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
-    pinMode(pinNumber, OUTPUT);
-    digitalWrite(pinNumber, LOW);
-  }
-  
+  for (int pinNumber = 2; pinNumber < 6; pinNumber++) {
+    if (pinNumber != 4) {
+      pinMode(pinNumber, OUTPUT);
+      digitalWrite(pinNumber, LOW);
+    }
+  }
   connectToWifi();
   connectToMQTT();
-  
   setupDHT();
   SPI.begin();                // Init SPI bus
-   mfrc522.PCD_Init();         // Init MFRC522 card 
-//  setupRFID();
+  mfrc522.PCD_Init();         // Init MFRC522 card 
+  ShowReaderDetails();
+// client.subscribe("arduino-rfid-validation");
 
 }
 

}

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() {
