DHT11 issues (sprintf and snprintf) [Solved]

Ill start by saying that I am using the DHTLib found HERE

For some reason I can not get the response from the sensor (DHT.temperature and DHT.humidity) to work with the sprinf call.

Here is some example code. (NOTE: The point is to create a single variable formated in HTML that I can send to the network card.)

#include <dht.h>

char tdr[100];
dht DHT;

#define DHT11_PIN 8

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

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  delay(2000);
  snprintf(tdr, 100, "

The Temperature is %f C

The Humidity is %f\r\n", DHT.temperature, DHT.humidity);
  Serial.println(tdr);
}

Temperature and Humidity return a ?, not sure what I am doing wrong. The dht.h file shows both to be of type double. I have tried using just about every specifier in the list and none of them return the value.

So what I am doing wrong?

Floats doesn't work by default in sprintf.

See link in my signature, or alternatively you can first convert the float to a char array with function dtostrf.

Thank you for the input!

However, it kind of fixed the problem. lol

Here is the code

#include <dht.h>

char tdr[100];
char temp[5];
char humi[5];
dht DHT;

#define DHT11_PIN 8

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly
  int chk = DHT.read11(DHT11_PIN);
  delay(2000);
  dtostrf(DHT.temperature,5 , 2, temp);
  dtostrf(DHT.humidity,5 , 2, humi);
  Serial.print(DHT.temperature);
  Serial.print("\t");
  Serial.println(DHT.humidity);
  snprintf(tdr, 100, "

The Temperature is %sC

The Humidity is %s\r\n", temp, humi);
  Serial.println(tdr);
}

And here is the output

23.00 38.00

The Temperature is C

The Humidity is 38.00

Seems that Temperature is not being translated or I am doing it wrong somewhere.

I suppose it's because your temp and humi char arrays are too small, "23.00" is already 5 characters, so where would dtostrf store the NULL at the end to make it a valid C string? Increase size to at least 6 and I think it will work. I would increase the size of temp to 7 for possibly negative value :wink:

I noticed that a little earlier while playing with it. However, changing it to 7 does not fix the issue, but it does change the output.

23.00 37.00

The Temperature is 00C

The Humidity is 37.00

Cancel that, I just got it to work.

The solution is a bit odd.

if I set "char temp[7];" and use "dtostrf(DHT.temperature,7 , 2, temp);" it gives me the empty character.

if I set "char temp[7];" and use "dtostrf(DHT.temperature,6 , 2, temp);" it converts it correctly.

So I guess that dtostrf is not actually counting the "/0" in it's size of array calculation.

So I guess that dtostrf is not actually counting the "/0" in it's size of array calculation.

In the first call, you told it (incorrectly) that there was room in the array for it to write 7 characters plus the terminating NULL.

In the second call, you told it (correctly) that there was room in the array for it to write 6 characters plus the terminating NULL.

The function does not, and can not, determine that the values you passed in are correct.

Paul,

I was saying that I expected an array[7] to be 7 entries long and the "\0" to be deducted from it by the call.

Maybe I have it all wrong. So let me say it with an snprintf command

So if I set an array size of 20 and then do an snprintf(array, 20, "Some Data to %s in", array2) and array2 is "put " it will fail because the data going into the array is 20 char's long and there is no space for the "/0"?

Would doing this cause a failure or would the program take it and then give unexpected responses?

So if I set an array size of 20 and then do an snprintf(array, 20, "Some Data to %s in", array2) and array2 is "put " it will fail because the data going into the array is 20 char's long and there is no space for the "/0"?

If the array size is 20, you can write 19 characters (the 16 in the double quotes + the three in array2) plus the terminating NULL with no problems.

If array2 contained "send", then snprintf() would lop the n off the end. sprintf(), on the other hand, would not, and it would put the NULL beyond the end of the array, overwriting some other data.

Would doing this cause a failure or would the program take it and then give unexpected responses?

Unexpected responses ARE a failure, so it's not an either/or situation.