   Serial.println(year());
 }
 
-void printDigits(int digits)
-{
-  Serial.print(":");
-  if (digits < 10)
-    Serial.print('0');
-  Serial.print(digits);
+void wifi_connect()
+{
+  if (WiFi.status() != WL_CONNECTED)
+  {
+    Serial.println();
+    Serial.print("[WIFI] Connecting to ");
+    Serial.println(WIFI_SSID);
+
+    delay(10);
+    WiFi.mode(WIFI_STA);
+    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
+
+    int Attempt = 0;
+    while (WiFi.status() != WL_CONNECTED)
+    {
+      Serial.print(". ");
+      Serial.print(Attempt);
+      delay(100);
+      Attempt++;
+      if (Attempt == 150)
+      {
+        Serial.println();
+        Serial.println("[WIFI] Could not connect to WIFI, restarting...");
+        Serial.flush();
+        ESP.restart();
+        delay(200);
+      }
+    }
+
+    Serial.println();
+    Serial.print("[WIFI] connected");
+    Serial.print(" --> IP address: ");
+    Serial.println(WiFi.localIP());
+  }
+}
+
+String macToStr(const uint8_t* mac)
+{
+  String result;
+  for (int i = 0; i < 6; ++i)
+  {
+    result += String(mac[i], 16);
+    if (i < 5)
+      result += ':';
+  }
+  return result;
+}
+
+void setup()
+{
+  Serial.begin(115200);
+  Serial.println();
+  Serial.println("Starting....... ");
+
+  ir_data.ac_mode          = 0;
+  ir_data.ac_temp          = AC_DEFAULT_TEMP;
+  ir_data.ac_flow          = AC_DEFAULT_FLOW;
+  ir_data.haveData         = false;
+  ir_data.ac_presence_mode = 0;
+
+  lgWhisen.setActype(AC_CONF_TYPE);
+  lgWhisen.setHeating(AC_CONF_HEATING);
+  lgWhisen.setTemp(ir_data.ac_temp);
+  lgWhisen.setFlow(ir_data.ac_flow);
+  lgWhisen.setIrpin(IR_TX_PIN);
+
+  pinMode(PIR_INT, INPUT);
+  bpir_isr = false;
+  attachInterrupt(PIR_INT, pir_isr, RISING);
+
+  wifi_connect();
+
+  clientName += "esp8266-";
+  uint8_t mac[6];
+  WiFi.macAddress(mac);
+  clientName += macToStr(mac);
+  clientName += "-";
+  clientName += String(micros() & 0xff, 16);
+
+  configTime(9 * 3600, 0, "pool.ntp.org", "time.nist.gov");
+
+  // ntp update
+  udp.begin(localPort);
+  if (timeStatus() == timeNotSet)
+  {
+    Serial.println("[NTP] get ntp time");
+    setSyncProvider(getNtpTime);
+    delay(500);
+  }
+
+  // start with ac power down
+  lgWhisen.power_down();
+
+  ArduinoOTA_config();
+
+  reconnect();
+  lastReconnectAttempt = 0;
+  startMills = millis();
+}
+
+void loop()
+{
+  if (bpir_isr)
+  {
+    Serial.println("[PIR] ---> pir detected");
+    lastpirMillis = millis();
+    bpir_isr = false;
+  }
+
+  if ((millis() - lastpirMillis < MAX_PIR_TIME) && ( lastpirMillis != 0))
+  {
+    bpresence = true;
+  }
+  else
+  {
+    bpresence = false;
+  }  
+
+  if (!bpresence && ir_data.ac_presence_mode == 1)
+  {
+    Serial.println("[IR] -----> AC Power Down");
+    lgWhisen.power_down();
+    ir_data.ac_presence_mode = 0;
+  }
+
+  if (bpresence && ir_data.ac_presence_mode == 0 && ir_data.ac_mode == 1)
+  {
+    ir_data.haveData = true;
+  }
+
+  if (now() != prevDisplay)
+  {
+    prevDisplay = now();
+    if (timeStatus() == timeSet)
+    {
+      digitalClockDisplay();
+    }
+    else
+    {
+      Serial.println("[TIME] time not set");
+    }
+  }
+
+  if (WiFi.status() == WL_CONNECTED)
+  {
+    if (!client.connected())
+    {
+      long now = millis();
+      if (now - lastReconnectAttempt > 5000)
+      {
+        lastReconnectAttempt = now;
+        if (reconnect())
+        {
+          lastReconnectAttempt = 0;
+        }
+      }
+    }
+    else
+    {
+      // ac change, ir tx
+      if (ir_data.haveData)
+      {
+        lgWhisen.setTemp(ir_data.ac_temp);
+        lgWhisen.setFlow(ir_data.ac_flow);
+
+        switch (ir_data.ac_mode)
+        {
+          // ac power down
+          case 0:
+            Serial.println("[IR] -----> AC Power Down");
+            lgWhisen.power_down();
+            break;
+
+          // ac on
+          case 1:
+            Serial.println("[IR] -----> AC Power On");
+            lgWhisen.activate();
+            break;
+
+          default:
+            break;
+        }
+        ir_data.ac_presence_mode = ir_data.ac_mode;
+        ir_data.haveData = false;
+      }
+
+      if ((millis() - startMills) > REPORT_INTERVAL) 
+      {
+        sendCheck();
+        startMills = millis();
+      }
+      
+      client.loop();
+    }
+  }
+  else
+  {
+    wifi_connect();
+  }
 }
 
 // end of file

  {
    if (root.containsKey("ac_temp"))
    {
      ir_data.ac_temp = root["ac_temp"];
    }
    /*
        if (root.containsKey("ac_flow"))
        {
          ir_data.ac_flow = root["ac_flow"];
        }
    */
    if (ir_data.ac_mode == 1 && bpresence)
    {
      ir_data.haveData = true;
    }
  }
}

  return client.connected();
}

void ArduinoOTA_config()
{
  //OTA
  // Port defaults to 8266
  ArduinoOTA.setPort(8266);
  ArduinoOTA.setHostname("esp-irbedroom");
  ArduinoOTA.setPassword(OTA_PASSWORD);
  ArduinoOTA.onStart([]()
  {
    //sendUdpSyslog("ArduinoOTA Start");
  });
  ArduinoOTA.onEnd([]()
  {
    //sendUdpSyslog("ArduinoOTA End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
  {
    //syslogPayload = "Progress: ";
    //syslogPayload += (progress / (total / 100));
    //sendUdpSyslog(syslogPayload);
  });
  ArduinoOTA.onError([](ota_error_t error)
  {
    //ESP.restart();
    if (error == OTA_AUTH_ERROR) abort();
    else if (error == OTA_BEGIN_ERROR) abort();
    else if (error == OTA_CONNECT_ERROR) abort();
    else if (error == OTA_RECEIVE_ERROR) abort();
    else if (error == OTA_END_ERROR) abort();
  });

  ArduinoOTA.begin();
}

/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];

void sendNTPpacket(IPAddress & address)
{
