 //
 long startMills;
 
-void callback(const MQTT::Publish& pub) {
-  
-  Serial.print(" => ");
-  Serial.print(pub.topic());
-  Serial.print(" => ");
-  Serial.print(pub.payload_string());
-  
-  String subPayload = pub.payload_string() ;
-
-  if ( subPayload == "{\"LIGHT\":1}") 
-  {
-       relaystatus = 1 ;
-    
-  } else if ( subPayload == "{\"LIGHT\":0}") 
-  {
-       relaystatus = 0 ;
-  }
-  changelight() ;
-  Serial.println("");
-  Serial.print(" => relaystatus => ");
-  Serial.println(relaystatus);
-}
-
 WiFiClient wifiClient;
-PubSubClient client(wifiClient, server);
+PubSubClient client(server, 1883, callback, wifiClient);
 WiFiUDP udp;
 
-
-void setup() 
-{
-  Serial.begin(38400);
-  Serial.println("DHTxx test!");
-  delay(20);
-  
-  startMills = millis();
-
-  pinMode(pir,INPUT);
-  pinMode(RELAYPIN, OUTPUT);
-  pinMode(TOPBUTTONPIN, INPUT_PULLUP);
-
-  digitalWrite(RELAYPIN, relaystatus);
-
+long lastReconnectAttempt = 0;
+
+void wifi_connect() {
+  // WIFI
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);
 
   WiFi.mode(WIFI_STA);
-  WiFi.begin(ssid, password); 
+  WiFi.begin(ssid, password);
 
   int Attempt = 0;
   while (WiFi.status() != WL_CONNECTED) {

  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  //
  Serial.println("Starting UDP");
  udp.begin(localPort);

  }

  attachInterrupt(13, motion_detection, RISING);
  attachInterrupt(5, run_lightcmd, CHANGE);

  sensors.begin();
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");

  // set the resolution to 9 bit
  sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
