pietteTech_DHT library does not work with latest Particle OS (redux)

There have been several discussions on this topic but all are now closed and (as far as I can see) without resolution. I have a legacy Photon based project (circa 2017) that was running on OS v0.61 and uses a DHT11 with whatever version of this library was current at that time. I recently went to make a small change to the code and upgraded to the latest particle OS (3.0.0 rc.1). I also upgraded the pietteTech_DHT library to the latest version (0.0.12). I found that I am not getting data from the DHT11. I went back to the example code (below) and tried this simplest non-blocking example and it indeed hangs up. Here is the result:

Press any key to start.
DHT Simple program using DHT.acquireAndWait
LIB version: 0.0.12
---------------

0: Retrieving information from sensor: Read sensor:

My configuration is as follows: DHT11 is powered from the Photon’s 3.3 volt power supply. The DHT11 data pin is connected to Photon D2 and pulled up to the 3.3 volt power supply with a 4.7 Kohm resistor. I have two instances of this hardware, both of which have been working with the legacy firmware since 2017, and neither of which work with the latest build of Particle OS and latest pietteTech_DHT library. The example code that produced the hung-up result (above) was compiled against OS v2.0.1 and OS 3.0.0 rc .1 and fails (hangs up without obtaining status or data from the DHT11). Here is the test code that I used to produce the results, above:

// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>

/*
 * FILE:        DHT_simple.cpp
 * VERSION:     0.4
 * PURPOSE:     Example that uses DHT library with two sensors
 * LICENSE:     GPL v3 (http://www.gnu.org/licenses/gpl.html)
 *
 * Samples one sensor and monitors the results for long term
 * analysis.  It calls DHT.acquireAndWait
 *
 * Scott Piette (Piette Technologies) scott.piette@gmail.com
 *      January 2014        Original Spark Port
 *      October 2014        Added support for DHT21/22 sensors
 *                          Improved timing, moved FP math out of ISR
 *      September 2016      Updated for Particle and removed dependency
 *                          on callback_wrapper.  Use of callback_wrapper
 *                          is still for backward compatibility but not used
 */

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

#define DHTTYPE  DHT11       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D2           // Digital pin for communications

/*
 * NOTE: Use of callback_wrapper has been deprecated
 */
//declaration
//void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
int n;      // counter

void setup()
{
    Serial.begin(9600);
    while (!Serial.available()) {
        Serial.println("Press any key to start.");
		Particle.process();
        delay (1000);
    }
    Serial.println("DHT Simple program using DHT.acquireAndWait");
    Serial.print("LIB version: ");
    Serial.println(DHTLIB_VERSION);
    Serial.println("---------------");
}

/*
 * NOTE:  Use of callback_wrapper has been deprecated but left in this example
 * to confirm backwards compabibility.
 */
// This wrapper is in charge of calling
// must be defined like this for the lib work
//void dht_wrapper() {
//    DHT.isrCallback();
//}

void loop()
{
    Serial.print("\n");
    Serial.print(n);
    Serial.print(": Retrieving information from sensor: ");
    Serial.print("Read sensor: ");
    //delay(100);
  
    int result = DHT.acquireAndWait();

    switch (result) {
        case DHTLIB_OK:
            Serial.println("OK");
            break;
        case DHTLIB_ERROR_CHECKSUM:
            Serial.println("Error\n\r\tChecksum error");
            break;
        case DHTLIB_ERROR_ISR_TIMEOUT:
            Serial.println("Error\n\r\tISR time out error");
            break;
        case DHTLIB_ERROR_RESPONSE_TIMEOUT:
            Serial.println("Error\n\r\tResponse time out error");
            break;
        case DHTLIB_ERROR_DATA_TIMEOUT:
            Serial.println("Error\n\r\tData time out error");
            break;
        case DHTLIB_ERROR_ACQUIRING:
            Serial.println("Error\n\r\tAcquiring");
            break;
        case DHTLIB_ERROR_DELTA:
            Serial.println("Error\n\r\tDelta time to small");
            break;
        case DHTLIB_ERROR_NOTSTARTED:
            Serial.println("Error\n\r\tNot started");
            break;
        default:
            Serial.println("Unknown error");
            break;
    }
    Serial.print("Humidity (%): ");
    Serial.println(DHT.getHumidity(), 2);

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

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

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

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

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

    n++;
    delay(2500);
}

I need a non-blocking library for the DHT11 in my project. I would like to know what versions of Particle OS and the pietteTech_DHT library still work, and how to downgrade my Photons to the earlier OS version if that is what it takes. Alternative solutions would also be appreciated. I really should have a more recent OS (than 0.61) on the Photon because of improved reporting and health checks, as well as improved performance.

I don’t see a DHT.begin() call in your setup().

I’m pretty sure I also pointed this out in many of the other threads talking about issues with that library.
If you can link to some specific threads that suffer from the same issue but lack that info I’ll be happy to add it there too :wink:

@ScruffeR: Thank you -- all is now well! This method call was not in our original 2017 code (which worked fine), so I guess that DHT>begin() was either added later or at least was not needed way back when.

This may have been the problem with this posting as well:

Anyway - thank you! The problem is solved.

That is true.
At some point (2/2019) it became unsafe to call pinMode() in a constructor and shortly after (11/2019) also disallowed to call detachInterrupt() from within the ISR, hence it was required to move these calls into a explicitly callable function.
However, that call is also present in the library examples. So whenever moving on to a newer version of a library it's advisable to also check the provided samples against your legacy code.

About the post you linked, that was from before the addition of DHT.begin() and since the OP never responded to my suggestions it's hard to tell what the cause actually was.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.