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.
#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
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.