Go Down

Topic: Class for DHT11, DHT21 and DHT22 (temperature & humidity) (Read 153381 times) previous topic - next topic

MaxG

Hi Rob,

No disrespect... I appreciate putting your work out there for others to use!

I took the files from GitHub as they were yesterday...
Proceeded as described; got the same compile error  in both IDEs...
Looked for an alternative, tested the AdaFruit library... worked, done.

Cheers, Max
Mainly using UNOs. Everything needs to be defined.

robtillaart

Thanks,

The latest version is an expirimental version,

The latest stable is - https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTstable
Rob Tillaart

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

robtillaart

Updated the DHTlib and DHTstable to support DHT12 and AM23XX.

- https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib (experimental AVR version)
- https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTstable (stable)


Comments and remarks as always welcome
Rob Tillaart

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

FooPlinger

So, I have just gotten the DHT11 to work and display values on an OLED, but the sketch I am using helpfully puts an "F" after the temperature, but only displays the temp in Celcius.  I have been digging through posts and websites for the last couple of hours, but have been unsuccessful.  I keep seeing people say use float, but I have no clue what that means, others say it is part of the library, but they must be using a different library than I am.
Using this library, how can I make it display the temperature in Farenheit?

Code: [Select]

#include <U8glib.h>  // U8glib library
#include <dht.h>     // DHT library

#define dht_dpin 2  //  pin to which the sensor is connected
dht DHT;
               /*Uncomment and comment*/
//U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // CLK=13, DIN=11, CS=10, DC=9, Reset=8
//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8

void draw(void)
{
   u8g.setFont(u8g_font_fub11r);   // select font
   u8g.drawStr(0, 11, "Temp: ");   // put string of display at position X, Y
   u8g.drawStr(80, 11, "Humi: ");
   u8g.setFont(u8g_font_fub14r);
   u8g.setPrintPos(10, 45);        // set position
   u8g.print(DHT.temperature, 0);  // display temperature from DHT11 in Celsius
   u8g.println("F");
   u8g.setPrintPos(72, 45);        // set position
   u8g.print(DHT.humidity, 0);     // display humidity from DHT11
   u8g.println("%");
}
void setup(void)
{

}

void loop(void)
{
   DHT.read11(dht_dpin);  // Read dpin on DHT11
   u8g.firstPage(); 
   do
{
   draw();
}  while( u8g.nextPage() );
   delay(5000);  // Delay of 2 sec before accessing DHT11 (min - 2sec)
}
                           /*END OF FILE*/

robtillaart

>> Using this library, how can I make it display the temperature in Farenheit?

You can convert any Celsius temperature to Fahrenheit, that does not depend on the sensor.


float Tc = someObject.getTemperatureC(....);

float Tf = Tc * 1.8 + 32;

you should add this formula into your sketch.


Some interesting temperature is -40C as that is also -40F;  And 100F is about human body temperature.

The Celsius scale is based on the physics of water (melting/boiling)  @ 1013 hPa  or 1 atmosphere or sea level pressure.

More fun is the Kelvin scale as it has no negative temperatures in practice.



Rob Tillaart

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

FooPlinger

After some trial and error, it seems that float is mostly working, but now it displays 32F all the time.  I changed the formula to add 33 in the conversion, and it displays 33.   Seems the float is being set to the last number in the conversion formula?

               /*Uncomment and comment*/
//U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // CLK=13, DIN=11, CS=10, DC=9, Reset=8
//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8

float Tc = DHT.temperature;
float Tf =  (Tc*1.8+32);

void draw(void)
{
   u8g.setFont(u8g_font_fub11r);   // select font
   u8g.drawStr(0, 11, "Temp: ");   // put string of display at position X, Y
   u8g.drawStr(80, 11, "Humi: ");
   u8g.setFont(u8g_font_fub14r);
   u8g.setPrintPos(10, 45);        // set position
   u8g.print(Tf, 0);
//   u8g.print(DHT.temperature, 0);  // display temperature from DHT11 in Celsius
   u8g.println("F");
   u8g.setPrintPos(72, 45);        // set position
   u8g.print(DHT.humidity, 0);     // display humidity from DHT11
   u8g.println("%");

FooPlinger

After more searching and work, it seems I just don't understand float.   I finally got it working with the code below.
A few notes.   If I tried to print Tc, it shows 0.  If I print Tf, it shows 32.



Code: [Select]

#include <U8glib.h>  // U8glib library
#include <dht.h>     // DHT library

#define dht_dpin 2  //  pin to which the sensor is connected
dht DHT;
               /*Uncomment and comment*/
//U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // CLK=13, DIN=11, CS=10, DC=9, Reset=8
//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9, 8); // CLK=13, DIN=11, CS=10, DC=9, Reset=8

//float Tc = DHT.temperature;
//float Tf = Tc * 1.8 + 32;
//float Tf = (32 + (DHT.temperature * 1.8));

void draw(void)
{
   u8g.setFont(u8g_font_fub11r);   // select font
   u8g.drawStr(0, 11, "Temp: ");   // put string of display at position X, Y
   u8g.drawStr(80, 11, "Humi: ");
   u8g.setFont(u8g_font_fub14r);
   u8g.setPrintPos(10, 45);        // set position
//   u8g.print(Tc, 0);
//   u8g.print(Tf, 0);
   u8g.print((DHT.temperature * 1.8 + 32), 0);  // display temperature from DHT11 in Fahreneit
   u8g.println("F");
   u8g.setPrintPos(72, 45);        // set position
   u8g.print(DHT.humidity, 0);     // display humidity from DHT11
   u8g.println("%");
}
void setup(void)
{

}

void loop(void)
{
   DHT.read11(dht_dpin);  // Read dpin on DHT11
   u8g.firstPage(); 
   do
{
   draw();
}  while( u8g.nextPage() );
   delay(5000);  // Delay of 2 sec before accessing DHT11 (min - 2sec)
}
                           /*END OF FILE*/

robtillaart


Replace

Code: [Select]
u8g.print((DHT.temperature * 1.8 + 32), 0);  // display temperature from DHT11 in Fahreneit

by

Code: [Select]

float f = DHT.temperature * 1.8 + 32;
int whole = (int)f;  // take the whole part of the float
int decimal = (int) ((f-whole)*10);   // first decimal
u8g.print(whole);
u8g.print(".");
u8g.print(decimal);

splitting it up in pieces can help.



footnote:
The DHT11 has only integer values for temperature and humidity, so no need to use floats there.


Rob Tillaart

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

FooPlinger

It looks like my problem was putting the float and calculations between lines 9 and 11, and then trying to print them later.  Once I moved your code all together inside the "void draw(void) " section, it works fine.

Thank you for the help, now I am off to see how to log this somewhere.  I have a dirt floor and cinder-block room under my brick porch on my new house that I want to use as a root cellar.  I am looking to log the data every x minutes (maybe ten, maybe 30) and keep those values so I can track it throughout the year.   I will also be putting a second dht11 outside of the root cellar to record the differences between the two.

Merry Christmas!

robtillaart

#234
Dec 24, 2017, 06:19 pm Last Edit: Dec 24, 2017, 06:19 pm by robtillaart
Better use a DHT22 as it more accurate and supports below zero (Celsius) temperatures.

did you do the storage math?

1 sample == 50 bytes? (including timestamp)

every 10 seconds --> 6 / minute  --> 360 / hour --> 9000 /day --> 3.300.000 / year ==> 165 MB
Rob Tillaart

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

Go Up