esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet

My target is to send temperature-humidity sensor data to cloud using MQTT protocol.
I am using Arduino mega, esp8266 wifi model and groove temperature humidity sensor.

  • Connections for Communication:
  • ESP01 → Arduino
  • Vcc → 3.3V
  • GND → GND
  • TxD → Rx1 (Pin 19)
  • RxD → Tx1 (Pin 18)
  • CH_PD → 3.3V
    My code is :
#include <ESP8266WiFi.h>            //https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi
#include <WiFiClient.h>
#include <PubSubClient.h>           //https://github.com/Imroy/pubsubclient
#include <ArduinoJson.h>            //https://bblanchon.github.io/ArduinoJson/

const char *ssid = "No Country for Free Wi-Fi"; 
const char *password = ""; 

String mqtt_server = "postman.cloudmqtt.com"; 
int mqtt_port =15165;
String mqtt_user = "ksekgfgb";
String mqtt_pass = "7QjnA--PflNc"; 
String device_name = "ardmega";
String group= "agri";
 
String top1,top2,top3,top4;
int mqtt_data_status;
int t,h;
WiFiClient mqtttestclient; 
PubSubClient mqttclient(mqtttestclient);
 

void callback(const MQTT::Publish& pub){
     String payload = pub.payload_string();
     if(String(pub.topic()) == "/home/room1/led1/htu21"){  
               DynamicJsonBuffer  jsonBuffer;
               JsonObject& json = jsonBuffer.parseObject(payload);
                 t = json["temperature"]; 
                 h = json["humidity"]; 
               Serial.print("temperature ");
               Serial.println(t);
               Serial.print("humidity ");
               Serial.println(h);
        }
}
void mqtt_connect(){
     mqttclient.set_server(mqtt_server,mqtt_port);
     if (mqttclient.connect(MQTT::Connect(device_name).set_auth(mqtt_user.c_str(), mqtt_pass.c_str()))) {
         mqttclient.set_callback(callback);
         Serial.println("Connected to MQTT server");
         mqttclient.subscribe("/home/#");
        } 
}
 
void setup(void) {
     Serial.begin(115200);
  
     
     top3 += "/home/"+group+ "/"+device_name+"/";
     top2 += "/home/"+group+"/";
     top1 += "/home/";

     Serial.print("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
           }
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
     mqtt_connect();
}

void loop(void){
     if (mqttclient.connected()){
         mqttclient.loop();
         }
}

#############
Now there is no compilation error but while uploading following problems are faced :
Sketch uses 282908 bytes (56%) of program storage space. Maximum is 499696 bytes.
Global variables use 27284 bytes (33%) of dynamic memory, leaving 54636 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.6
2.6
esptool.py v2.6
Serial port COM10
Connecting……____Traceback (most recent call last):
File “C:\Users\SOURAV KAIBARTYA\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.2/tools/upload.py”, line 25, in
esptool.main(fakeargs)
File “C:/Users/SOURAV KAIBARTYA/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 2653, in main
esp.connect(args.before)
File “C:/Users/SOURAV KAIBARTYA/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 468, in connect
raise FatalError(‘Failed to connect to %s: %s’ % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

_
please help me out

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.