Unable to get results from DHT22

Hi,

I have a core from the origional kickstarter project, however only recently have I had the time to sit down and play with the unit.

I did a firmware update (including the depp firmware update) and setup with a basic breadboard and a DHT22 to capture temp/humidity, my code flashes fine and runs however it appears to never be able to display the data on either serial or the particle console, code is below;

#include "PietteTech_DHT/PietteTech_DHT.h"  // Uncomment if building in IDE

// system defines
#define DHTTYPE  DHT22              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   3         	    // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   2000  // Sample every two seconds

/*
 * NOTE: Use of callback_wrapper has been deprecated but left in this example
 *       to confirm backwards compabibility.  Look at DHT_2sensor for how
 *       to write code without the callback_wrapper
 */

double temperature;
double humidity;

// globals
unsigned int DHTnextSampleTime;	    // Next time we want to start sample
bool bDHTstarted;		    // flag to indicate we started acquisition
int n;                              // counter

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

PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

void dht_wrapper() {
  DHT.isrCallback();
}

void setup()
{
    WiFi.on();
    Particle.connect();
          // Variables Exposed to Cloud
    Particle.variable("temperature", temperature);
    Particle.variable("humidity", humidity);
    Particle.variable("time", Time.now());
    Particle.publish("message: ", "I'm alive!");
    Serial.begin(9600);
    DHTnextSampleTime = 0;  // Start the first sample immediately
}


void loop()
{
    

  // Check if we need to start the next sample
  if (millis() > DHTnextSampleTime) {
	if (!bDHTstarted) {		// start the sample
	    Particle.publish("message: ", "In loop() and trying to acquire from sensor");
	    DHT.acquire();
	    bDHTstarted = true;
	    Particle.publish("message: ", "Finished getting data from sensor");
	}

	if (!DHT.acquiring()) {		// has sample completed?

	    // get DHT status
	    Particle.publish("message: ", "Going to check the result");
	    int result = DHT.getStatus();

	    Serial.print("Read sensor: ");
	    Particle.publish("message: ", "Read sensor");
	    switch (result) {
		case DHTLIB_OK:
		    Serial.println("OK");
		    Particle.publish("message: ", "Good Data");
		    break;
		case DHTLIB_ERROR_CHECKSUM:
		    Serial.println("Error\n\r\tChecksum error");
			Particle.publish("message: ", "Checksum sensor");    
		    break;
		case DHTLIB_ERROR_ISR_TIMEOUT:
		    Serial.println("Error\n\r\tISR time out error");
		    Particle.publish("message: ", "ISR time out error");	    
		    break;
		case DHTLIB_ERROR_RESPONSE_TIMEOUT:
		    Serial.println("Error\n\r\tResponse time out error");
		    Particle.publish("message: ", "Response time out error");	    
		    break;
		case DHTLIB_ERROR_DATA_TIMEOUT:
		    Serial.println("Error\n\r\tData time out error");
		    Particle.publish("message: ", "Data time out error");	    
		    break;
		case DHTLIB_ERROR_ACQUIRING:
		    Serial.println("Error\n\r\tAcquiring");
		    Particle.publish("message: ", "Error Acquiring from sensor");
		    break;
		case DHTLIB_ERROR_DELTA:
		    Serial.println("Error\n\r\tDelta time to small");
		    Particle.publish("message: ", "Delta time to small");	    
		    break;
		case DHTLIB_ERROR_NOTSTARTED:
		    Serial.println("Error\n\r\tNot started");
		    Particle.publish("message: ", "Not started");	    
		    break;
		default:
		    Serial.println("Unknown error");
		    Particle.publish("message: ", "WTF");	    
		    break;
	    }

        humidity = DHT.getHumidity();
        temperature = DHT.getCelsius();
	
        Serial.printlnf("Humidity\t: %.2f %%", humidity);
        Serial.printlnf("Temperature\t: %.2f °C", temperature);
        Particle.publish("Humidity", String(humidity) + "%");
        Particle.publish("Temperature", String(temperature) + " °C");

	    n++;  // increment counter
	    bDHTstarted = false;  // reset the sample flag so we can take another
	    DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;  // set the time for next sample
	}
    }

}

The particle console displays the “Finished getting data from sensor” message but never moves on to displaying the actual data

Any help would be appreciated!

Rhys

Do you see the “Going to check the result” message get published? If not, you should check that your DHT22 is connected properly; either it’s not or it’s possible that you have a defective device.

Thanks for the response, I have swapped out for a second DHT I have with no change, also I put both DHT’s into an arduino uno and they have worked fine

Back to my first question; do you see the “Going to check the result” message get published?
I ran your code on my Photon, and it works fine there. I’m not sure why it would be any different on a Core.

Post a picture of your setup and wiring.

I dug out one of my old Cores, and I am getting the same results you are. DHT.acquiring() always returns true, so it never makes it past that test. I tried powering it from 3v3 and from Vin, with and without a 1k pullup resistor on the data line (it works without that on the Photon, but I’ve seen some tutorials that use it, and others that do not).

After Edit: I compiled against 0.6.0 (above), then I tried 0.5.3 and 0.4.9. It finally worked with 0.4.9. The PietteTech library uses interrupts, and there is a known issue with interrupts and the Core in firmware versions 0.5.3 and up. See this link.

@ric, does reenabling interrupts in setup() or STARTUP() cure the problem for you on 0.5.3ff?

It should work, but extra confirmation always helps :wink:
https://github.com/spark/firmware/issues/1136

Adding interrupts(); to setup() on firmware 0.6 has fixed everything, getting good consistent results from the core/dht22 now

Thanks - pretty sure I wouldn’t have figured this one out myself!

2 Likes

Do take a look at how many publishes you’re pushing out without any noticeable delays. Currently I can count 5 of them right after one another, which will exceed the publish limit. Do take that into consideration;)

2 Likes

Sometimes (on 0.6.0), but sometimes I'm getting an SOS (invalid case), so I think my Core has other problems. I haven't used it in a long time. It may be in need of a deep update.

@kebabman Here is an interesting article on Hackaday showing how bad the DHT 22 sensor is, it’s worse than I thought and it’s not something that can be fixed via software coding. Figure I would share it with you.