MQTT disconnects after http.end()

Scenario: Existing project: read soft serial then do http post New project: MQTT player from other (MrDIY / MrDIY Audio Notifier · GitLab)

Objective: Combine together (Result: successful )

Issue: When read serial do http post, after http.end(). MQTT client disconnect. It taks above 10 secs to reconnect.

The only way I found is just a workaround to add ESP.restart(); after each http post. But that’s stupid. I have spend more than 10 hours to fix this issue. Any one can help. Apperaciate and great thanks.

Eg code:

void loop() {
  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();

  if (mySerial.available() > 0)  //Checks is there any data in buffer
  {
    receive_data = String(receive_data) + String(char(mySerial.read()));
    //Serial.println(receive_data);

    if (String(receive_data).indexOf("lighton") >= 0) {
      Serial.println("lighton");
      receive_data = "";
      http.begin(client, switchon);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", auth);
      int httpCode = http.POST("{\"entity_id\": \"switch.sonoff_guest_room\"}");
      Serial.printf("[http] GET... code: %d\n", httpCode);
      http.end();
      //ESP.restart(); 
    }
}

Issue resolved by define HTTPClient http; in the loop instead of define in global. MQTT client still will be disconnect after http.end but it will reconnect in 1 sec.