Home assistant, mqtt, arduino wemos D1 dosnt work

Hello all

i am working on a setup home assistant > mqtt > arduino D1> Led output
I have already tried many examples and read forums but unfortunately no idea where it is going wrong.

my arduino indicates that it is connected to the wifi and the MQTT when i look in node red also everything seems to be oke what am i missing?

Arduino code:

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

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

#define MQTT_SERVER "192.168.X.XX" 
const char* ssid = "myID";
const char* password = "myPW";

int SwitchedPin = D1;
String switch1;
String strTopic;
String strPayload;


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

void setup() {
  

  pinMode(SwitchedPin, OUTPUT); 
  digitalWrite(SwitchedPin, LOW);

  ArduinoOTA.setHostname("My Arduino WEMO");
  ArduinoOTA.begin(); // OTA initialization

  //start the serial line for debugging
  Serial.begin(115200);
  delay(100);

  //start wifi subsystem
  WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server
  reconnect();

  //wait a bit before starting the main loop
      delay(2000);
}


void loop(){

  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {reconnect();}

  //maintain MQTT connection
  client.loop();

  //MUST delay to allow ESP8266 WIFI functions to run
  delay(10); 
  ArduinoOTA.handle();
}

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  strTopic = String((char*)topic);
  if(strTopic == "ha/led")
    {
    switch1 = String((char*)payload);
    if(switch1 == "ON")
      {
        Serial.println("ON");
        digitalWrite(SwitchedPin, HIGH);
      }
    else
      {
        Serial.println("OFF");
        digitalWrite(SwitchedPin, LOW);
      }
    }
}

 


void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
    Serial.print("Connecting to ");
    Serial.println(ssid);

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    //print out some more debug once connected
    Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if(WiFi.status() == WL_CONNECTED){
  // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      // Generate client name based on MAC address and last 8 bits of microsecond counter
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      WiFi.macAddress(mac);
      clientName += macToStr(mac);

      if (client.connect((char*) clientName.c_str())) { 
        Serial.print("\tMQTT Connected");
      }

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}
    }
  }
}

//generate unique name from MAC addr
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;
}

Home assistant switch:

switch:
 - platform: mqtt
   name: "LED"    
   command_topic: "ha/led"
   payload_on: "ON"
   payload_off: "OFF"
   optimistic: true
   qos: 0
   retain: true

mqtt:
    broker: 192.168.X.XX

i hope someone can help me

thanx