Use Serial Print variable in client.publish with pubsubclient

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

void loop() {
Serial.println(ReadVoltage(33),3);]
  Serial.println(analogRead(33));
  delay(1000);
}

double ReadVoltage(byte pin){
  double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if(reading < 1 || reading > 4095) return 0;
  // return -0.000000000009824 * pow(reading,3) + 0.000000016557283 * pow(reading,2) + 0.000854596860691 * reading + 0.065440348345433;
  return -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089;
}

Serial.println(ReadVoltage(33),3);
How can I publish this with client.publish?

int voltage = (ReadVoltage(33));

if (!isnan(voltage))
{
snprintf (msg, 20, "%d", voltage);
/* publish the message */
client.publish(BATTERY_TOPIC, msg);
}

Voltage: 3.089
Analog Voltage: 3917

That is my output from Serial

If I use that it publishes 3
Thanks for help
~Marlon

int voltage = (ReadVoltage(33));Change the type to float. But then your sprintf won't work, so change it to dtostrf.

Please remember to use code tags when posting code

float voltage = (ReadVoltage(33));

 if (!isnan(voltage))
  {
    dtostrf (msg, 20, "%d", voltage);
    /* publish the message */
    client.publish(BATTERY_TOPIC, msg);
  }

It wont work for me. On compiling I get following error:

exit status 1
cannot convert 'char*' to 'double' for argument '1' to 'char* dtostrf(double, signed char, unsigned char, char*)'

The first argument to dtostrf() is the double to convert. RTFM.

I dont understand. Can you please sent me the corrected code please?

mar_robHD:
I dont understand. Can you please sent me the corrected code please?

You don't understand that you need to google functions you've never used before, to see how they should be used? If that IS the case, we can't help you.

What I would have used was

dtostrf(voltage, 6, 2, msg); // Assumes that msg is at least 7 characters

YMMV.