Esp8266 - multitask

I'm currently working on a project where I'm using an ESP8266 to connect to WiFi and MQTT to control a relay based on GPIO pin status and MQTT messages. However, I'm encountering an issue where sometimes, if there are WiFi connection problems or if WiFi is not available, the relay switching is not instant.

Here's a breakdown of the problem and the code I've tried:

I'm monitoring the input status of GPIO pin D6 via an interrupt. When there's a change in the pin status, I want to turn on or off the relay connected to pin D2 accordingly.

Below is the code I've tried, but it doesn't work

#include <ESP8266WiFi.h>

const char* ssid1 = "wifi1";
const char* password1 = "123456789";
const char* ssid2 = "WIFI2";
const char* password2 = "123456789";

const int interruptPin = D6;
const int ledPin = D2;

volatile bool interruptFlag = false;

void relayTask() {
  pinMode(ledPin, OUTPUT);
  for (;;) {
    if (interruptFlag) {
      if (digitalRead(interruptPin) == HIGH) {
        Serial.println("Interrupt pin is HIGH");
        digitalWrite(ledPin, HIGH);
      } else {
        Serial.println("Interrupt pin is LOW");
        digitalWrite(ledPin, LOW);
      }
      interruptFlag = false;
    }
    delay(10);  // Delay for 10 milliseconds
  }
}

void input_ISR() {
  interruptFlag = true;
}

void connectToWiFi() {
  Serial.println("Connecting to WiFi...");

  // Connect to first WiFi network
  WiFi.begin(ssid1, password1);
  unsigned long startTime = millis();  // Record start time

  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {  // Try for 10 seconds
    delay(1000);  // Delay for 1 second
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to WiFi: " + String(ssid1));
    return; // Exit the function if WiFi is connected successfully
  } else {
    Serial.println("Failed to connect to " + String(ssid1) + ", trying backup...");
  }

  // Connect to backup WiFi network
  WiFi.begin(ssid2, password2);
  startTime = millis();  // Reset start time

  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {  // Try for 10 seconds
    delay(1000);  // Delay for 1 second
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to backup WiFi: " + String(ssid2));
  } else {
    Serial.println("Failed to connect to backup WiFi");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), input_ISR, CHANGE);

  // Start relay task
  TaskHandle_t relayTaskHandle;
  xTaskCreate(relayTask, "RelayTask", 2048, NULL, 1, &relayTaskHandle);

  // Start WiFi connection task
  TaskHandle_t wifiTaskHandle;
  xTaskCreate(connectToWiFi, "WiFiTask", 2048, NULL, 1, &wifiTaskHandle);
}

void loop() {
  // Nothing to do here
}

Can You describe the steps You've taken during the build of the project up to this point?
What parts have been verified okey separately?

Compilation error: 'xTaskCreate' was not declared in this scope
tried xTaskCreatePinnedToCore but same problem
i think they works just on esp32

Any relay control test control done?
Any Wifi connect test code made and verified?
Ant pure MQTT contact code made and verified?

That's just not possible for code that doesn't compile.

Good luck and hope someone takes on Your question. I'm out.