Go Down

Topic: [LIB] Interrupt driven DHTLib (DHT11 & DHT22 = idDHTLib) (Read 24872 times) previous topic - next topic

Dessimat0r

Running simple idDHT11 example with dht22callback:

Code: [Select]
ididDHTLib Example program
LIB version: 0.0.2
---------------

Retrieving information from sensor: Read sensor:


... and then nothing.

Dessimat0r

#16
Jul 30, 2013, 07:41 pm Last Edit: Jul 30, 2013, 07:44 pm by Dessimat0r Reason: 1
With 2 second delay in some places:

Code: [Select]
Retrieving information from sensor: Read sensor: Error

Time out error
Humidity (%): -2.00
Temperature (oC): -2.00
Temperature (oF): -2.00
Temperature (K): -2.00
Dew Point (oC): -2.00
Dew Point Slow (oC): -2.00


It repeats this exact output in a loop.

Code: [Select]


/*
 Board          int.0  int.1  int.2  int.3  int.4  int.5
Uno, Ethernet  2       3
Mega2560         2       3       21      20      19      18
Leonardo         3       2       0       1
Due          (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)

This example, as difference to the other, make use of the new method acquireAndWait()
*/

#include <idDHTLib.h>

int idDHTLibPin = 2; //Digital pin for comunications
int idDHTLibIntNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)

//declaration
void dhtLib_wrapper(); // must be declared before the lib initialization

// Lib instantiate
idDHTLib DHTLib(idDHTLibPin,idDHTLibIntNumber,dhtLib_wrapper);

void setup()
{
 Serial.begin(9600);
 Serial.println("idDHTLib Example program");
 Serial.print("LIB version: ");
 Serial.println(IDDHTLIB_VERSION);
 Serial.println("---------------");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dhtLib_wrapper() {
 DHTLib.dht22Callback(); // Change dht11Callback() for a dht22Callback() if you have a DHT22 sensor
}
void loop()
{
 Serial.print("\nRetrieving information from sensor: ");
 Serial.print("Read sensor: ");
 delay(2000);
 
 int result = DHTLib.acquireAndWait();
 switch (result)
 {
 case IDDHTLIB_OK:
   Serial.println("OK");
   break;
 case IDDHTLIB_ERROR_CHECKSUM:
   Serial.println("Error\n\r\tChecksum error");
   break;
 case IDDHTLIB_ERROR_TIMEOUT:
   Serial.println("Error\n\r\tTime out error");
   break;
 case IDDHTLIB_ERROR_ACQUIRING:
   Serial.println("Error\n\r\tAcquiring");
   break;
 case IDDHTLIB_ERROR_DELTA:
   Serial.println("Error\n\r\tDelta time to small");
   break;
 case IDDHTLIB_ERROR_NOTSTARTED:
   Serial.println("Error\n\r\tNot started");
   break;
 default:
   Serial.println("Unknown error");
   break;
 }
 Serial.print("Humidity (%): ");
 Serial.println(DHTLib.getHumidity(), 2);

 Serial.print("Temperature (oC): ");
 Serial.println(DHTLib.getCelsius(), 2);

 Serial.print("Temperature (oF): ");
 Serial.println(DHTLib.getFahrenheit(), 2);

 Serial.print("Temperature (K): ");
 Serial.println(DHTLib.getKelvin(), 2);

 Serial.print("Dew Point (oC): ");
 Serial.println(DHTLib.getDewPoint());

 Serial.print("Dew Point Slow (oC): ");
 Serial.println(DHTLib.getDewPointSlow());

 delay(2000);
}

niesteszeck


This DHTTester code works:

Code: [Select]
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
  }
}


Any major differences?



Yes, at least one in my first check, the declaration/initialization of the lib is different, in the first example you use:
Code: [Select]
DHT dht(DHTTYPE, DHTPIN);
In the second one:
Code: [Select]
DHT dht(DHTPIN, DHTTYPE);

so, I suppose as the second one is working, that is correct, and the first one have an error in the constructor call

niesteszeck


With 2 second delay in some places:

Code: [Select]
Retrieving information from sensor: Read sensor: Error

Time out error
Humidity (%): -2.00
Temperature (oC): -2.00
Temperature (oF): -2.00
Temperature (K): -2.00
Dew Point (oC): -2.00
Dew Point Slow (oC): -2.00


It repeats this exact output in a loop.

Code: [Select]


/*
 Board          int.0  int.1  int.2  int.3  int.4  int.5
Uno, Ethernet  2       3
Mega2560         2       3       21      20      19      18
Leonardo         3       2       0       1
Due          (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)

This example, as difference to the other, make use of the new method acquireAndWait()
*/

#include <idDHTLib.h>

int idDHTLibPin = 2; //Digital pin for comunications
int idDHTLibIntNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)

//declaration
void dhtLib_wrapper(); // must be declared before the lib initialization

// Lib instantiate
idDHTLib DHTLib(idDHTLibPin,idDHTLibIntNumber,dhtLib_wrapper);

void setup()
{
 Serial.begin(9600);
 Serial.println("idDHTLib Example program");
 Serial.print("LIB version: ");
 Serial.println(IDDHTLIB_VERSION);
 Serial.println("---------------");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dhtLib_wrapper() {
 DHTLib.dht22Callback(); // Change dht11Callback() for a dht22Callback() if you have a DHT22 sensor
}
void loop()
{
 Serial.print("\nRetrieving information from sensor: ");
 Serial.print("Read sensor: ");
 delay(2000);
 
 int result = DHTLib.acquireAndWait();
 switch (result)
 {
 case IDDHTLIB_OK:
   Serial.println("OK");
   break;
 case IDDHTLIB_ERROR_CHECKSUM:
   Serial.println("Error\n\r\tChecksum error");
   break;
 case IDDHTLIB_ERROR_TIMEOUT:
   Serial.println("Error\n\r\tTime out error");
   break;
 case IDDHTLIB_ERROR_ACQUIRING:
   Serial.println("Error\n\r\tAcquiring");
   break;
 case IDDHTLIB_ERROR_DELTA:
   Serial.println("Error\n\r\tDelta time to small");
   break;
 case IDDHTLIB_ERROR_NOTSTARTED:
   Serial.println("Error\n\r\tNot started");
   break;
 default:
   Serial.println("Unknown error");
   break;
 }
 Serial.print("Humidity (%): ");
 Serial.println(DHTLib.getHumidity(), 2);

 Serial.print("Temperature (oC): ");
 Serial.println(DHTLib.getCelsius(), 2);

 Serial.print("Temperature (oF): ");
 Serial.println(DHTLib.getFahrenheit(), 2);

 Serial.print("Temperature (K): ");
 Serial.println(DHTLib.getKelvin(), 2);

 Serial.print("Dew Point (oC): ");
 Serial.println(DHTLib.getDewPoint());

 Serial.print("Dew Point Slow (oC): ");
 Serial.println(DHTLib.getDewPointSlow());

 delay(2000);
}




I see that is the "simple" example, have you tried the other example?

niesteszeck

#19
Jul 30, 2013, 08:15 pm Last Edit: Jul 30, 2013, 08:39 pm by niesteszeck Reason: 1
I have added a new branch to the lib that have more detailed timeout error (we can see wich of the three time out error is causing the problem)
Please try this modification to see the output so we can figure aout, what part of the code is generating the timeout. The new branch is called dht22_bug_detect and you can download it from https://github.com/niesteszeck/idDHTLib/tree/dht22_bug_detect

Dessimat0r

Ah, thanks for noticing that problem. That explains my issues then with the other lib :) I'll try with the non-simple example.

Dessimat0r

#21
Jul 30, 2013, 08:45 pm Last Edit: Jul 30, 2013, 08:48 pm by Dessimat0r Reason: 1
With non-simple example:

Code: [Select]
idDHTLib Example program
LIB version: 0.0.2
---------------

Retrieving information from sensor: Read sensor:


And then nothing..

Code:
Code: [Select]
/*
  Board           int.0   int.1   int.2   int.3   int.4   int.5
Uno, Ethernet   2   3
Mega2560   2   3   21   20   19   18
Leonardo   3   2   0   1
Due           (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)
*/

#include <idDHTLib.h>

int idDHTLibPin = 2; //Digital pin for comunications
int idDHTLibIntNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)

//declaration
void dhtLib_wrapper(); // must be declared before the lib initialization

// Lib instantiate
idDHTLib DHTLib(idDHTLibPin,idDHTLibIntNumber,dhtLib_wrapper);

void setup()
{
  Serial.begin(9600);
  Serial.println("idDHTLib Example program");
  Serial.print("LIB version: ");
  Serial.println(IDDHTLIB_VERSION);
  Serial.println("---------------");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dhtLib_wrapper() {
  DHTLib.dht22Callback(); // Change dht11Callback() for a dht22Callback() if you have a DHT22 sensor
}
void loop()
{
  Serial.print("\nRetrieving information from sensor: ");
  Serial.print("Read sensor: ");
  //delay(2000);
  DHTLib.acquire();
  while (DHTLib.acquiring())
    ;
  int result = DHTLib.getStatus();
  switch (result)
  {
  case IDDHTLIB_OK:
    Serial.println("OK");
    break;
  case IDDHTLIB_ERROR_CHECKSUM:
    Serial.println("Error\n\r\tChecksum error");
    break;
  case IDDHTLIB_ERROR_TIMEOUT:
    Serial.println("Error\n\r\tTime out error");
    break;
  case IDDHTLIB_ERROR_ACQUIRING:
    Serial.println("Error\n\r\tAcquiring");
    break;
  case IDDHTLIB_ERROR_DELTA:
    Serial.println("Error\n\r\tDelta time to small");
    break;
  case IDDHTLIB_ERROR_NOTSTARTED:
    Serial.println("Error\n\r\tNot started");
    break;
  default:
    Serial.println("Unknown error");
    break;
  }
  Serial.print("Humidity (%): ");
  Serial.println(DHTLib.getHumidity(), 2);

  Serial.print("Temperature (oC): ");
  Serial.println(DHTLib.getCelsius(), 2);

  Serial.print("Temperature (oF): ");
  Serial.println(DHTLib.getFahrenheit(), 2);

  Serial.print("Temperature (K): ");
  Serial.println(DHTLib.getKelvin(), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(DHTLib.getDewPoint());

  Serial.print("Dew Point Slow (oC): ");
  Serial.println(DHTLib.getDewPointSlow());

  delay(2000);
}


With the initial 2000 sec delay as before, there is the initial version output, then in a loop:
Code: [Select]

Retrieving information from sensor: Read sensor: Error

Time out error
Humidity (%): -2.00
Temperature (oC): -2.00
Temperature (oF): -2.00
Temperature (K): -2.00
Dew Point (oC): -2.00
Dew Point Slow (oC): -2.00


Code:

Code: [Select]
/*
 Board          int.0  int.1  int.2  int.3  int.4  int.5
Uno, Ethernet  2  3
Mega2560  2  3  21  20  19  18
Leonardo  3  2  0  1
Due          (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)
*/

#include <idDHTLib.h>

int idDHTLibPin = 2; //Digital pin for comunications
int idDHTLibIntNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)

//declaration
void dhtLib_wrapper(); // must be declared before the lib initialization

// Lib instantiate
idDHTLib DHTLib(idDHTLibPin,idDHTLibIntNumber,dhtLib_wrapper);

void setup()
{
 Serial.begin(9600);
 Serial.println("idDHTLib Example program");
 Serial.print("LIB version: ");
 Serial.println(IDDHTLIB_VERSION);
 Serial.println("---------------");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dhtLib_wrapper() {
 DHTLib.dht22Callback(); // Change dht11Callback() for a dht22Callback() if you have a DHT22 sensor
}
void loop()
{
 Serial.print("\nRetrieving information from sensor: ");
 Serial.print("Read sensor: ");
 delay(2000);
 DHTLib.acquire();
 while (DHTLib.acquiring())
   ;
 int result = DHTLib.getStatus();
 switch (result)
 {
 case IDDHTLIB_OK:
   Serial.println("OK");
   break;
 case IDDHTLIB_ERROR_CHECKSUM:
   Serial.println("Error\n\r\tChecksum error");
   break;
 case IDDHTLIB_ERROR_TIMEOUT:
   Serial.println("Error\n\r\tTime out error");
   break;
 case IDDHTLIB_ERROR_ACQUIRING:
   Serial.println("Error\n\r\tAcquiring");
   break;
 case IDDHTLIB_ERROR_DELTA:
   Serial.println("Error\n\r\tDelta time to small");
   break;
 case IDDHTLIB_ERROR_NOTSTARTED:
   Serial.println("Error\n\r\tNot started");
   break;
 default:
   Serial.println("Unknown error");
   break;
 }
 Serial.print("Humidity (%): ");
 Serial.println(DHTLib.getHumidity(), 2);

 Serial.print("Temperature (oC): ");
 Serial.println(DHTLib.getCelsius(), 2);

 Serial.print("Temperature (oF): ");
 Serial.println(DHTLib.getFahrenheit(), 2);

 Serial.print("Temperature (K): ");
 Serial.println(DHTLib.getKelvin(), 2);

 Serial.print("Dew Point (oC): ");
 Serial.println(DHTLib.getDewPoint());

 Serial.print("Dew Point Slow (oC): ");
 Serial.println(DHTLib.getDewPointSlow());

 delay(2000);
}

niesteszeck

#22
Jul 30, 2013, 10:11 pm Last Edit: Jul 30, 2013, 10:15 pm by niesteszeck Reason: 1
Please, can you try the new branch that I created on github? (in a previous post is linked) so I can get a error message that represent what kind of timeout occurs

Dessimat0r


niesteszeck

#24
Jul 30, 2013, 10:16 pm Last Edit: Jul 30, 2013, 10:17 pm by niesteszeck Reason: 1
don't worry ;)
forget to mention, use the simple example!!!!!!

Dessimat0r

On the new simple example (no modifications, correct pin / interrupt already set):
Code: [Select]

Retrieving information from sensor: Read sensor: Error

Time out error 3
Humidity (%): -7.00
Temperature (oC): -7.00
Temperature (oF): -7.00
Temperature (K): -7.00
Dew Point (oC): -7.00
Dew Point Slow (oC): -7.00


This goes in a loop :)

niesteszeck

#26
Jul 30, 2013, 10:36 pm Last Edit: Jul 30, 2013, 10:42 pm by niesteszeck Reason: 1
I added 10 ms more to accept a 1 as valid signal
on line 111
from this
Code: [Select]
} else if(60<delta && delta<135) { //valid in timing
to this
Code: [Select]
} else if(60<delta && delta<145) { //valid in timing

you can manually change it or download the updated branch, please try this, maybe this sensor takes a litle bit more to send a one, but still a strange thing.

If this still not working, you can try modifying this values. I changed delta<135 to a delta<145 so I added 10ms more to accept  a signal as valid. So you can play whit the 60 and 135 (145) numbers to check what timing (the 0 that is 76ms or the 1 that is 120ms) is causing the problem

Dessimat0r

Now this goes in a loop:


Code: [Select]
Retrieving information from sensor: Read sensor: Error

Checksum error
Humidity (%): -1.00
Temperature (oC): -1.00
Temperature (oF): -1.00
Temperature (K): -1.00
Dew Point (oC): -1.00
Dew Point Slow (oC): -1.00

Dessimat0r

I had the old callback by accident, but the results are the same if I change it to the dht22 callback.

Code: [Select]
Retrieving information from sensor: Read sensor: Error

Checksum error
Humidity (%): -1.00
Temperature (oC): -1.00
Temperature (oF): -1.00
Temperature (K): -1.00
Dew Point (oC): -1.00
Dew Point Slow (oC): -1.00

niesteszeck

OK
so some signal is misinterpreted, can you try the updated branch or just change the code  on line 60
from
Code: [Select]
delayMicroseconds(40);
to
Code: [Select]
delayMicroseconds(25);

Go Up