 	  
 	
 	// We need to report to the MQTT Server
-	if (mqttconnect() != 0) {
-	  Serial.println("Unable to connect to MQTT Server");
-	} else {
+	while (!mqtttimeout) {
+	  if(mqttconnect() != 0){
+		  Serial.println("Unable to connect to MQTT Server " + mqttfailcount + " tries");
+		  delay(1000);
+	      mqttfailcount++;
+	  } else {
+		  Serial.println("MQTT Successfully Connected");
+		  mqtttimeout = true;
+		  mqttsuccess = true;
+	  }
+	  
+	  if(mqttfailcount >= 30 ){
+		  Serial.println("Giving up MQTT connection after "+ mqttfailcount + " tries" );
+		  mqtttimeout = true;
+	  }
+	  
+	  
+	}
+	
+	if(mqttsuccess){
 	  // Connect to MQTT Server
 	  //client.connect(mqttclientname.c_str(), mqttusername.c_str(), mqttpassword.c_str() );
 	  //Following is original

	  
  


  digitalWrite(REDLED, LOW);
  digitalWrite(GREENLED, LOW);

  if (!configloaded) {
    loadconfig();
  }
  int sleepinterval = 30; // seconds
  bool mqttconnectsuccess = false;
  bool mqtttimeout = false;
  int mqttfailcount = 0;
  
  if ( !trySTAWiFi() ) {
    // STA Failed so load softAP
