Pages: [1]   Go Down
Author Topic: Efficient and simple DHT library  (Read 4082 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a library for reading out the DHT family of temperature/humidity sensors with the following features:
  • Support for DHT11 and DHT22, AM2302, RHT03
  • Auto detect sensor model
  • Low memory footprint
  • Very small code base
Usage:
Code:
#include "DHT.h"

DHT dht;

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

  dht.setup(2); // data pin 2
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  Serial.print(dht.getHumidity());
  Serial.print("\t");
  Serial.print(dht.getTemperature());
}

Download link: https://github.com/markruys/arduino-DHT

I liked writing it - perhaps useful for someone else...
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great. Will compare yours with my current when I have the time.

One remark: could you put the strings into PROGMEM to save a several bytes of RAM?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you put the strings into PROGMEM to save a several bytes of RAM?
I guess you refer to:
Code:
char* DHT::getStatusString()
{
  switch ( DHT::getStatus() ) {
    case DHT::ERROR_NONE:
      return "OK";

    case DHT::ERROR_TIMEOUT:
      return "TIMEOUT";

    case DHT::ERROR_CHECKSUM:
      return "CHECKSUM";

    default:
      return "ERROR";
  }
}
Perhaps I miss your point, but these strings already are in flash memory. The method returns a pointer to a PROGMEM location.
Logged

Sydney, Australia
Offline Offline
Sr. Member
****
Karma: 6
Posts: 397
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a library for reading out the DHT family of temperature/humidity sensors with the following features:
  • Support for DHT11 and DHT22, AM2302, RHT03
  • Auto detect sensor model
  • Low memory footprint
  • Very small code base
Usage:
Code:
#include "DHT.h"

DHT dht;

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

  dht.setup(2); // data pin 2
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  Serial.print(dht.getHumidity());
  Serial.print("\t");
  Serial.print(dht.getTemperature());
}

Download link: https://github.com/markruys/arduino-DHT

I liked writing it - perhaps useful for someone else...


Thanks Very much for this - i was just about implement my first one of these so will give it a whirl and report back !

regards

Craig
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you put the strings into PROGMEM to save a several bytes of RAM?
Perhaps I miss your point, but these strings already are in flash memory. The method returns a pointer to a PROGMEM location.
No, you did not miss my point. smiley-red
I got less memory usage when defining these strings as const char progmem. Weird. Must check again, might have made more sketch changes that cause the difference in RAM usage...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you put the strings into PROGMEM to save a several bytes of RAM?
Perhaps I miss your point, but these strings already are in flash memory. The method returns a pointer to a PROGMEM location.
No, you did not miss my point. smiley-red
I got less memory usage when defining these strings as const char progmem. Weird. Must check again, might have made more sketch changes that cause the difference in RAM usage...
Well, you made me dig a bit deeper in the workings of PROGMEM storage, and you are right SRAM usages can be lowered! Strings in
Code:
return "TIMEOUT"
are copied to SRAM and returned to the caller. So I tried to rewrite getStatusString() and store the strings in PROGMEM. I don't want users of the library be bothered with strcpy_P statements, so I came up with this:
Code:
prog_char P_OK[]       PROGMEM = "OK";
prog_char P_TIMEOUT[]  PROGMEM = "TIMEOUT";
prog_char P_CHECKSUM[] PROGMEM = "CHECKSUM";

const char *DHT::getStatusString() {
  prog_char *c;
  switch ( error ) {
    case DHT::ERROR_CHECKSUM:
      c = P_CHECKSUM; break;

    case DHT::ERROR_TIMEOUT:
      c = P_TIMEOUT; break;

    default:
      c = P_OK; break;
  }

  static char buffer[9];
  strcpy_P(buffer, c);

  return buffer;
}
Indeed SRAM usage drops with (2+1 + 7+1 + 8+1 - 9) = 11 bytes. The cost however is 26 bytes more code, and I don't think that's worth it, do you. Perhaps you know a more elegant way?
Logged

0
Offline Offline
Faraday Member
**
Karma: 47
Posts: 5903
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I mark,
i will test your library on a DHT11 and report here

for the progmem question should use the arduino "F" function ?

Code:
Version 1.0 of the Arduino IDE introduced the F() syntax for storing strings in flash memory rather than RAM. e.g.
Serial.println(F("This string will be stored in flash memory"));
« Last Edit: July 16, 2013, 05:02:47 am by Testato » Logged

- [Guida] IDE - http://goo.gl/ln6glr
- [Lib] ST7032i LCD I2C - http://goo.gl/GNojT6
- [Lib] PCF8574+HD44780 LCD I2C - http://goo.gl/r7CstH

0
Offline Offline
Faraday Member
**
Karma: 47
Posts: 5903
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried your library and all work good with DHT11, soon will arrive a DHT22 to me and i will test with it also.

2 tips for you:

- Instead of "arduino-DHT-master" you must use "arduino_DHT_master" because "- minus symbol" it is not allowed in arduino IDE library, and this broke the autoimport function (Sketch->Import->Add Library)

- May use a version number in the name of the .zip so we can immediately check if is there a new version on github
Logged

- [Guida] IDE - http://goo.gl/ln6glr
- [Lib] ST7032i LCD I2C - http://goo.gl/GNojT6
- [Lib] PCF8574+HD44780 LCD I2C - http://goo.gl/r7CstH

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

FYI
There exists a DHT21 too. It has a different dataformat (again), link to PDF see - http://forum.arduino.cc/index.php?topic=58531.msg1366942#msg1366942 -
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: