Pages: [1] 2   Go Down
Author Topic: DHT22 and mySQL error (%d)  (Read 5190 times)
0 Members and 1 Guest are viewing this topic.
Harlow, Essex, UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Quote
connected, writing ...
Humidity: 51.30 %   Temperature: 22.60 *C
GET /arduino/arduino_push_output.php?t=-13107&h=16820

Code:
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 Offline
Faraday Member
**
Karma: 150
Posts: 6130
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


IIRC %f doesn't work

check this for float to string
Code:
#include <stdlib.h>
char *float2s(float f, unsigned int digits=2)
{
  static char buf[16];
  return dtostre(f, buf, digits, 1);
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6130
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IIRC %f doesn't work
OH!! That SUCKS!!  smiley-sad
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


@Tim,
There are worse things in the world .... for this there is at least a solution ...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6130
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 1
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad, that would've been too simple smiley-wink

Steve
Logged

Harlow, Essex, UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I add:

Code:
      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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6130
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@robtillaart: You are the MAN! The second function works good. No longer sucks at all!  smiley
Logged

Harlow, Essex, UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This has now goneover my head  smiley-eek

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 smiley-sad
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6130
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 Offline
Faraday Member
**
Karma: 70
Posts: 3741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 1
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 70
Posts: 3741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Inefficient simple illustrative version:
Code:
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

Pages: [1] 2   Go Up
Jump to: