I want to publish numeric value to MQTT but cannot workout conversion dataype

So you want to publish the value of responss, not the value of 2 variables ?

If so, then take note of what I said in reply #18, ie

A more logical flow would seem be to read the user input and if there is something to publish then go ahead and publish it having tested that the client is connected and to reconnect if it isn’t

So you want to publish the value of responss, not the value of 2 variables ?

Value of responss gives more than 2 values and I only want to publish 2 values on MQTTBox. What is syntax for that?

Sorry, I am lost

responss is an int and can only have one value at a time
In your program responss gets its value from reading a single byte from Serial

What are the 2 values that you are referring to ?

Please describe the flow of what the user does and when the value of responss should be published

What are the 2 values that you are referring to?

I am referring to responss last two values. Attached is the image of Serial Monitor and in this image, you clearly see that responss gives 6 values in which AF, FC, FE, 40 is input and 15, D2 is sensor output. Seriall . I want to publish 15, D2 on MQTTBox. Please help me in this regard

Write a function that publishes responss so that you can call it when you need it, then in the loop that does the 6 reads call it when you read the 5th and 6th values

As I have said before, only publishing the value in the reconnect() function is the wrong thing to do

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

int respons;
int responss;

//  network setting.

const char* ssid = "hamid";
const char* password = "3214774136";
const char* mqtt_server = "broker.mqtt-dashboard.com";

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

void setup() {

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

void setup_wifi() {

  delay(10);
  // 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.begin(19200);

  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

}

void reconnect() {

  // Loop until we're reconnected

  Serial.println();
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      char dataArray[10]; //an array to put data in
      //char dataArray1[10];
      sprintf(dataArray, "%d", responss ); //turn the data into a C string
      client.publish("Topic", dataArray); //publish the data
      // ... and resubscribe
      //client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  // sensor programming

  Serial.begin(19200);
  byte poll[] = {0xAF, 0xFC, 0xFE, 0x40};
  Serial.write(poll, sizeof(poll));
  delay(3000);
  int x = 6;

  while (x > 0)
  {
    responss = Serial.read();
    //Serial.print("output =");
    Serial.println(responss, HEX);
    delay(500);
    x--;
  }
  Serial.end();
  //resetFunc();
}
if (!client.connected()) {
  reconnect();
}
client.loop();
long now = millis();
//respons = Serial.read();
if (now - lastMsg > 2000) {
  lastMsg = now;
  ++value;
  Serial.print("Publish message: ");
  Serial.println(respons);
  //client.publish("Topic", "sensor");
}

Please tell me the syntax or exact command and where I need to write those command as I am new to programming

I am not going to write it for you

This section of code

      char dataArray[10]; //an array to put data in
      //char dataArray1[10];
      sprintf(dataArray, "%d", responss - 1); //turn the data into a C string
      //sprintf(dataArray1, "%d", responss);
      client.publish("Topic", dataArray); //publish the data

publishes the value of responss. Take it out of reconnect() and put it in its own function

This section of code

  while (x > 0)
  {
    responss = Serial.read();
    //Serial.print("output =");
    Serial.println(responss, HEX);
    delay(500);
    x--;
  }

reads 6 values from Serial (badly). You want to publish the fifth and sixth values so when when you have read either of them into responss call your new function

If/when you get that working we can talk about making your code more robust

How to write a function o call 5th and sixth value? Please help me

Start with counting the received values
In

while (x > 0)
  {
    responss = Serial.read();
    //Serial.print("output =");
    Serial.println(responss, HEX);
    delay(500);
    x--;
  }

print the value of x before printing responss
what do you see ?
what do the numbers mean ?

printing x gives 6,5,4,3,2,1. Actually I applied this while loop and gives x=6 because controller gives too many raw values after reading the sensor data. So purpose of int x =6; is to specify the values which I needed

Using that value can you tell when you have just read the fifth and sixth value ?

Its on 1 and 2. Please help me to publish the values on these.

Good

Add this function to your sketch
while (x > 0)
{
responss = Serial.read();
//Serial.print("output =");
Serial.println(responss, HEX);
delay(500);
x--;
}
Call it after you read the fifth and sixth values
Does it print the correct values ?

expected primary-expression before ';' token it gives this error when I put x-; only. It only gives values x--; on this

My fault, I posted the wrong code section

Add this function to your code and call it after you read the fifth and sixth values
Does it print the correct values ?

void output()
{
  Serial.print("the value of responss is ");
  Serial.println(responss);
}

What does this means?

You have previously established that you know the value of x when you read either of these two values so, when x is one of the two trigger values call the new output() function

Yes it prints the all six values That I needed. What is next step?

It was my mistake. Actually I made random guess because x=6 and x=--; is delimiter and after seeing the Serial mointor I said its 1 and 2. Please help me

@UKHeliBob If I want to publish all the six values on MQTTBox what is syntax for that?
Thanks