3 DHT22s w/ Timer - Runs through once, then stops

Hello Smart Coders,

I’ve been trying hard to work on my own, research the Community find my own answers. I’m having some trouble running three DHT22’s. I run the code, it seems to “partially” run through one time, then it stops. I’m not sure it the timer isn’t coded right, or if I’m supposed to add another line to indicate the loop. Or, if there’s a better timer to use. If you could glance through it and let me know. I’ll worry about incorporating it into Blynk after the Serial Monitor looks good.

Photon Particle
(3) DHT22
Particle Web IDE
PietteTech_DHT Lib
SparkCorePolledTimer

THANKS EVERYONE !!! @peekay123 @kennethlimcp @Moors7

    // This #include statement was automatically added by the Particle IDE.
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"

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

#define DHTTYPEA  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINA   A0           // Digital pin for comunications
#define DHTTYPEB  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINB   A1           // Digital pin for comunications
#define DHTTYPEC  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINC   A3           // Digital pin for comunications

SparkCorePolledTimer TstatTimer(2500); //Create a timer object and set it's timeout in milliseconds

void Tstat(void); // set's name of timer object to be used.  To run the Tstat section of code

//declaration
void dht_wrapperA(); // must be declared before the lib initialization
void dht_wrapperB(); // must be declared before the lib initialization
void dht_wrapperC(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHTA(DHTPINA, DHTTYPEA, dht_wrapperA);
PietteTech_DHT DHTB(DHTPINB, DHTTYPEB, dht_wrapperB);
PietteTech_DHT DHTC(DHTPINC, DHTTYPEC, dht_wrapperC);
int n;      // counter

//#define LOOP_DELAY 15000 // 1000 per Second intervals
int _sensorA_error_count;
int _sensorB_error_count;
int _sensorC_error_count;
int _spark_error_count;
unsigned long _lastTimeInLoop;
bool active_wrapper;    // NOTE: Do not remove this, it prevents compiler from optimizing

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapperA() {
    active_wrapper = true;      // NOTE: Do not remove this, it prevents compiler from optimizing
    DHTA.isrCallback();
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapperB() {
    active_wrapper = false;      // NOTE: Do not remove this, it prevents compiler from optimizing
    DHTB.isrCallback();
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapperC() {
    active_wrapper = false;      // NOTE: Do not remove this, it prevents compiler from optimizing
    DHTC.isrCallback();
}

void setup()
{
    Serial.begin(9600);
    TstatTimer.SetCallback(Tstat);

  pinMode( 5 , OUTPUT); // Whole House Fan #1 Motor 4amp
  pinMode( 4 , OUTPUT); // Whole House Fan #2 Motor 4amp
  pinMode( 7 , OUTPUT); // Door Double on Digital Pin D7
  pinMode( 6 , OUTPUT); // Door Single on Digital Pin D6
// The next two lines shutdown the relay so when the power bumps your doors don't pop open. 
  digitalWrite( 7 , LOW );
  digitalWrite( 6 , LOW );
  digitalWrite( 5 , HIGH );
  digitalWrite( 4 , HIGH );    

    Serial.println("DHT Example program using 3 DHT sensors");
    Serial.println("---> DHT.acquire and DHT.aquiring");
    Serial.print("LIB version: ");
    Serial.println(DHTLIB_VERSION);
    Serial.println("---------------");

    delay(500);        // Delay to let the sensors settle
    _lastTimeInLoop = millis();
}

#if defined(DHT_REPORT_TIMING)
// This function will report the timings collected
void printEdgeTiming(class PietteTech_DHT *_d) {
    byte n;
    volatile uint8_t *_e = &_d->_edges[0];
    
    Serial.print("Edge timing = ");
    for (n = 0; n < 41; n++) {
        Serial.print(*_e++);
        if (n < 40)
            Serial.print(".");
    }
    Serial.print("\n\r");
}
#endif

void printSensorData(class PietteTech_DHT *_d) {
    int result = _d->getStatus();

    if (result != DHTLIB_OK)
        if (_d == &DHTA)
            _sensorA_error_count++;
        else
            _sensorB_error_count++;
        else
            _sensorC_error_count++;            
        
    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;
    }

#if defined(DHT_REPORT_TIMING)
    // print debug timing information
    printEdgeTiming(_d);
#endif
    Serial.print("Humidity (%): ");
    Serial.println(_d->getHumidity(), 3);
    Serial.print("Temperature (oF): ");
    Serial.println(_d->getFahrenheit(), 3);
    Serial.print("Temperature (K): ");
    Serial.println(_d->getKelvin(), 3);
}
    
void loop ()
{
       TstatTimer.Update();
}

void Tstat(void)
{
    unsigned long _us = millis();
    int _delta = (_us - _lastTimeInLoop);

/*    if (_delta > (1.05 * LOOP_DELAY))
        _spark_error_count++;
*/
    // Launch the acquisition on the two sensors
    DHTA.acquire();
    DHTB.acquire();
    DHTC.acquire();
    
    // Print information for Sensor A
    int InsideT = DHTA.getFahrenheit();
    int InsideH = DHTA.getHumidity();
    int InsideK = DHTA.getKelvin();
    int InsideD = DHTA.getDewPoint();
    
    Serial.print("\n");
    Serial.print(n);
    Serial.print(" : ");
    Serial.print((float) (_delta / 1000.0));
    Serial.print("s");
    if (_sensorA_error_count > 0 || _spark_error_count > 0) {
        Serial.print(" : E=");
        Serial.print(_sensorA_error_count);
        Serial.print("/");
        Serial.print(_spark_error_count);
    }
    Serial.print(", Retrieving information from sensor: ");
    Serial.print("Read sensor INSIDE: ");

    while (DHTA.acquiring()) ;
    printSensorData(&DHTA);

    // Print information for Sensor B
    int OutsideT = DHTB.getFahrenheit();
    int OutsideH = DHTB.getHumidity();
    int OutsideK = DHTB.getKelvin();
    int OutsideD = DHTB.getDewPoint();
    
    Serial.print("\n");
    Serial.print(n);
    Serial.print(" : ");
    Serial.print((float) (_delta / 1000.0));
    Serial.print("s");
    if (_sensorB_error_count > 0 || _spark_error_count > 0) {
        Serial.print(" : E=");
        Serial.print(_sensorB_error_count);
        Serial.print("/");
        Serial.print(_spark_error_count);
    }
    Serial.print(", Retrieving information from sensor: ");
    Serial.print("Read sensor OUTSIDE: ");

    while (DHTB.acquiring()) ;
    printSensorData(&DHTB);
    
    // Print information for Sensor C
    int AtticT = DHTC.getFahrenheit();
    int AtticH = DHTC.getHumidity();
    int AtticK = DHTC.getKelvin();
    int AtticD = DHTC.getDewPoint();
   
    Serial.print("\n");
    Serial.print(n);
    Serial.print(" : ");
    Serial.print((float) (_delta / 1000.0));
    Serial.print("s");
    if (_sensorC_error_count > 0 || _spark_error_count > 0) {
        Serial.print(" : E=");
        Serial.print(_sensorC_error_count);
        Serial.print("/");
        Serial.print(_spark_error_count);
    }
    Serial.print(", Retrieving information from sensor: ");
    Serial.print("Read sensor ATTIC: ");

    while (DHTC.acquiring()) ;
    printSensorData(&DHTC);
    

    n++;
    _lastTimeInLoop = _us;
 /*   
/// Inside TStat Info Blynk Info POST
  Blynk.virtualWrite(V01, InsideT);
  Blynk.run();
  Blynk.virtualWrite(V02, InsideH);
  Blynk.run();
  Blynk.virtualWrite(V03, InsideD;
  Blynk.run();
  Blynk.virtualWrite(V04, InsideK);
  Blynk.run();
  
/// Outside TStat Info  
  Blynk.virtualWrite(V05, OutsideT);
  Blynk.run();
  Blynk.virtualWrite(V06, OutsideH);
  Blynk.run();
  Blynk.virtualWrite(V07, OutsideD);
  Blynk.run();
  Blynk.virtualWrite(V08, OutsideK);
  Blynk.run();
  
/// Attic TStat Info  
  Blynk.virtualWrite(V09, OutsideT);
  Blynk.run();
  Blynk.virtualWrite(V10, OutsideH);
  Blynk.run();
  Blynk.virtualWrite(V11, OutsideD);
  Blynk.run();
  Blynk.virtualWrite(V12, OutsideK);
  Blynk.run();
  
  */
}

Not 100% sure but does this library do a repeated interval call to the function?

I would test without the library and make sure the sensors are giving me values every 2 seconds just to be sure the DHT side is working well first.

1 Like

@kennethlimcp That’s kind-of where I came from. Whenever I buy new sensors, I throw them on a solo BB and test that sensor code singly to make sure I get what I need to get out of them . This code, and these sensors worked great! They all kicked out the parameters they were supposed to, to the Serial Monitor (temp, hum, kelvin, etc) I carefully (I thought) placed it in my main code, the three sensors took so long to read and such when it was in the void loop () that it caused huge delays to simple relays firing on demand from Blynk (that worked great before). I read @ScruffR comments on the board about not putting delays () and while() inside the loop so I tried to use a software timer but it reads one of the three sensors then freezes up. I’m not fully tuned up on how exactly a software timer works? I get the class and calls and such, the syntax of it all, but at the end of the void loop(), what makes it go to the void (Anything) and run that again delay or not? I started to read up on @peekay123 's “SparkIntervalTimer.h” Github library and example, googled to no avail for other examples but still, I don’t understand what runs it again and again, after the anything.end(); or anythingTimer.Update(); being outside the void loop() {} ??? Advice ???

Thanks for acknowledging and answering my post and cry for help !!

Lane