MQTT Project Help - Potentiometer values

Good evening,
I have been working on a new project that reads the value of a potentiometer, and when the value changes, sends the value out to my mqtt broker.
I worked through the sketch in stages, to make sure there were no errors as i was going along, referencing other similar works for structure and order.
I would like the value of the pot to be sent out as a value between 0-255. This was working before i added the connection to my mqtt broker, but now the value reads between 0-1024.
I am using a nodemcu (i know its not an arduino) but i am using the arduino IDE.
I have been staring at this sketch for hours now, and i think i have now gone code blind, and i cant see what i’m missing/overlooking.
When i change the analogRead to potValue, i get values between 0-1024. When i use currentValue or previousValue, i get either 0 or 1024.

Could someone cast their eyes over this and point out my mistake?

Thanks

#include <ESP8266WiFi.h>
#include <PubSubClient.h>


int potValue = A0;
int currentValue = 0;
int previousValue = 0;
int delta = 0;
int deltaValue = 3; // needs to be changed to suit your needs

const char* ssid = "SSID";//put your wifi ssid here
const char* password = "PW";//put your wifi password here.
const char* mqtt_server = "Broker";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];

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

void callback(char* topic, byte* payload, unsigned int length)
{

} //end callback

void reconnect() {
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      client.subscribe("homeassistant/binary_sensor/master_switch/scene_switch/command");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
} //end reconnect()

void setup() {

  Serial.begin(57600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);


}
void loop() {
  {
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
    long now = millis();
    int status;

    currentValue = analogRead(potValue);

    currentValue = map(currentValue, 0, 1023, 0, 255);
    currentValue = constrain(currentValue, 0, 255);

    delta = abs(previousValue - currentValue);

    if (delta >= deltaValue) {
      lastMsg = now;
      int val = analogRead(potValue);
      String msg = " ";
      msg = msg + val;
      char message[58];
      msg.toCharArray(message, 58);
      Serial.println(message);

      client.publish("home_assistant/dimmer", message);
    }

  }

  previousValue = currentValue;

  delay(500);
}
    currentValue = analogRead(potValue);

    currentValue = map(currentValue, 0, 1023, 0, 255);
    currentValue = constrain(currentValue, 0, 255);

Are you a journo, used to being paid by the word?

    currentValue = analogRead(potValue) / 4;

Nodemcu, so a ESP8266 module with a 100k:220k resistor divider on it's analogue input. A pot and an ESP8266 (not ratiometric and crappy analogue input) is not the best combination. My ESP-12 doesn't return an analogue value from 0-1023, but 8-1024. You should see what your Nodemcu returns first, before you decide on a simple divide by 4.

Not sure what your goal is, but a rotary encoder could be a better solution for an ESP8266 than a pot. Leo..

Of course!

Thanks for your help!

Thanks Leo,

I did look at a ky-040, but i plan to use the digital pins for button functions.

At this stage the potentiometer does not need to be 100% so i’m not too worried.

Wawa: My ESP-12 doesn't return an analogue value from 0-1023, but 8-1024.

Eleven bits?

AWOL: Eleven bits?

Yes. int rawValue = analogRead(A0); // returns 1024 on my WeMos D1 mini when the analogue pin is connected to 3.3volt. Grounding the pin returns a fluctuating 7-8 I must have a special one :o Leo..