       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.println("DEBUG :: Sending RFID event : ");
-  Serial.print(rfid);
+  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);
-//  tempHumMsg.concat(",");
-//  tempHumMsg.concat(humidity);
-  char message_buff[40];
+  char message_buff[rfidMsg.length()+1];
   rfidMsg.toCharArray(message_buff, rfidMsg.length()+1);
-  client.publish("arduino-rfid", message_buff);
+//  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) {
-  //client.subscribe("inTopic");
-  Serial.println("DEBUG :: Sending temp ");
-  Serial.println(temp);
   //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[80];
+  char message_buff[tempHumMsg.length()+1];
   tempHumMsg.toCharArray(message_buff, tempHumMsg.length()+1);
-  client.publish("arduino-weather", message_buff);
+  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";
 }
 
-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);
-}
 
 float getDHT11HumidityReadings() {
   float h = dht.readHumidity();
-  // Read temperature as Celsius
-  float t = dht.readTemperature();
-  // Read temperature as Fahrenheit
-  float f = dht.readTemperature(true);
-  
   // Check if any reads failed and exit early (to try again).
-  if (isnan(h) || isnan(t) || isnan(f)) {
+  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();
   }
-
-  // Compute heat index
-  // Must send in temp in Fahrenheit!
-  float hi = dht.computeHeatIndex(f, h);
-
-  Serial.print("Humidity: "); 
-  Serial.print(h);
-  Serial.print(" %\t");
-  Serial.print("Temperature: "); 
-  Serial.print(t);
-  Serial.print(" *C ");
-  Serial.print(f);
-  Serial.print(" *F\t");
-  Serial.print("Heat index: ");
-  Serial.print(hi);
-  Serial.println(" *F");
-  delay(500);
- return h;
-}
-
-float readTemperature() {
-  // read the value on AnalogIn pin 0 and store it in a variable
-  int sensorVal = analogRead(sensorPin);
-  // send the 10-bit sensor value out the serial port
-  Serial.print("DEBUG :: Sensor Value: ");
-  Serial.print(sensorVal);
-  // convert the ADC reading to voltage
-  float voltage = (sensorVal / 1024.0) * 5.0;
-  // Send the voltage level out the Serial port
-  Serial.print(", Volts: ");
-  Serial.print(voltage);
-
-  // convert the voltage to temperature in degrees C the sensor changes 10 mV per degree
-  // the datasheet says there's a 500 mV offset ((volatge - 500mV) times 100)
-  Serial.print(", degrees C: ");
-  float temperature = (voltage - .5) * 100;
-  Serial.println(temperature);
-
-  // if the current temperature is lower than the baseline turn off all LEDs
-  if (temperature < baselineTemp) { // if the temperature rises 2-4 degrees, turn an LED on
-    digitalWrite(2, LOW);
-    digitalWrite(3, LOW);
-    digitalWrite(4, LOW);
-  } else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) {
-    digitalWrite(2, HIGH);
-    digitalWrite(3, LOW);
-    digitalWrite(4, LOW);
-  }  else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) { // if the temperature rises 4-6 degrees, turn a second LED on
-    digitalWrite(2, HIGH);
-    digitalWrite(3, HIGH);
-    digitalWrite(4, LOW);
-  } else if (temperature >= baselineTemp + 6) { // if the temperature rises more than 6 degrees, turn all LEDs on
-    digitalWrite(2, HIGH);
-    digitalWrite(3, HIGH);
-    digitalWrite(4, HIGH);
-  }
-  delay(1000);
-  
-  //TODO return the temperature value from this function
-  return temperature;
+  return h;
+}
+
+float getTemperature() {
+  return dht.readTemperature();
 }
 
 void connectToWifi() {

// room temperature in Celcius
const float baselineTemp = 30.0;

char ssid[] = "NETGEAR67";     //  your network SSID (name)
char pass[] = "huskywater261";    // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//#define MQTT_SERVER "192.168.0.8"
byte mac[]    = {  0x90, 0xA2, 0xDA, 0x0E, 0x07, 0x18 };
byte serverRabbit[] = { 192, 168, 0, 2 };//192.168.0.8
byte ip[]     = { 192, 168, 0, 4 };
char clientName[] = "clientA";
char user[] = "mqtt";
char pwd[] = "mqtt";
int port = 1883;
int tiltState;
//int previousTiltState = 2;

//Time on arduino http://playground.arduino.cc/Code/DateTime


  }
  // you're connected now, so print out the data:
  Serial.println("DEBUG :: You're connected to the network");
  //printWifiData();
}

// handles message arrived on subscribed topic(s)
void callbackFunction(char* topic, byte* payload, unsigned int length) {
  Serial.println("---------------BOOM!!!!!!!!!!!!!!!!!!--------------------- : ");
  //  arduino-rfid-validation
  String resp = String((char*)payload);
  Serial.println("Payload: " + resp);
  
  if (resp == "Valid") {
    digitalWrite(2, HIGH);
  } else {
    digitalWrite(3, HIGH);
  }
  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);
//}
