Harlow, Essex, UK
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« on: September 18, 2011, 04:29:29 am » |
I have DHT22 temp & humidity sensor and using the DHT.h library. I am then using a mySQL table to store the values. However, using the below code (similar to what I have seen elsewhere), the value being sent to mySQL are totally different to what the serial debug window is showing. I'm guessing its to do with the %d as part of the php URL, but i cannot find what this represents. Any one able to assist to why the values are so different. Many thanks, Serial Debug Window connected, writing ... Humidity: 51.30 % Temperature: 22.60 *C GET /arduino/arduino_push_output.php?t=-13107&h=16820
void getTemp (){ // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float humidity = dht.readHumidity(); float temp = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(temp) || isnan(humidity)) { Serial.println("Failed to read from DHT"); } else { Serial.println("connected, writing ..."); Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" *C"); // int temp = dht.readTemperature(); // int humidity = dht.readHumidity(); delay(50); sprintf(strURL,"GET /arduino/arduino_push_output.php?t=%d&h=%d",temp,humidity); client.println(strURL); client.println(); Serial.println(strURL); } }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3452
|
 |
« Reply #1 on: September 18, 2011, 05:34:52 am » |
According to your code, the variables temp and humidity are type float. You are converting them to a string with a integer converter. %d is a decimal integer %f is a floating point number
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9409
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: September 18, 2011, 05:50:06 am » |
IIRC %f doesn't work check this for float to string #include <stdlib.h> char *float2s(float f, unsigned int digits=2) { static char buf[16]; return dtostre(f, buf, digits, 1); }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3452
|
 |
« Reply #3 on: September 18, 2011, 05:58:23 am » |
IIRC %f doesn't work
OH!! That SUCKS!! 
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9409
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #4 on: September 18, 2011, 06:15:08 am » |
@Tim, There are worse things in the world .... for this there is at least a solution ...
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3452
|
 |
« Reply #5 on: September 18, 2011, 06:26:03 am » |
Maybe that was an over reaction. I know when things suck. Trust me. Thanks for the info. That would have certainly caught me off guard later on.
I am playing with the routine you suggested now.
EDIT: Well, that fix doesn't SUCK. It just sucks. 10.2 ends up 1.02e+01
So to get it to 10.2 from there is going to require some parsing, huh?
|
|
|
|
« Last Edit: September 18, 2011, 06:37:04 am by SurferTim »
|
Logged
|
|
|
|
|
Harlow, Essex, UK
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« Reply #6 on: September 18, 2011, 06:27:39 am » |
Thanks for taking the time to reply people, appriciated. It was driving me mad ! robtillaart - How would I add your suggestion into my current function so it outputs the correct decimal numbers ? You are correct though that %f doesn't work  , that would've been too simple  Steve
|
|
|
|
|
Logged
|
|
|
|
|
Harlow, Essex, UK
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« Reply #7 on: September 18, 2011, 06:43:24 am » |
If I add: int temp = dht.readTemperature(); int humidity = dht.readHumidity();
It outputs correctly (for example 24 degrees), but not down to 2 decimal places which is what I require.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9409
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #8 on: September 18, 2011, 06:43:39 am » |
from - http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42 - these avr libs can be used in Arduino!! The dtostre() function converts the double value passed in val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.
Conversion is done in the format "[-]d.ddde±dd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision prec; if the precision is zero, no decimal-point character appears. If flags has the DTOSTRE_UPPERCASE bit set, the letter 'E' (rather than 'e' ) will be used to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is "00".
If flags has the DTOSTRE_ALWAYS_SIGN bit set, a space character will be placed into the leading position for positive numbers.
If flags has the DTOSTRE_PLUS_SIGN bit set, a plus sign will be used instead of a space character in this case.
The dtostre() function returns the pointer to the converted string s. char* dtostrf ( double __val, signed char __width, unsigned char __prec, char * __s )
The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.
Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.
The dtostrf() function returns the pointer to the converted string s.
seems you need the dtostrf() variant.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3452
|
 |
« Reply #9 on: September 18, 2011, 06:50:37 am » |
@robtillaart: You are the MAN! The second function works good. No longer sucks at all! 
|
|
|
|
|
Logged
|
|
|
|
|
Harlow, Essex, UK
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« Reply #10 on: September 18, 2011, 07:17:30 am » |
This has now goneover my head  Could you please show what I would need to do to alter my code to produce the required results. I have been looking at http://www.php.net/manual/en/function.sprintf.php but this hasn't helped much as it suggest the %f should work 
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3452
|
 |
« Reply #11 on: September 18, 2011, 07:31:06 am » |
I do not think the %f will work. I do believe robtillaart. The second function he suggested works good, but not quite the same way. This is how I tested it: float testFloat = 10.24;
void setup() { Serial.begin(9600); }
void loop() { char tBuffer[16]; Serial.println(dtostrf(testFloat,8,2,tBuffer)); delay(1000); testFloat = testFloat + 1.1; } Upload it and open the serial monitor.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2353
|
 |
« Reply #12 on: September 18, 2011, 07:38:05 am » |
sprintf using %f does work in standard C, but as PaulS pointed out in another thread on the subject today, it's not implemented for arduino, to save space.
|
|
|
|
|
Logged
|
|
|
|
|
Harlow, Essex, UK
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« Reply #13 on: September 18, 2011, 07:44:19 am » |
Yup, I see that this code works, but i can't see how i can fit this into my code, a little over my head at the moment to pass humidity and temp.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2353
|
 |
« Reply #14 on: September 18, 2011, 07:58:01 am » |
Inefficient simple illustrative version: char tBuffer[16]; char hBuffer[16]; dtostrf(temp,8,2,tBuffer); dtostrf(humidity,8,2,hBuffer); sprintf(strURL,"GET /arduino/arduino_push_output.php?t=%s&h=%s",tBuffer,hBuffer);
|
|
|
|
|
Logged
|
|
|
|
|
|