failed to combine 2 code nodemcuv3

Hello All. I’m working on a project on Nodemcuv3. the project uses Adafruit IO MQTT Library and ESP8266 IRRemote Library. here is my code-

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>


uint16_t RECV_PIN = D5;
uint16_t L1 = D6;
uint16_t L2 = D7;



#define WLAN_SSID       "xxxxxxxxxxxxxxxxxxxxxx"
#define WLAN_PASS       "xxxxxxxxxxxxxxxxxxxxxxxxxx"
#define AIO_SERVER      "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define AIO_SERVERPORT  1883                   
#define AIO_USERNAME    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define AIO_KEY         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

Adafruit_MQTT_Subscribe L1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/l1");
Adafruit_MQTT_Subscribe L2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/l2");


IRrecv irrecv(RECV_PIN);
decode_results results;

void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);
  irrecv.enableIRIn();
  pinMode(L1,OUTPUT);
  pinMode(L2,OUTPUT);
  Serial.println(F("starting..........."));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

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

  mqtt.subscribe(&l1);
  mqtt.subscribe(&l2);
}

uint32_t x=0;

void loop() {
  IRremote();
  MQTT();
}

void IRremote() {
  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    if (results.value == 0xJJ89QQ) {
      digitalWrite(L1,HIGH);
    }
    if (results.value == 0xJJ90QQ) {
      digitalWrite(L1,LOW);
    }
    if (results.value == 0xFFFCC5) {
      digitalWrite(L2,HIGH);
    }
    if (results.value == 0xFFFCC4) {
      digitalWrite(L2,LOW);
    }
    irrecv.resume();
  }
}
void MQTT() {
  MQTT_connect();
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &L1) {
      Serial.print(F("Got_L1: "));
      Serial.println((char *)L1.lastread);
      uint16_t num = atoi((char *)L1.lastread);
      digitalWrite(L1,num);
    }

    if (subscription == &L2) {
      Serial.print(F("Got_L2: "));
      Serial.println((char *)L2.lastread);
      uint16_t num = atoi((char *)L2.lastread);
      digitalWrite(L2,num);
    }
  }
  


  if(! mqtt.ping()) {
    mqtt.disconnect();
  }}
void MQTT_connect() {
  int8_t ret;

  
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { 
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(50);  
       retries--;
       if (retries == 0) {
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

Now the problems are-
1.code is running ok with 0 error.but whenever i try to trigger L1/L2 via IR remote, it responses very lately,nearly 3-5 secends.
2.my target is to make a backup/ redundancy when there is no internet. if there is no internet, i want to trigger the I/O pins via IR remote.whenever i disconnect wifi from nodemcu,the IR remote stops working.

it there a glitch in a code? can anyone correct/solve it?
Thanks :slight_smile: