   {
     if (root.containsKey("ac_temp"))
     {
-      ir_data.ac_temp = root["ac_temp"];
-    }
-    /*
-        if (root.containsKey("ac_flow"))
+     if (ir_data.ac_temp != root["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.ac_flow = root["ac_flow"];
+            ir_data.haveData = true;
         }
-    */
-    if (ir_data.ac_mode == 1 && bpresence)
-    {
-      ir_data.haveData = true;
+      }
     }
   }
 }

  return client.connected();
}

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());
  }
}

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();

  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();
  }
}


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;
}

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

time_t getNtpTime()
{
  while (udp.parsePacket() > 0) ;
  sendNTPpacket(mqtt_server);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 2500)
  {
    int size = udp.parsePacket();
    if (size >= NTP_PACKET_SIZE)
    {
      udp.read(packetBuffer, NTP_PACKET_SIZE);
      unsigned long secsSince1900;
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0;
}

void sendNTPpacket(IPAddress & address)
{

  Serial.println(year());
}

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
