ESP32 met potmeter werkt niet na WiFi connectie

Hey allemaal!
Met mijn ESP32 wil ik de waarde van de potmeter meten. Dit gaat prima, totdat ik hem met WiFi verbindt. Wanneer ik de ESP32 met WiFi verbindt, krijg ik alleen de waarde 4094 terug. Wanneer ik de ESP32 niet met WiFi verbindt krijg ik wel de real-time waarde.

Deze code werkt:

#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);
}

Deze code werkt niet:

#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);
}

Wat kan ik doen om wel de real-time waarde van de potmeter te kunnen aflezen terwijl de ESP32 verbonden is met WiFi? Ik ben nieuwsgierig naar jullie gedachten, alvast heel hartelijk bedankt!

Hoe is de potmeter aangesloten?
Het lijkt dat je enkel de loper (aan pin4) en één van de buitenste aansluitingen gebruikt (aan massa).

Dit is een bekend Esp32 probleem, zie bv de volgende issues

WiFi.mode() breaks analogRead() for lower pins · Issue #1152 · espressif/arduino-esp32 · GitHub.

Je moet een pin uit de ADC1 range gebruiken.