MQTT Relay with Push button

Hello,
I’m trying to build a MQTT relay with push button. I’m using ESP01. The problem is that whenever I push the button, it switches on the relay for 1 or 2 seconds then it switches off it. But from HASSIO I can control relay without misbehavior.
ESP01 pinouts:
GPIO0 is input,(pullup).
GPIO2 is output => connected to a transistor NPN.

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

void callback(char* topic, byte* payload, unsigned int length);


#define MQTT_SERVER "192.168.200.10"
const char* ssid = "Juniper";
const char* password = "1111";
const char *mqtt_user = "homeassistant";
const char *mqtt_password = "testpass";
IPAddress ip(192, 168, 200, 85); 
IPAddress gateway(192, 168, 200, 1); 
IPAddress subnet(255, 255, 255, 0);

const int switchPin1 = 2;
const int buttonPin = 0;

int buttonState = 0;
int lightstate = 0;


char const* switchTopic1 = "/house/switch1/";

WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup() {
  pinMode(buttonPin, INPUT); 
  pinMode(switchPin1, OUTPUT); 
  digitalWrite(switchPin1, LOW);
  WiFi.config(ip, gateway, subnet);  
  WiFi.begin(ssid, password);
  delay(2000);
  attachInterrupt(digitalPinToInterrupt(buttonPin), button, FALLING);

}


void loop(){
  if (!client.connected() && WiFi.status() == 3) {reconnect();}
  client.loop();
  delay(10); 
   }


void button(){
       if (lightstate == 0){
       digitalWrite(switchPin1, HIGH); 
       client.publish("/house/switchConfirm1/", "1");
       delay(50);
       lightstate = 1;
       delay(1000);

     } else if (lightstate == 1){
       digitalWrite(switchPin1, LOW);
       client.publish("/house/switchConfirm1/", "0");
       delay(50);
       lightstate = 0;
       delay(1000);
     }

}




void callback(char* topic, byte* payload, unsigned int length) {


  String topicStr = topic; 
   if (topicStr == "/house/switch1/") 
    {
     if(payload[0] == '1'){
       digitalWrite(switchPin1, HIGH);
       client.publish("/house/switchConfirm1/", "1");
       lightstate = 1;
       delay(1000);
       }
     else if (payload[0] == '0'){
       digitalWrite(switchPin1, LOW);
       client.publish("/house/switchConfirm1/", "0");
       lightstate = 0;
       delay(1000);
      }
    }
}




void reconnect() {

  if(WiFi.status() != WL_CONNECTED){
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
 }

  if(WiFi.status() == WL_CONNECTED){
    while (!client.connected()) {
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      WiFi.macAddress(mac);
      clientName += macToStr(mac);
      if (client.connect((char*) clientName.c_str(),mqtt_user, mqtt_password)) { 
        client.subscribe(switchTopic1);

      }

      else{}
    }
  }
}

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

Any idea?

  attachInterrupt(digitalPinToInterrupt(buttonPin), button, FALLING);

Interrupt handlers are supposed to be fast - lightening fast.

Everything done in an interrupt handler MUST work with interrupts enabled.

       client.publish("/house/switchConfirm1/", "1");

I seriously doubt it.

       delay(50);

NO!

       delay(1000);

Hell, no!

Why are you using an interrupt to read the state of a switch pressed by a human? That is, in 99.9% of the cases, wrong.

Thanks,
then what must I use?

With this code, it switches on the relay only:

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

void callback(char* topic, byte* payload, unsigned int length);


#define MQTT_SERVER "192.168.200.10"
const char* ssid = "";
const char* password = "";
const char *mqtt_user = "";
const char *mqtt_password = "";
IPAddress ip(192, 168, 200, 85); 
IPAddress gateway(192, 168, 200, 1); 
IPAddress subnet(255, 255, 255, 0);

const int switchPin1 = 2;
const int buttonPin = 0;

int buttonState = 0;
int lightstate = 0;
volatile byte state = LOW;

char const* switchTopic1 = "/house/switch1/";

WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup() {
//  pinMode(buttonPin, FUNCTION_3);
  pinMode(buttonPin, INPUT); 
  pinMode(switchPin1, OUTPUT); 
  digitalWrite(switchPin1, LOW);
  WiFi.config(ip, gateway, subnet);  
  WiFi.begin(ssid, password);
//  reconnect();
  delay(2000);
}


void loop(){
  if (!client.connected() && WiFi.status() == 3) {reconnect();}
  client.loop();
  delay(10); 
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW);
       if (lightstate == 0){
       digitalWrite(switchPin1, HIGH); 
       client.publish("/house/switchConfirm1/", "1");
       lightstate = 1;
     } else {
       digitalWrite(switchPin1, LOW);
       client.publish("/house/switchConfirm1/", "0");
       lightstate = 0;
     }

}




void callback(char* topic, byte* payload, unsigned int length) {


  String topicStr = topic; 
   if (topicStr == "/house/switch1/") 
    {
     if(payload[0] == '1'){
       digitalWrite(switchPin1, HIGH);
       client.publish("/house/switchConfirm1/", "1");
       lightstate = 1;
       delay(1000);
       }
     else if (payload[0] == '0'){
       digitalWrite(switchPin1, LOW);
       client.publish("/house/switchConfirm1/", "0");
       lightstate = 0;
       delay(1000);
      }
    }
}




void reconnect() {

  if(WiFi.status() != WL_CONNECTED){
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
 }

  if(WiFi.status() == WL_CONNECTED){
    while (!client.connected()) {
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      WiFi.macAddress(mac);
      clientName += macToStr(mac);
      if (client.connect((char*) clientName.c_str(),mqtt_user, mqtt_password)) { 
        client.subscribe(switchTopic1);

      }

      else{}
    }
  }
}

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;
}
  if (buttonState == LOW);

If statements that properly end with a semicolon are rarer than unicorns.

  String topicStr = topic;
   if (topicStr == "/house/switch1/")
    {

What is wrong with using strcmp() with topic?

      else{}

An else statement is not required. If there is nothing to do if the if statement does not evaluate to true, leave the else statement out.

then what must I use?

Your brain. Why do you think you need to use an interrupt? Why is responding to a switch press more important that anything else loop() is currently doing?

Thank you for your help, the problem that I have from day one is " if you push the button and keep it for few seconds it switches between on and off" which is obvious. That's why I was thinking maybe with interrupt I can resolve it?!
My idea was " if you push the button, it switches once either On or Off" something like this.

Is there any idea to overcome this issue?

I'm going to do push button with Debounce and millis,,

Did you fix the ; thing that PaulS pointed out?

The ; on this "if"...

if (buttonState == LOW);

... means that the "if" is immediately ended, and the line below will be honoured regardless of the LOW-ness or HIGH-ness of buttonState.

And when you remove the ; the next part will also need to be in {...}

I would be very inclined to get the button press working in a standalone sketch with no MQTT publishing in the picture to complicate things, so that you can clarify in your mind how buttons should be handled.

Your buttonPin is pinMode-ed as input- do you have an external pullup or pulldown to guarantee its state when it's not pressed?