dht22 with relay irregular measurement and operation



  • Double dht22 with relay is working good but the 2 DHT's give logging irregular
    H1= 50.20
    H0= 75.70
    H0= 75.80
    T1= 23.90
    H1= 50.30
    T0= 21.40
    H0= 75.70
    T0= 21.50
    H0= 75.90
    T0= 21.40
    H0= 75.80

    is it possible to measure in an sequence? Also the relay cannot be used by the button everytime I like, also immediate after the measurement it can be used. is there something wrong in the delay?
    Here the code as it is now

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_HUM2 1
    #define CHILD_ID_TEMP1 3
    #define CHILD_ID_TEMP2 4
    
    // RelayWithActuator stuff
    #define RELAY_PIN  6  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  7  // Arduino Digital I/O pin number for button 
    #define CHILD_ID 5   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    Bounce debouncer = Bounce(); 
    bool state;
    MySensor gw;
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    #define HEARTBEAT       10
    unsigned int     timer = 0;
    // Sleep time between reads (in milliseconds)
    #define SLEEP_TIME      3000 
    
    DHT* dht[2];
    byte sensorPin[2] = {3, 4};
    float lastTemp[2] = {0.0, 0.0};
    float lastHum[2] = {0.0, 0.0};
    
    boolean metric = true;
    //MyMessage msgHum(CHILD_ID_HUM1, V_HUM);
    //MyMessage msgTemp(CHILD_ID_TEMP1, V_TEMP);
    
    void setup()
    {
      gw.begin(incomingMessage, AUTO, true);
      for (int i = 0; i < 2; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      gw.sendSketchInfo("HumidityAndRelay", "1.0");
    
      gw.present(CHILD_ID_HUM1, S_HUM);
      gw.present(CHILD_ID_HUM2, S_HUM);
      gw.present(CHILD_ID_TEMP1, S_TEMP);
      gw.present(CHILD_ID_TEMP1, S_TEMP);
    
       // Setup the button and Activate internal pull-up
      pinMode(BUTTON_PIN,INPUT_PULLUP);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
     
      timer = 0;
      metric = gw.getConfig().isMetric;
    }
    
    void loop()
    {
      gw.process(); 
      timer++;
      
      if (timer > (SLEEP_TIME / HEARTBEAT))   
      { 
        // Reset timer again and for the next timed loop
        timer = 0;
        for (int i = 0; i < 2; i++)
        {
          delay(dht[i]->getMinimumSamplingPeriod());
          float temperature = dht[i]->getTemperature();
          if (isnan(temperature))
          {
            Serial.print(F("Failed reading temperature from DHT"));
            Serial.println(i);
          }
          else if (temperature != lastTemp[i])
          {
            lastTemp[i] = temperature;
            if (!metric)
            {
              temperature = dht[i]->toFahrenheit(temperature);
            }
            //gw.send(msgTemp.set(temperature, i));
            Serial.print(F("T"));
            Serial.print(i);
            Serial.print(F("= "));
            Serial.println(temperature);
          }
          float humidity = dht[i]->getHumidity();
          if (isnan(humidity))
          {
            Serial.print("Failed reading humidity from DHT");
            Serial.println(i);
          }
          else if (humidity != lastHum[i])
          {
            lastHum[i] = humidity;
            //gw.send(msgHum.set(humidity, 1));
            Serial.print(F("H"));
            Serial.print(i);
            Serial.print(F("= "));
            Serial.println(humidity);
          }
        }
      } 
      
      // Get the button update state (true if a change was detected) 
      if (debouncer.update() && debouncer.read() == 0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      
      gw.wait(HEARTBEAT); //sleep a bit
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    


  • somebody any suggestions?



  • So I'm new to all this, however anytime I get funky things going on when I have multiple sensors on a node I found that introducing a delay in between the readings helped smooth things out. Hopefully someone will correct me if I'm wrong, but I have had some luck with delays. Also I built one to control my hot tub and I found noise from the pump messed things up, de-coupling capacitors became my friend after that.


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 2
  • 1
  • 2
  • 1

52
Online

11.4k
Users

11.1k
Topics

112.6k
Posts