ESP32 potmeter not working after wifi-connection

Hey guys,
I want to sense the value of a potmeter and connect my ESP32 to wifi. However, when I connect the ESP32 to WiFi, the potmeter value returns only 4094. If I do not connect it to WiFi, it does work and does return the real-time value.

This is the code that does work:

#include <WiFi.h>
#include <PubSubClient.h>
/* change it with your ssid-password */
const char* ssid = "AndroidAP";
const char* password = "pkjl2388";
/* this is the IP of PC/raspberry where you installed MQTT Server */
const char* mqtt_server = "192.168.43.1";
/* create an instance of PubSubClient client */
WiFiClient lightPotmeter;
PubSubClient client(lightPotmeter);
/* topics */
#define LIGHT_TOPIC    "smarthome/room1/light"

long lastMsg = 0;
char msg[20];

int potPin = 4;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor


void setup() {
    Serial.begin(115200);

}

void loop() {
      val = analogRead(potPin);
   Serial.println("val=");
   Serial.println(val);
    delay(100);
}

Here is the code that does not work:

#include <WiFi.h>
#include <PubSubClient.h>
/* change it with your ssid-password */
const char* ssid = "AndroidAP";
const char* password = "pkjl2388";
/* this is the IP of PC/raspberry where you installed MQTT Server */
const char* mqtt_server = "192.168.43.1";
/* create an instance of PubSubClient client */
WiFiClient lightPotmeter;
PubSubClient client(lightPotmeter);
/* topics */
#define LIGHT_TOPIC    "smarthome/room1/light"

long lastMsg = 0;
char msg[20];

int potPin = 4;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor


void setup() {
    Serial.begin(115200);

  // We start by connecting to a WiFi network
  Serial.println();
  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());

  /* configure the MQTT server with IPaddress and port */
  client.setServer(mqtt_server, 1883);
  
}

void loop() {
      val = analogRead(potPin);
   Serial.println("val=");
   Serial.println(val);
    delay(100);
}

What can I do to make the ESP connect with WiFi while returning me the real-time value of the potmeter?

I am curious to your thoughts, many thanks in advance!