     }
   }
 
-  client.loop();
+  if (WiFi.status() == WL_CONNECTED) {
+    if (!client.connected()) {
+      if  (
+        client.connect(MQTT::Connect((char*) clientName.c_str())
+                       .set_clean_session()
+                       .set_will("status", "down")
+                       .set_keepalive(2))
+      ) {
+        client.publish(hellotopic, "hello from ESP8266 s03");
+        client.set_callback(callback);
+        client.subscribe(subtopic);
+      }
+    }
+
+    if (client.connected())
+      client.loop();
+  }
 
 }
 
 void checkDisplayValue() {
-  if ( sleepmode != o_sleepmode ) 
+  if ( sleepmode != o_sleepmode )
   {
     displaysleepmode(sleepmode);
     o_sleepmode = sleepmode;

    lcd.setCursor(16, 2);
    lcd.print(" ");
    lcd.setCursor(17, 2);
    lcd.print(" ");    
  }

}

void sendmqttMsg(String payload)
{
  if (!client.connected()) {
    if (client.connect((char*) clientName.c_str()))
    {
      Serial.println("Connected to MQTT broker again esp8266/arduino/s03");
      Serial.print("Topic is: ");
