Port disconnects after I upload my sketch

I am also having the same issue, please see the code:

#include <WiFi.h>
#include <HTTPClient.h>
#include <TFT_eSPI.h>       // Include TFT library for display
#include <JPEGDecoder.h>    // JPEG decoder library

// Replace with your network credentials
const char* ssid = "AyushPC";
const char* password = "senecasus";

// ESP32-CAM IP address
String serverName = "http://192.168.34.82/capture"; 

TFT_eSPI tft = TFT_eSPI();  // Create TFT instance

void setup() {
  Serial.begin(115200);
  
  // Initialize the TFT display
  tft.init();
  tft.setRotation(1);  // Adjust the rotation if necessary
  tft.fillScreen(TFT_BLACK);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Send an HTTP GET request to fetch the captured image from ESP32-CAM
    http.begin(serverName);
    int httpCode = http.GET();

    // Check the response
    if (httpCode == HTTP_CODE_OK) {
      WiFiClient *stream = http.getStreamPtr();

      // Decode and render the image using JPEGDecoder
      if (JpegDec.decodeSdFile(stream)) {
        renderJPEG(0, 0);  // Display the image on the TFT at coordinates (0, 0)
      } else {
        Serial.println("JPEG decoding failed!");
      }
    } else {
      Serial.println("Error on HTTP request");
    }

    http.end();
  }

  delay(100);  // Adjust based on the frame rate you want (reduce delay for smoother video)
}

void renderJPEG(int xpos, int ypos) {
  // Render the JPEG image on the TFT
  uint16_t *pImg = JpegDec.pImage;  // Pointer to image pixels

  uint16_t mcu_w = JpegDec.MCUSPerRow;
  uint16_t mcu_h = JpegDec.MCUSPerCol;
  uint16_t mcu_x = 0;
  uint16_t mcu_y = 0;

  for (uint16_t j = 0; j < mcu_h; j++) {
    for (uint16_t i = 0; i < mcu_w; i++) {
      JpegDec.read();
      tft.drawBitmap(xpos + mcu_x, ypos + mcu_y, JpegDec.MCUWidth, JpegDec.MCUHeight, pImg);
      mcu_x += JpegDec.MCUWidth;
    }
    mcu_x = 0;
    mcu_y += JpegDec.MCUHeight;
  }
}

Tried hard resetting esp32 nano however, it only connects for one instance and as soon as I upload the code, it would disconnect and never connect.

Please don't hijack threads @ap15182002. I have split your post out to its own topic.

Hijacking is against the Arduino forum rules. The reason is that it may sidetrack the discussion, even preventing the creator of the topic from getting the assistance they need. It also reduces your own chances of getting assistance.

This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Hi @ap15182002.

Are you using an Arduino Nano ESP32 board?:

📷

ABX00092_00.default_1000x750.jpg by Arduino - CC BY-SA 4.0 (crop background)

I ask because your sketch appears to be for use with a board that has a camera (e.g., the ESP32-CAM).

When you say "disconnect", do you mean that the board stops producing a serial port?

If so, this is probably caused by a bug in either the "TFT_eSPI" library or version >2.0.14 of the "Arduino ESP32 Boards" / "esp32" boards platform: