What is happening is that the variables you declared for mqttTemp etc are stored in memory something like this
variable memory address
mqttPres @ 1000-1004 (5 bytes)
mqttHum @ 1005-1010 (6 bytes)
mqttTemp @ 1011-1015 (6 bytes)
For a start, you only have enough room (with the mandatory NUL byte string terminator) for 4, 5 and 5 characters
When you convert pressure, the NUL byte is being placed as the first byte in mqttHum, therefore, when printing mqttHum you get an empty string output (since the first byte is NUL)
To really see what's happening, here's some code that illustrates the issue
char mqttTemp[6] = "TTTTT";
char mqttHum[6] = "HHHHH";
char mqttPres[5] = "PPPP";
void setup() {
float temp = 25.57;
float hum = 40.15;
float pres = 97684.39;
Serial.begin(74880);
// put your setup code here, to run once:
delay(100);
Serial.println("before");
printThem();
dtostrf(temp, 4, 2, mqttTemp);
Serial.println("converted temp");
printThem();
dtostrf(hum, 4, 2, mqttHum);
Serial.println("converted hum");
printThem();
dtostrf(pres, 5, 0, mqttPres);
Serial.println("converted press");
printThem();
}
void printThem()
{
Serial.print("mqttPres ");
Serial.print((unsigned long) &mqttPres, DEC);
Serial.print(": ");
for (int i = 0; i < 5; i++) {
Serial.print((int) mqttPres[i], DEC);
Serial.print(' ');
}
Serial.println();
Serial.print("mqttHum ");
Serial.print((unsigned long) &mqttHum, DEC);
Serial.print(": ");
for (int i = 0; i < 6; i++) {
Serial.print((int) mqttHum[i], DEC);
Serial.print(' ');
}
Serial.println();
Serial.print("mqttTemp ");
Serial.print((unsigned long) &mqttTemp, DEC);
Serial.print(": ");
for (int i = 0; i < 6; i++) {
Serial.print((int) mqttTemp[i], DEC);
Serial.print(' ');
}
Serial.println();
}
void loop() {
delay(50);
// put your main code here, to run repeatedly:
}
The above outputs the following:
before
mqttPres 1073644748: 80 80 80 80 0
mqttHum 1073644753: 72 72 72 72 72 0
mqttTemp 1073644759: 84 84 84 84 84 0
converted temp
mqttPres 1073644748: 80 80 80 80 0
mqttHum 1073644753: 72 72 72 72 72 0
mqttTemp 1073644759: 50 53 46 53 55 0
converted hum
mqttPres 1073644748: 80 80 80 80 0
mqttHum 1073644753: 52 48 46 49 53 0
mqttTemp 1073644759: 50 53 46 53 55 0
converted press
mqttPres 1073644748: 57 55 54 56 52
mqttHum 1073644753: 0 48 46 49 53 0
^ ======== note the leading 0
mqttTemp 1073644759: 50 53 46 53 55 0
The simplest fix, of course, is to declare your variables with the correct size
char mqttTemp[7]; // because -10.00 takes 6 characters + 1 for NULL
char mqttHum[7]; // because 100.00 takes 6 characters + 1 for NULL
char mqttPres[6]; // because 97684 takes 5 characters + 1 for NULL - if pressure can reach 100000 then use 7 here as well
and then use dtostrf
correctly, i.e. the second argument is the total width, including .
and a possible -
dtostrf(temp,6,2,mqttTemp);
dtostrf(hum,6,2,mqttHum);
dtostrf(pres,5,0,mqttPres);
dtostrf
incorrectly (the width of temperature is at least 5, or 6 if you can get temperatures at -10.00 or less, and the width of humidity is at least 6, since100.00
is 6 characters